byChunkAlloc

Allows access to a stream's data as an input range, chunk-by-chunk.

The resulting range is only empty once the stream has no more data to read.

Notes: The stream's current position will be used.

No part of this function or the resulting range uses GC memory. GC memory is only used if Alloc uses the GC, and/or if the given stream uses the GC.

The resulting range uses std.typecons.RefCounted for it's internal buffer, so copying the range has a small overhead.

  1. ByChunkAllocRange!Alloc byChunkAlloc(Stream stream, size_t bufferSize)
    byChunkAlloc
    (
    Alloc
    )
    if (
    AllocHelper!Alloc.IsStatic
    )
  2. ByChunkAllocRange!Alloc byChunkAlloc(Stream stream, size_t bufferSize, Alloc alloc)

Parameters

stream Stream

The Stream to use.

bufferSize size_t

The size of the buffer, this also determines the max size of each chunk.

Examples

import std.experimental.allocator.mallocator : Mallocator;
import std.algorithm                         : all;
import jaster.stream.memory                  : MemoryStreamGC;

auto stream = new MemoryStreamGC();
foreach(i; 0..129)
    stream.write([cast(ubyte)i]);

stream.position = 0;
assert(stream.byChunkAlloc!Mallocator(4).all!(arr => arr.length > 0));
import std.experimental.allocator.building_blocks : StatsCollector, Options;
import std.experimental.allocator.mallocator      : Mallocator;
import std.algorithm                              : all;
import jaster.stream.memory                       : MemoryStreamGC;

alias Alloc = StatsCollector!(Mallocator, Options.all, Options.all);
auto alloc  = new Alloc();

auto stream = new MemoryStreamGC();
foreach(i; 0..129)
    stream.write([cast(ubyte)i]);

stream.position = 0;
assert(stream.byChunkAlloc!(Alloc*)(4, alloc).all!(arr => arr.length > 0));
assert(alloc.bytesUsed == 0);

// TODO: Test with more complex range chains, including holding the resulting range in a variable and doing stuff with it manually.
//       If that all passes with 0 bytes used in the end, and 0 crashes, then it should all be good :)

Meta