From 968cbafb4c4770f45a89708e26c5397fead4f819 Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Tue, 26 Feb 2013 23:25:38 +0100 Subject: [PATCH 1/2] Eat trailing space. --- src/msgpack.d | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/msgpack.d b/src/msgpack.d index 6bd6d14..a52547e 100644 --- a/src/msgpack.d +++ b/src/msgpack.d @@ -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. * @@ -903,7 +903,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!"] * ----- * @@ -987,7 +987,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 @@ -1020,7 +1020,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}], @@ -1059,7 +1059,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}], @@ -1150,8 +1150,8 @@ unittest } { // pointer static struct PTest - { - ubyte format; + { + ubyte format; union { @@ -1215,7 +1215,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); @@ -1346,7 +1346,7 @@ unittest class UnpackException : MessagePackException { this(string message) - { + { super(message); } } @@ -1450,7 +1450,7 @@ version (D_Ddoc) } } else -{ +{ private mixin template InternalBuffer() { private: @@ -1571,7 +1571,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; } @@ -1648,7 +1648,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. * @@ -2528,7 +2528,7 @@ unittest void toMsgpack(P)(ref P p) const { p.packArray(num); } void fromMsgpack(ref Unpacker u) - { + { assert(u.beginArray() == 1); u.unpack(num); } @@ -2721,7 +2721,7 @@ struct Value } - Type type; /// represents value type + Type type; /// represents value type Via via; /// represents real value @@ -3323,7 +3323,7 @@ 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); @@ -3470,7 +3470,7 @@ unittest * // do stuff (obj is a Value) * } * } - * + * * if (unpacker.size) * throw new Exception("Message is too large"); * ----- @@ -3746,7 +3746,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: From 2e3b3dffe6f87a0f46e256a4f59edae3355feed5 Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Tue, 26 Feb 2013 23:27:33 +0100 Subject: [PATCH 2/2] Implement void[0] serialization support. --- src/msgpack.d | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/msgpack.d b/src/msgpack.d index a52547e..a4e2e35 100644 --- a/src/msgpack.d +++ b/src/msgpack.d @@ -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; @@ -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) @@ -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; @@ -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) @@ -2870,7 +2880,6 @@ struct Value return cast(T)as!(OriginalType!T); } - /// ditto @property @trusted T as(T)() if (isArray!T) @@ -2898,6 +2907,12 @@ struct Value } } + /// ditto + @property @trusted + T as(T)() if (is(Unqual!T == void[0])) + { + return []; + } /// ditto @property @trusted @@ -3328,6 +3343,14 @@ unittest * 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); }