Skip to main content

OneOrMany

Struct OneOrMany 

Source
pub struct OneOrMany<A = Same, F = PreferOne>(/* private fields */);
Expand description

A collection that also accepts a single element.

When deserializing, a sequence is the collection and every other value (an atom or a map) is a single element, which becomes a collection of one. The elements are deserialized with the adapter A (by default Same). This is for data that writes a list with one element as the element itself ("tags": "a" and "tags": ["a", "b"]). As a sequence is always the collection, elements cannot be sequences themselves.

When serializing, F decides the form: PreferOne (the default) writes a collection with exactly one element as that element and PreferMany always writes a sequence.

Supported are Vec<T>, VecDeque<T>, BTreeSet<T> and HashSet<T>.

use deser::adapters::{OneOrMany, PreferMany};
use deser::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Rule {
    #[deser(as = OneOrMany)]
    hosts: Vec<String>,
    #[deser(as = OneOrMany<_, PreferMany>)]
    ports: Vec<u16>,
}

let rule: Rule = deser_json::from_str(r#"{"hosts": "a", "ports": 80}"#).unwrap();
assert_eq!(rule.hosts, ["a"]);
assert_eq!(rule.ports, [80]);
assert_eq!(
    deser_json::to_string(&rule).unwrap(),
    r#"{"hosts":"a","ports":[80]}"#
);

In formats where keys can be given more than once (like query strings, see ContainerShape::set_multimap) the values of all occurrences of the key are collected, as without the adapter.

Trait Implementations§

Source§

impl<'de, T: Ord + Send, A: Deserialize<'de, T>, F: OneOrManyForm> Deserialize<'de, BTreeSet<T>> for OneOrMany<A, F>

Source§

fn deserialize_into<'out>( out: &'out mut Option<BTreeSet<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>

Returns what the value expects, for error messages. Read more
Source§

fn initial_value() -> Option<BTreeSet<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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

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>

Creates a sink that updates an existing value. Read more
Source§

impl<'de, T: Hash + Eq + Send, H: BuildHasher + Default + Send, A: Deserialize<'de, T>, F: OneOrManyForm> Deserialize<'de, HashSet<T, H>> for OneOrMany<A, F>

Source§

fn deserialize_into<'out>( out: &'out mut Option<HashSet<T, H>>, 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>

Returns what the value expects, for error messages. Read more
Source§

fn initial_value() -> Option<HashSet<T, H>>

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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

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>

Creates a sink that updates an existing value. Read more
Source§

impl<'de, T: Send, A: Deserialize<'de, T>, F: OneOrManyForm> Deserialize<'de, Vec<T>> for OneOrMany<A, F>

Source§

fn deserialize_into<'out>( out: &'out mut Option<Vec<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>

Returns what the value expects, for error messages. Read more
Source§

fn initial_value() -> Option<Vec<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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

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>

Creates a sink that updates an existing value. Read more
Source§

impl<'de, T: Send, A: Deserialize<'de, T>, F: OneOrManyForm> Deserialize<'de, VecDeque<T>> for OneOrMany<A, F>

Source§

fn deserialize_into<'out>( out: &'out mut Option<VecDeque<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>

Returns what the value expects, for error messages. Read more
Source§

fn initial_value() -> Option<VecDeque<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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

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>

Creates a sink that updates an existing value. Read more
Source§

impl<T: Sync, A: Serialize<T>, F: OneOrManyForm> Serialize<BTreeSet<T>> for OneOrMany<A, F>

Source§

fn serialize<'a>( value: &'a BTreeSet<T>, _state: &mut State, ) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn describe(value: &BTreeSet<T>, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn container_shape(value: &T) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

impl<T: Sync, H: BuildHasher + Sync, A: Serialize<T>, F: OneOrManyForm> Serialize<HashSet<T, H>> for OneOrMany<A, F>

Source§

fn serialize<'a>( value: &'a HashSet<T, H>, _state: &mut State, ) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn describe(value: &HashSet<T, H>, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn container_shape(value: &T) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

impl<T: Sync, A: Serialize<T>, F: OneOrManyForm> Serialize<Vec<T>> for OneOrMany<A, F>

Source§

fn serialize<'a>( value: &'a Vec<T>, _state: &mut State, ) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn describe(value: &Vec<T>, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn container_shape(value: &T) -> ContainerShape

Returns the shape of the value if it’s a map or sequence. Read more
Source§

impl<T: Sync, A: Serialize<T>, F: OneOrManyForm> Serialize<VecDeque<T>> for OneOrMany<A, F>

Source§

fn serialize<'a>( value: &'a VecDeque<T>, _state: &mut State, ) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn describe(value: &VecDeque<T>, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

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, F> Freeze for OneOrMany<A, F>
where PhantomData<fn() -> (A, F)>: Freeze,

§

impl<A, F> RefUnwindSafe for OneOrMany<A, F>

§

impl<A, F> Send for OneOrMany<A, F>
where PhantomData<fn() -> (A, F)>: Send,

§

impl<A, F> Sync for OneOrMany<A, F>
where PhantomData<fn() -> (A, F)>: Sync,

§

impl<A, F> Unpin for OneOrMany<A, F>
where PhantomData<fn() -> (A, F)>: Unpin,

§

impl<A, F> UnsafeUnpin for OneOrMany<A, F>

§

impl<A, F> UnwindSafe for OneOrMany<A, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.