read

Reads a POD struct from a range of bytes.

  1. T read(Range input)
    T
    read
    (
    T
    Range
    )
    (
    Range input
    )
    if (
    isInputRange!Range
    &&
    is(ElementType!Range : const ubyte)
    )
  2. void read(Range input, T val)
  3. void read(Range input, T* val)

Parameters

T

type to read

input Range

bytes to read data from

Examples

Read struct from raw data

ubyte[] data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
align(1)
struct X {
	align(1):
	ubyte a;
	ushort b;
	uint c;
	ulong d;
	ubyte[5] e;
}

auto readData = data.read!X();
with (readData) {
	assert(a == 0);
	assert(b == 0x201);
	assert(c == 0x6050403);
	assert(d == 0x0E0D0C0B0A090807);
	assert(e == [15, 16, 17, 18, 19]);
}

Read struct from raw data with destination premade

ubyte[] data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
align(1)
struct X {
	align(1):
	ubyte a;
	ushort b;
	uint c;
	ulong d;
	ubyte[5] e;
}

X readData;
data.read!X(readData);
with (readData) {
	assert(a == 0);
	assert(b == 0x201);
	assert(c == 0x6050403);
	assert(d == 0x0E0D0C0B0A090807);
	assert(e == [15, 16, 17, 18, 19]);
}

Read struct from raw data with destination preallocated on heap

ubyte[] data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
align(1)
struct X {
	align(1):
	ubyte a;
	ushort b;
	uint c;
	ulong d;
	ubyte[5] e;
}

auto readData = new X;
data.read!X(readData);
with (readData) {
	assert(a == 0);
	assert(b == 0x201);
	assert(c == 0x6050403);
	assert(d == 0x0E0D0C0B0A090807);
	assert(e == [15, 16, 17, 18, 19]);
}

Meta