Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for void[0] types. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions src/msgpack.d
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct RefBuffer


/**
* Writes the argument to buffer and stores the reference of writed content
* Writes the argument to buffer and stores the reference of writed content
* if the argument size is smaller than threshold,
* otherwise stores the reference of argument directly.
*
Expand Down Expand Up @@ -661,7 +661,7 @@ struct Packer(Stream) if (isOutputRange!(Stream, ubyte) && isOutputRange!(Stream


/// ditto
ref Packer pack(T)(in T array) if (isArray!T)
ref Packer pack(T)(in T array) if (isArray!T && !is(Unqual!T == void[0]))
{
alias typeof(T.init[0]) U;

Expand Down Expand Up @@ -706,6 +706,11 @@ struct Packer(Stream) if (isOutputRange!(Stream, ubyte) && isOutputRange!(Stream
return this;
}

/// ditto
ref Packer pack(T)(in T array) if (isArray!T && is(Unqual!T == void[0]))
{
return this;
}

/// ditto
ref Packer pack(T)(in T array) if (isAssociativeArray!T)
Expand Down Expand Up @@ -903,7 +908,7 @@ struct Packer(Stream) if (isOutputRange!(Stream, ubyte) && isOutputRange!(Stream
* packer.beginArray(3).pack(true, 1); // -> [true, 1,
*
* // other operation
*
*
* packer.pack("Hi!"); // -> [true, 1, "Hi!"]
* -----
*
Expand Down Expand Up @@ -987,7 +992,7 @@ Packer!(Stream) packer(Stream)(Stream stream, bool withFieldName = false)
}


version (unittest)
version (unittest)
{
alias Appender!(ubyte[]) SimpleBuffer;
alias packer packerBuilder; // Avoid issue: http://d.puremagic.com/issues/show_bug.cgi?id=9169
Expand Down Expand Up @@ -1020,7 +1025,7 @@ unittest
enum : ulong { A = ubyte.max, B = ushort.max, C = uint.max, D = ulong.max }

static UTest[][] tests = [
[{Format.UINT8, A}],
[{Format.UINT8, A}],
[{Format.UINT8, A}, {Format.UINT16, B}],
[{Format.UINT8, A}, {Format.UINT16, B}, {Format.UINT32, C}],
[{Format.UINT8, A}, {Format.UINT16, B}, {Format.UINT32, C}, {Format.UINT64, D}],
Expand Down Expand Up @@ -1059,7 +1064,7 @@ unittest
enum : long { A = byte.min, B = short.min, C = int.min, D = long.min }

static STest[][] tests = [
[{Format.INT8, A}],
[{Format.INT8, A}],
[{Format.INT8, A}, {Format.INT16, B}],
[{Format.INT8, A}, {Format.INT16, B}, {Format.INT32, C}],
[{Format.INT8, A}, {Format.INT16, B}, {Format.INT32, C}, {Format.INT64, D}],
Expand Down Expand Up @@ -1150,8 +1155,8 @@ unittest
}
{ // pointer
static struct PTest
{
ubyte format;
{
ubyte format;

union
{
Expand Down Expand Up @@ -1215,7 +1220,7 @@ unittest
auto test = tests[I];

foreach (i, T; TypeTuple!(ubyte, ushort, uint)) {
mixin DefinePacker;
mixin DefinePacker;
mixin("packer.begin" ~ Name ~ "(i ? test[i].value : A);");

assert(buffer.data[0] == test[i].format);
Expand Down Expand Up @@ -1346,7 +1351,7 @@ unittest
class UnpackException : MessagePackException
{
this(string message)
{
{
super(message);
}
}
Expand Down Expand Up @@ -1450,7 +1455,7 @@ version (D_Ddoc)
}
}
else
{
{
private mixin template InternalBuffer()
{
private:
Expand Down Expand Up @@ -1571,7 +1576,7 @@ else
{
const size = target.length;

buffer_ = new ubyte[](size > bufferSize ? size : bufferSize);
buffer_ = new ubyte[](size > bufferSize ? size : bufferSize);
used_ = size;
buffer_[0..size] = target;
}
Expand Down Expand Up @@ -1648,7 +1653,7 @@ struct Unpacker
* unpacker.unpack(b) // b is deserialized value or
* // assigns null if deserialized value is nil
* -----
*
*
* Params:
* value = the reference of value to assign.
*
Expand Down Expand Up @@ -1931,7 +1936,7 @@ struct Unpacker
* UnpackException when doesn't read from buffer or precision loss occurs and
* MessagePackException when $(D_PARAM T) type doesn't match serialized type.
*/
ref Unpacker unpack(T)(ref T array) if (isArray!T)
ref Unpacker unpack(T)(ref T array) if (isArray!T && !is(Unqual!T == void[0]))
{
alias typeof(T.init[0]) U;

Expand Down Expand Up @@ -2011,6 +2016,11 @@ struct Unpacker
return this;
}

/// ditto
ref Unpacker unpack(T)(ref T array) if (isArray!T && is(Unqual!T == void[0]))
{
return this;
}

/// ditto
ref Unpacker unpack(T)(ref T array) if (isAssociativeArray!T)
Expand Down Expand Up @@ -2528,7 +2538,7 @@ unittest

void toMsgpack(P)(ref P p) const { p.packArray(num); }
void fromMsgpack(ref Unpacker u)
{
{
assert(u.beginArray() == 1);
u.unpack(num);
}
Expand Down Expand Up @@ -2721,7 +2731,7 @@ struct Value
}


Type type; /// represents value type
Type type; /// represents value type
Via via; /// represents real value


Expand Down Expand Up @@ -2870,7 +2880,6 @@ struct Value
return cast(T)as!(OriginalType!T);
}


/// ditto
@property @trusted
T as(T)() if (isArray!T)
Expand Down Expand Up @@ -2898,6 +2907,12 @@ struct Value
}
}

/// ditto
@property @trusted
T as(T)() if (is(Unqual!T == void[0]))
{
return [];
}

/// ditto
@property @trusted
Expand Down Expand Up @@ -3323,11 +3338,19 @@ unittest
assert(tuple.field[1] == 1u);
assert(tuple.field[2] == "Hi!");

/*
/*
* non-MessagePackable object is stopped by static assert
* static struct NonMessagePackable {}
* auto nonMessagePackable = value.as!(NonMessagePackable);
*/

// value-less hash (aka set)
void[0][int] hash;
hash[1] = [];
ubyte[] data = pack(hash);
void[0][int] hash2;
unpack(data, hash2);
assert(1 in hash2);
}


Expand Down Expand Up @@ -3470,7 +3493,7 @@ unittest
* // do stuff (obj is a Value)
* }
* }
*
*
* if (unpacker.size)
* throw new Exception("Message is too large");
* -----
Expand Down Expand Up @@ -3746,7 +3769,7 @@ struct StreamingUnpacker
case State.FLOAT:
_f temp;

temp.i = load32To!uint(buffer_[base..base + trail]);
temp.i = load32To!uint(buffer_[base..base + trail]);
callbackFloat(obj, temp.f);
goto Lpush;
case State.DOUBLE:
Expand Down