[][src]Struct rustic_hal::resource::OneOrMany

pub struct OneOrMany<T> { /* fields omitted */ }

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]

create a new empty object

Force to be serialized as array, even if only one element

retrieve the length of the wrapped vector

Retrieves a single element if possible.

Returns an immutable reference to the contained links

Add an element to the wrapped vector.

Adds an element to the vector in a chainable way

Trait Implementations

impl<T: Clone> Clone for OneOrMany<T>
[src]

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for OneOrMany<T>
[src]

impl<T> Serialize for OneOrMany<T> where
    T: Serialize + Clone
[src]

impl<'de, T> Deserialize<'de> for OneOrMany<T> where
    T: Deserialize<'d> + Clone
[src]

Auto Trait Implementations

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

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

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<T> From for T
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

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