|
52 | 52 | next*: SafeGenNext[?!T]
|
53 | 53 |
|
54 | 54 | proc flatMap[T, U](
|
55 |
| - fut: Future[?!T], fn: SafeFunction[?!T, ?!U] |
| 55 | + fut: auto, fn: SafeFunction[?!T, ?!U] |
56 | 56 | ): Future[?!U] {.async: (raises: [CancelledError]).} =
|
57 |
| - let raisingFut = cast[Future[?!T].Raising([CancelledError])](fut) |
58 |
| - let t = await raisingFut |
| 57 | + let t = await fut |
59 | 58 | await fn(t)
|
60 | 59 |
|
61 | 60 | proc flatMap[T, U](
|
62 |
| - fut: Future[?!T], fn: SafeFunction[?!T, Option[?!U]] |
| 61 | + fut: auto, fn: SafeFunction[?!T, Option[?!U]] |
63 | 62 | ): Future[Option[?!U]] {.async: (raises: [CancelledError]).} =
|
64 |
| - let raisingFut = cast[Future[?!T].Raising([CancelledError])](fut) |
65 |
| - let t = await raisingFut |
| 63 | + let t = await fut |
66 | 64 | await fn(t)
|
67 | 65 |
|
68 | 66 | ########################################################################
|
@@ -141,13 +139,11 @@ proc finish*[T](self: SafeAsyncIter[T]): void =
|
141 | 139 | proc finished*[T](self: SafeAsyncIter[T]): bool =
|
142 | 140 | self.finished
|
143 | 141 |
|
144 |
| -iterator items*[T](self: SafeAsyncIter[T]): Future[?!T] = |
| 142 | +iterator items*[T](self: SafeAsyncIter[T]): auto {.inline.} = |
145 | 143 | while not self.finished:
|
146 | 144 | yield self.next()
|
147 | 145 |
|
148 |
| -iterator pairs*[T]( |
149 |
| - self: SafeAsyncIter[T] |
150 |
| -): tuple[key: int, val: Future[?!T]] {.inline.} = |
| 146 | +iterator pairs*[T](self: SafeAsyncIter[T]): auto {.inline.} = |
151 | 147 | var i = 0
|
152 | 148 | while not self.finished:
|
153 | 149 | yield (i, self.next())
|
@@ -176,25 +172,23 @@ proc mapFilter*[T, U](
|
176 | 172 | mapPredicate: SafeFunction[?!T, Option[?!U]],
|
177 | 173 | finishOnErr: bool = true,
|
178 | 174 | ): Future[SafeAsyncIter[U]] {.async: (raises: [CancelledError]).} =
|
179 |
| - var nextFutU: Option[Future[?!U]] |
| 175 | + var nextU: Option[?!U] |
180 | 176 |
|
181 | 177 | proc filter(): Future[void] {.async: (raises: [CancelledError]).} =
|
182 |
| - nextFutU = Future[?!U].none |
| 178 | + nextU = none(?!U) |
183 | 179 | while not iter.finished:
|
184 |
| - let futT: Future[?!T] = iter.next() |
| 180 | + let futT = iter.next() |
185 | 181 | if mappedValue =? await futT.flatMap(mapPredicate):
|
186 |
| - let fut: Future[?!U] = newFuture[?!U]("mapFilter.filter") |
187 |
| - fut.complete(mappedValue) |
188 |
| - nextFutU = fut.some |
| 182 | + nextU = some(mappedValue) |
189 | 183 | break
|
190 | 184 |
|
191 | 185 | proc genNext(): Future[?!U] {.async: (raises: [CancelledError]).} =
|
192 |
| - let futU = cast[Future[?!U].Raising([CancelledError])](nextFutU.unsafeGet) |
| 186 | + let u = nextU.unsafeGet |
193 | 187 | await filter()
|
194 |
| - await futU |
| 188 | + u |
195 | 189 |
|
196 | 190 | proc isFinished(): bool =
|
197 |
| - nextFutU.isNone |
| 191 | + nextU.isNone |
198 | 192 |
|
199 | 193 | await filter()
|
200 | 194 | SafeAsyncIter[U].new(genNext, isFinished, finishOnErr = finishOnErr)
|
|
0 commit comments