mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-02-27 12:00:44 +00:00
moved several utils
This commit is contained in:
parent
6bdae367a2
commit
03bd04544e
@ -6,7 +6,7 @@ template toFuture*[T](v: T): Future[T] =
|
||||
fut.complete(v)
|
||||
fut
|
||||
|
||||
template toThrough*[T](s: Stream[T]): Through[T] =
|
||||
proc toThrough*[T](s: Stream[T]): Through[T] {.inline.} =
|
||||
proc sinkit(i: Source[T]) {.async.} =
|
||||
await s.sink()(i)
|
||||
|
||||
@ -29,6 +29,12 @@ template pipe*[T](s: Stream[T] | Source[T],
|
||||
|
||||
template pipe*[T](s: Stream[T] | Source[T],
|
||||
x: Stream[T] | Sink[T]): Future[void] =
|
||||
var pipeline: Source[T]
|
||||
when s is Source[T]:
|
||||
pipeline = s
|
||||
elif s is Stream[T]:
|
||||
pipeline = s.source
|
||||
|
||||
var terminal: Future[void]
|
||||
when x is Stream[T]:
|
||||
terminal = x.sink()(pipeline)
|
||||
@ -41,6 +47,25 @@ template pipe*[T](s: Stream[T] | Source[T],
|
||||
t: varargs[Through[T]],
|
||||
x: Stream[T] | Sink[T]): Future[void] =
|
||||
var pipeline = pipe(s, t)
|
||||
pipe(s, x)
|
||||
pipe(pipeline, x)
|
||||
|
||||
proc appendNl*(): Through[seq[byte]] =
|
||||
proc append(item: Future[seq[byte]]): Future[seq[byte]] {.async.} =
|
||||
result = await item
|
||||
result.add(byte('\n'))
|
||||
|
||||
return proc(i: Source[seq[byte]]): Source[seq[byte]] {.gcsafe.} =
|
||||
return iterator(): Future[seq[byte]] {.closure.} =
|
||||
for item in i:
|
||||
yield append(item)
|
||||
|
||||
proc stripNl*(): Through[seq[byte]] =
|
||||
proc strip(item: Future[seq[byte]]): Future[seq[byte]] {.async.} =
|
||||
result = await item
|
||||
if result.len > 0 and result[^1] == byte('\n'):
|
||||
result.setLen(result.high)
|
||||
|
||||
return proc(i: Source[seq[byte]]): Source[seq[byte]] {.gcsafe.} =
|
||||
return iterator(): Future[seq[byte]] {.closure.} =
|
||||
for item in i:
|
||||
yield strip(item)
|
||||
|
Loading…
x
Reference in New Issue
Block a user