mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2026-01-09 22:28:27 -05:00
chore: removing unused type StreamSeq (#1507)
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
# Nim-LibP2P
|
||||
# Copyright (c) 2023 Status Research & Development GmbH
|
||||
# Licensed under either of
|
||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||
# at your option.
|
||||
# This file may not be copied, modified, or distributed except according to
|
||||
# those terms.
|
||||
|
||||
{.push raises: [].}
|
||||
|
||||
import stew/bitops2
|
||||
|
||||
type StreamSeq* = object
|
||||
# Seq adapted to the stream use case where we add data at the back and
|
||||
# consume at the front in chunks. A bit like a deque but contiguous memory
|
||||
# area - will try to avoid moving data unless it has to, subject to buffer
|
||||
# space. The assumption is that data is typically consumed fully.
|
||||
#
|
||||
# See also asio::stream_buf
|
||||
buf: seq[byte] # Data store
|
||||
rpos: int # Reading position - valid data starts here
|
||||
wpos: int # Writing position - valid data ends here
|
||||
|
||||
template len*(v: StreamSeq): int =
|
||||
v.wpos - v.rpos
|
||||
|
||||
func grow(v: var StreamSeq, n: int) =
|
||||
if v.rpos == v.wpos:
|
||||
# All data has been consumed, reset positions
|
||||
v.rpos = 0
|
||||
v.wpos = 0
|
||||
|
||||
if v.buf.len - v.wpos < n:
|
||||
if v.rpos > 0:
|
||||
# We've consumed some data so we'll try to move that data to the beginning
|
||||
# of the buffer, hoping that this will clear up enough capacity to avoid
|
||||
# reallocation
|
||||
moveMem(addr v.buf[0], addr v.buf[v.rpos], v.wpos - v.rpos)
|
||||
v.wpos -= v.rpos
|
||||
v.rpos = 0
|
||||
|
||||
if v.buf.len - v.wpos >= n:
|
||||
return
|
||||
|
||||
# TODO this is inefficient - `setLen` will copy all data of buf, even though
|
||||
# we know that only a part of it contains "valid" data
|
||||
v.buf.setLen(nextPow2(max(64, v.wpos + n).uint64).int)
|
||||
|
||||
template prepare*(v: var StreamSeq, n: int): var openArray[byte] =
|
||||
## Return a buffer that is at least `n` bytes long
|
||||
mixin grow
|
||||
v.grow(n)
|
||||
|
||||
v.buf.toOpenArray(v.wpos, v.buf.len - 1)
|
||||
|
||||
template commit*(v: var StreamSeq, n: int) =
|
||||
## Mark `n` bytes in the buffer returned by `prepare` as ready for reading
|
||||
v.wpos += n
|
||||
|
||||
func add*(v: var StreamSeq, data: openArray[byte]) =
|
||||
## Add data - the equivalent of `buf.prepare(n) = data; buf.commit(n)`
|
||||
if data.len > 0:
|
||||
v.grow(data.len)
|
||||
copyMem(addr v.buf[v.wpos], unsafeAddr data[0], data.len)
|
||||
v.commit(data.len)
|
||||
|
||||
template data*(v: StreamSeq): openArray[byte] =
|
||||
# Data that is ready to be consumed
|
||||
# TODO a double-hash comment here breaks compile (!)
|
||||
v.buf.toOpenArray(v.rpos, v.wpos - 1)
|
||||
|
||||
template toOpenArray*(v: StreamSeq, b, e: int): openArray[byte] =
|
||||
# Data that is ready to be consumed
|
||||
# TODO a double-hash comment here breaks compile (!)
|
||||
v.buf.toOpenArray(v.rpos + b, v.rpos + e - b)
|
||||
|
||||
func consume*(v: var StreamSeq, n: int) =
|
||||
## Mark `n` bytes that were returned via `data` as consumed
|
||||
v.rpos += n
|
||||
|
||||
func consumeTo*(v: var StreamSeq, buf: var openArray[byte]): int =
|
||||
let bytes = min(buf.len, v.len)
|
||||
if bytes > 0:
|
||||
copyMem(addr buf[0], addr v.buf[v.rpos], bytes)
|
||||
v.consume(bytes)
|
||||
bytes
|
||||
|
||||
func clear*(v: var StreamSeq) =
|
||||
v.consume(v.len)
|
||||
|
||||
func assign*(v: var StreamSeq, buf: openArray[byte]) =
|
||||
v.clear()
|
||||
v.add(buf)
|
||||
@@ -10,8 +10,8 @@
|
||||
# those terms.
|
||||
|
||||
import
|
||||
testvarint, testconnection, testbridgestream, testminprotobuf, teststreamseq,
|
||||
testsemaphore, testheartbeat, testfuture, testzeroqueue, testbytesview
|
||||
testvarint, testconnection, testbridgestream, testminprotobuf, testsemaphore,
|
||||
testheartbeat, testfuture, testzeroqueue, testbytesview
|
||||
|
||||
import testminasn1, testrsa, testecnist, tested25519, testsecp256k1, testcrypto
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
{.used.}
|
||||
|
||||
# Nim-Libp2p
|
||||
# Copyright (c) 2023 Status Research & Development GmbH
|
||||
# Licensed under either of
|
||||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||
# at your option.
|
||||
# This file may not be copied, modified, or distributed except according to
|
||||
# those terms.
|
||||
|
||||
import unittest2
|
||||
import stew/byteutils
|
||||
import ../libp2p/stream/streamseq
|
||||
|
||||
suite "StreamSeq":
|
||||
test "basics":
|
||||
var s: StreamSeq
|
||||
|
||||
check:
|
||||
s.data().len == 0
|
||||
|
||||
s.add([byte 0, 1, 2, 3])
|
||||
|
||||
check:
|
||||
@(s.data()) == [byte 0, 1, 2, 3]
|
||||
|
||||
s.prepare(10)[0 ..< 3] = [byte 4, 5, 6]
|
||||
|
||||
check:
|
||||
@(s.data()) == [byte 0, 1, 2, 3]
|
||||
|
||||
s.commit(3)
|
||||
|
||||
check:
|
||||
@(s.data()) == [byte 0, 1, 2, 3, 4, 5, 6]
|
||||
|
||||
s.consume(1)
|
||||
|
||||
check:
|
||||
@(s.data()) == [byte 1, 2, 3, 4, 5, 6]
|
||||
|
||||
s.consume(6)
|
||||
|
||||
check:
|
||||
@(s.data()) == []
|
||||
|
||||
s.add([])
|
||||
check:
|
||||
@(s.data()) == []
|
||||
|
||||
var o: seq[byte]
|
||||
|
||||
check:
|
||||
0 == s.consumeTo(o)
|
||||
|
||||
s.add([byte 1, 2, 3])
|
||||
|
||||
o.setLen(2)
|
||||
o.setLen(s.consumeTo(o))
|
||||
check:
|
||||
o == [byte 1, 2]
|
||||
|
||||
o.setLen(s.consumeTo(o))
|
||||
|
||||
check:
|
||||
o == [byte 3]
|
||||
Reference in New Issue
Block a user