Implement InputStream.nonBlockingReads and document it

This commit is contained in:
Zahary Karadjov 2020-05-06 23:54:28 +03:00
commit ec5c3af313
No known key found for this signature in database
GPG key ID: C8936F8A3073D609
3 changed files with 56 additions and 0 deletions

View file

@ -274,6 +274,22 @@ func getBestContiguousRunway(s: InputStream): Natural =
flipPage s
result = s.span.len
template nonBlockingReads*(sp: InputStream|AsyncInputStream, newName, blk: untyped) =
let s = InputStream sp
const sname = astToStr(sp)
let vtable = s.vtable
s.vtable = nil
try:
let `newName` {.inject.} = s
blk
finally:
s.vtable = vtable
template nonBlockingReads*(sp: InputStream|AsyncInputStream, blk: untyped) =
nonBlockingReads(sp, astToStr(sp), blk)
func totalUnconsumedBytes*(s: InputStream): Natural =
## Returns the number of bytes that are currently sitting within the stream
## buffers and that can be consumed with `read` or `advance`.