Skip to main content

OneOrMany

Struct OneOrMany 

Source
pub struct OneOrMany<T>(/* private fields */);
Expand description

A value that may serialize as either a single T or as an ordered array of T.

On the wire an empty OneOrMany is emitted as an empty array, a single entry as a bare value, and multiple entries as a JSON array.

§Examples

let one: OneOrMany<String> =
    serde_json::from_str(r#""Hello""#).unwrap();
assert_eq!(one.as_slice(), &["Hello".to_owned()]);

let many: OneOrMany<String> =
    serde_json::from_str(r#"["A", "B"]"#).unwrap();
assert_eq!(many.len(), 2);

Implementations§

Source§

impl<T> OneOrMany<T>

Source

pub const fn new() -> Self

Creates an empty OneOrMany.

Source

pub fn one(value: T) -> Self

Creates a OneOrMany containing a single value.

Source

pub const fn many(values: Vec<T>) -> Self

Creates a OneOrMany from a pre-existing Vec.

Source

pub fn as_slice(&self) -> &[T]

Returns a slice view of the contained values.

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice view of the contained values.

Source

pub fn into_vec(self) -> Vec<T>

Returns the underlying Vec, consuming self.

Source

pub fn push(&mut self, value: T)

Appends a value.

Source

pub const fn is_empty(&self) -> bool

Returns true if empty.

Source

pub const fn len(&self) -> usize

Returns the number of contained values.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the contained values.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns a mutable iterator over the contained values.

Source

pub fn first(&self) -> Option<&T>

Returns a reference to the first value, if any.

Source

pub fn last(&self) -> Option<&T>

Returns a reference to the last value, if any.

Trait Implementations§

Source§

impl<T: Clone> Clone for OneOrMany<T>

Source§

fn clone(&self) -> OneOrMany<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for OneOrMany<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for OneOrMany<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for OneOrMany<T>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Eq> Eq for OneOrMany<T>

Source§

impl<T> From<T> for OneOrMany<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for OneOrMany<T>

Source§

fn from(values: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for OneOrMany<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for OneOrMany<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoIterator for OneOrMany<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a OneOrMany<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut OneOrMany<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for OneOrMany<T>

Source§

fn eq(&self, other: &OneOrMany<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Serialize> Serialize for OneOrMany<T>

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for OneOrMany<T>

Auto Trait Implementations§

§

impl<T> Freeze for OneOrMany<T>

§

impl<T> RefUnwindSafe for OneOrMany<T>
where T: RefUnwindSafe,

§

impl<T> Send for OneOrMany<T>
where T: Send,

§

impl<T> Sync for OneOrMany<T>
where T: Sync,

§

impl<T> Unpin for OneOrMany<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for OneOrMany<T>

§

impl<T> UnwindSafe for OneOrMany<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.