mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-11 07:11:30 +11:00
Add support for Some
and None
This commit is contained in:
parent
8abe8b7ebf
commit
794fa10153
|
@ -23,7 +23,7 @@ impl<'de: 'w, 'w, R: Read + ?Sized> de::Deserializer<'de> for PayloadDeserialize
|
|||
|
||||
forward_to_deserialize_any! {
|
||||
i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
bytes byte_buf unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map enum identifier ignored_any
|
||||
}
|
||||
|
||||
|
@ -101,6 +101,13 @@ impl<'de: 'w, 'w, R: Read + ?Sized> de::Deserializer<'de> for PayloadDeserialize
|
|||
}
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
|
|
|
@ -174,14 +174,14 @@ impl<'a, W: Write + ?Sized> Serializer for &'a mut PayloadSerializer<'_, '_, W>
|
|||
}
|
||||
|
||||
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
||||
unsupported!("None")
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
unsupported!("Some")
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use std::marker::PhantomData;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -198,3 +199,25 @@ fn value_from_json() {
|
|||
|
||||
assert_eq!(struct_, struct_de);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn some_none_round_trip() {
|
||||
let mut buf = Vec::new();
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
|
||||
struct Options {
|
||||
some: Option<i32>,
|
||||
none: Option<i32>,
|
||||
}
|
||||
|
||||
let opts = Options {
|
||||
some: Some(123),
|
||||
none: None,
|
||||
};
|
||||
|
||||
to_writer(&mut buf, &opts).unwrap();
|
||||
|
||||
let opts_de = from_reader(&mut buf.as_slice()).unwrap();
|
||||
|
||||
assert_eq!(opts, opts_de);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue