pub struct PickFirst<A>(/* private fields */);Expand description
Tries adapters in order until one of them accepts the value.
The adapters are given as a tuple of two to four adapters, _ stands
for Same. When deserializing, the value is delivered to the
adapters in order and the first one that accepts it wins. Values other
than atoms are recorded and replayed for every adapter (like for
untagged enums, see Recording), which keeps
their source locations. If no adapter accepts the value, the error of
the first adapter is reported. Missing values are handled by the first
adapter. Serialization uses the first adapter.
The most common use is to accept a value in more than one form, like numbers that are also sent as strings:
use deser::adapters::{DisplayFromStr, PickFirst};
use deser::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Item {
#[deser(as = PickFirst<(_, DisplayFromStr)>)]
id: u64,
}
let a: Item = deser_json::from_str(r#"{"id": 42}"#).unwrap();
let b: Item = deser_json::from_str(r#"{"id": "42"}"#).unwrap();
assert_eq!(a, b);
assert_eq!(deser_json::to_string(&b).unwrap(), r#"{"id":42}"#);Trait Implementations§
Source§impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2)>
impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2)>
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>, A3: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2, A3)>
impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>, A3: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2, A3)>
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>, A3: Deserialize<'de, T>, A4: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2, A3, A4)>
impl<'de, T: Send, A1: Deserialize<'de, T>, A2: Deserialize<'de, T>, A3: Deserialize<'de, T>, A4: Deserialize<'de, T>> Deserialize<'de, T> for PickFirst<(A1, A2, A3, A4)>
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<T: ?Sized, A1: Serialize<T>, A2> Serialize<T> for PickFirst<(A1, A2)>
impl<T: ?Sized, A1: Serialize<T>, A2> Serialize<T> for PickFirst<(A1, A2)>
Source§fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Source§impl<T: ?Sized, A1: Serialize<T>, A2, A3> Serialize<T> for PickFirst<(A1, A2, A3)>
impl<T: ?Sized, A1: Serialize<T>, A2, A3> Serialize<T> for PickFirst<(A1, A2, A3)>
Source§fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Source§impl<T: ?Sized, A1: Serialize<T>, A2, A3, A4> Serialize<T> for PickFirst<(A1, A2, A3, A4)>
impl<T: ?Sized, A1: Serialize<T>, A2, A3, A4> Serialize<T> for PickFirst<(A1, A2, A3, A4)>
Source§fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Auto Trait Implementations§
impl<A> Freeze for PickFirst<A>
impl<A> RefUnwindSafe for PickFirst<A>
impl<A> Send for PickFirst<A>
impl<A> Sync for PickFirst<A>
impl<A> Unpin for PickFirst<A>
impl<A> UnsafeUnpin for PickFirst<A>
impl<A> UnwindSafe for PickFirst<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more