Skip to content

Commit

Permalink
refactor: newtype variant without array
Browse files Browse the repository at this point in the history
  • Loading branch information
3Hren committed Sep 18, 2017
1 parent 04864c9 commit 58320b8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
3 changes: 1 addition & 2 deletions rmp-serde/src/decode.rs
Expand Up @@ -340,7 +340,6 @@ impl<'de, 'a, R: ReadSlice<'de>> serde::Deserializer<'de> for &'a mut Deserializ
self.read_bytes(len, visitor)
}
Marker::Reserved => Err(Error::TypeMismatch(Marker::Reserved)),
// TODO: Make something with exts.
marker => Err(Error::TypeMismatch(marker)),
}
}
Expand Down Expand Up @@ -496,7 +495,7 @@ impl<'de, 'a, R: ReadSlice<'de>> de::VariantAccess<'de> for VariantAccess<'a, R>
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
where T: DeserializeSeed<'de>
{
read_array_len(&mut self.de.rd)?;
// read_array_len(&mut self.de.rd)?;
seed.deserialize(self.de)
}

Expand Down
4 changes: 3 additions & 1 deletion rmp-serde/src/encode.rs
Expand Up @@ -425,7 +425,9 @@ where
}

fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(self, name: &'static str, variant_index: u32, variant: &'static str, value: &T) -> Result<Self::Ok, Self::Error> {
self.serialize_tuple_variant(name, variant_index, variant, 1)?;
// self.serialize_tuple_variant(name, variant_index, variant, 1)?;
encode::write_array_len(&mut self.wr, 2)?;
self.serialize_u32(variant_index)?;
value.serialize(self)
}

Expand Down
22 changes: 10 additions & 12 deletions rmp-serde/tests/decode_derive.rs
Expand Up @@ -122,8 +122,8 @@ fn pass_unit_variant() {

#[test]
fn pass_tuple_enum_with_arg() {
// The encoded byte-array is: [1, [42]].
let buf = [0x92, 0x01, 0x91, 0x2a];
// The encoded byte-array is: [1, 42].
let buf = [0x92, 0x01, 0x2a];
let cur = Cursor::new(&buf[..]);

#[derive(Debug, PartialEq, Deserialize)]
Expand All @@ -136,7 +136,7 @@ fn pass_tuple_enum_with_arg() {
let actual: Enum = Deserialize::deserialize(&mut de).unwrap();

assert_eq!(Enum::B(42), actual);
assert_eq!(4, de.get_ref().position())
assert_eq!(3, de.get_ref().position())
}

#[test]
Expand Down Expand Up @@ -221,23 +221,22 @@ fn pass_struct_enum_with_arg() {

#[test]
fn pass_newtype_variant() {
// The encoded bytearray is: [0, ['le message']].
let buf = [0x92, 0x0, 0x91, 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
// The encoded bytearray is: [0, 'le message'].
let buf = [0x92, 0x0, 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65];
let cur = Cursor::new(&buf[..]);

#[derive(Debug, PartialEq, Deserialize)]
struct Nested(String);
struct Newtype(String);

#[derive(Debug, PartialEq, Deserialize)]
enum Enum {
A(Nested),
B,
A(Newtype),
}

let mut de = Deserializer::new(cur);
let actual: Enum = Deserialize::deserialize(&mut de).unwrap();

assert_eq!(Enum::A(Nested("le message".into())), actual);
assert_eq!(Enum::A(Newtype("le message".into())), actual);
assert_eq!(buf.len() as u64, de.get_ref().position())
}

Expand Down Expand Up @@ -371,14 +370,13 @@ fn pass_internally_tagged_enum_struct() {

#[test]
fn pass_enum_with_one_arg() {
// The encoded bytearray is: [0, [[1, 2]]].
let buf = [0x92, 0x0, 0x91, 0x92, 0x01, 0x02];
// The encoded bytearray is: [0, [1, 2]].
let buf = [0x92, 0x0, 0x92, 0x01, 0x02];
let cur = Cursor::new(&buf[..]);

#[derive(Debug, PartialEq, Deserialize)]
enum Enum {
V1(Vec<u32>),
V2,
}

let mut de = Deserializer::new(cur);
Expand Down
6 changes: 2 additions & 4 deletions rmp-serde/tests/encode_derive.rs
Expand Up @@ -52,16 +52,14 @@ fn pass_newtype_struct() {
fn pass_newtype_variant() {
#[derive(Serialize)]
enum Enum {
V1,
V2(u64),
}

let mut buf = Vec::new();
Enum::V1.serialize(&mut Serializer::new(&mut buf)).unwrap();
Enum::V2(42).serialize(&mut Serializer::new(&mut buf)).unwrap();

// Expect: [0, []] [1, [42]].
assert_eq!(vec![0x92, 0x00, 0x90, 0x92, 0x01, 0x91, 0x2a], buf);
// Expect: [0, 42].
assert_eq!(vec![0x92, 0x00, 0x2a], buf);
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions rmp-serde/tests/round.rs
Expand Up @@ -66,7 +66,7 @@ fn round_trip_option_cow() {
}

#[test]
fn round_enum_with_nested_struct() {
fn round_enum_with_newtype_struct() {
use serde::Serialize;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
Expand All @@ -75,7 +75,6 @@ fn round_enum_with_nested_struct() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum Enum {
A(Newtype),
B,
}

let expected = Enum::A(Newtype("le message".into()));
Expand Down

0 comments on commit 58320b8

Please sign in to comment.