[−][src]Struct rustic_hal::resource::OneOrMany
A Simple wrapper around a vector to allow custom serialization when only 1 element is contained.
example
In the example below, the vector serializes to json as an object if it contains only one value, but as an array if more than one.
use rustic_hal::resource::OneOrMany; use rustic_hal::HalLink; use serde_json::to_string; let mut v = OneOrMany::new(); v.push(&HalLink::new("http://test.com")); assert_eq!(to_string(&v).unwrap(), r#"{"href":"http://test.com"}"#); v.push(&HalLink::new("http://test2.com")); assert_eq!(to_string(&v).unwrap(), r#"[{"href":"http://test.com"},{"href":"http://test2.com"}]"#);
Methods
impl<T> OneOrMany<T> where
T: Sized + Clone,
[src]
impl<T> OneOrMany<T> where
T: Sized + Clone,
pub fn new() -> OneOrMany<T>
[src]
pub fn new() -> OneOrMany<T>
create a new empty object
pub fn force_many(self) -> Self
[src]
pub fn force_many(self) -> Self
Force to be serialized as array, even if only one element
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
retrieve the length of the wrapped vector
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
pub fn single(&self) -> Option<&T>
[src]
pub fn single(&self) -> Option<&T>
Retrieves a single element if possible.
pub fn many(&self) -> &Vec<T>
[src]
pub fn many(&self) -> &Vec<T>
Returns an immutable reference to the contained links
pub fn push(&mut self, newval: &T)
[src]
pub fn push(&mut self, newval: &T)
Add an element to the wrapped vector.
pub fn with(self, newval: &T) -> Self
[src]
pub fn with(self, newval: &T) -> Self
Adds an element to the vector in a chainable way
Trait Implementations
impl<T: Clone> Clone for OneOrMany<T>
[src]
impl<T: Clone> Clone for OneOrMany<T>
fn clone(&self) -> OneOrMany<T>
[src]
fn clone(&self) -> OneOrMany<T>
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0
[src]Performs copy-assignment from source
. Read more
impl<T: Debug> Debug for OneOrMany<T>
[src]
impl<T: Debug> Debug for OneOrMany<T>
impl<T> Serialize for OneOrMany<T> where
T: Serialize + Clone,
[src]
impl<T> Serialize for OneOrMany<T> where
T: Serialize + Clone,
impl<'de, T> Deserialize<'de> for OneOrMany<T> where
T: Deserialize<'d> + Clone,
[src]
impl<'de, T> Deserialize<'de> for OneOrMany<T> where
T: Deserialize<'d> + Clone,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,