Added a helper proc: DelayedWriteCursor.totalBytesWrittenAfterCursor

Delayed writes a typically used to implement length-prefixed messages
and this procs makes it very easy to handle this use case.
This commit is contained in:
Zahary Karadjov 2019-06-16 18:49:49 +03:00
commit 1d693484f6
2 changed files with 51 additions and 12 deletions

View file

@ -136,6 +136,17 @@ proc decRef(x: var int16): int16 =
doAssert result >= 0
x = result
proc totalBytesWrittenAfterCursor*(cursor: DelayedWriteCursor): int =
template s: auto = cursor.stream
let
spanningPagesTotal = (cursor.stream.pages.len - cursor.page) * pageSize
deductedFromFirstPage = distance(unsafeAddr s.pages[cursor.page].buffer[0],
cursor.bufferEnd)
deductedFromLastPage = distance(s.head, s.bufferEnd)
spanningPagesTotal - deductedFromFirstPage - deductedFromLastPage
proc endWrite*(cursor: DelayedWriteCursor, data: openarray[byte]) =
if data.len != distance(cursor.head, cursor.bufferEnd):
doAssert false