mirror of
https://github.com/status-im/nim-codex.git
synced 2025-01-10 02:45:59 +00:00
e47b38af11
* Fixes/workarounds for nimsuggest failures in codex.nim. * remove rng prefix - it appears to work now * format new's to be more consistent * making proc formatting a bit more consistent
48 lines
814 B
Nim
48 lines
814 B
Nim
import std/sequtils
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/codex/chunker
|
|
import pkg/codex/rng
|
|
|
|
export chunker
|
|
|
|
type
|
|
RandomChunker* = Chunker
|
|
|
|
proc new*(
|
|
T: type RandomChunker,
|
|
rng: Rng,
|
|
chunkSize = DefaultChunkSize,
|
|
size: int,
|
|
pad = false
|
|
): RandomChunker =
|
|
## Create a chunker that produces random data
|
|
##
|
|
|
|
var consumed = 0
|
|
proc reader(data: ChunkBuffer, len: int): Future[int]
|
|
{.async, gcsafe, raises: [Defect].} =
|
|
var alpha = toSeq(byte('A')..byte('z'))
|
|
|
|
if consumed >= size:
|
|
return 0
|
|
|
|
var read = 0
|
|
while read < len:
|
|
rng.shuffle(alpha)
|
|
for a in alpha:
|
|
if read >= len:
|
|
break
|
|
|
|
data[read] = a
|
|
read.inc
|
|
|
|
consumed += read
|
|
return read
|
|
|
|
Chunker.new(
|
|
reader = reader,
|
|
pad = pad,
|
|
chunkSize = chunkSize)
|