Enum figment::value::magic::Either[][src]

pub enum Either<A, B> {
    Left(A),
    Right(B),
}

(De)serializes as either a magic value A or any other deserializable value B.

An Either<A, B> deserializes as either an A or B, whichever succeeds first.

The usual Either implementation or an “untagged” enum does not allow its internal values to provide hints to the deserializer. These hints are required for magic values to work. By contrast, this Either does provide the appropriate hints.

Example

use serde::{Serialize, Deserialize};
use figment::{Figment, value::magic::{Either, RelativePathBuf, Tagged}};

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Config {
    int_or_str: Either<Tagged<usize>, String>,
    path_or_bytes: Either<RelativePathBuf, Vec<u8>>,
}

fn figment<A: Serialize, B: Serialize>(a: A, b: B) -> Figment {
    Figment::from(("int_or_str", a)).merge(("path_or_bytes", b))
}

let config: Config = figment(10, "/a/b").extract().unwrap();
assert_eq!(config.int_or_str, Either::Left(10.into()));
assert_eq!(config.path_or_bytes, Either::Left("/a/b".into()));

let config: Config = figment("hi", "c/d").extract().unwrap();
assert_eq!(config.int_or_str, Either::Right("hi".into()));
assert_eq!(config.path_or_bytes, Either::Left("c/d".into()));

let config: Config = figment(123, &[1, 2, 3]).extract().unwrap();
assert_eq!(config.int_or_str, Either::Left(123.into()));
assert_eq!(config.path_or_bytes, Either::Right(vec![1, 2, 3].into()));

let config: Config = figment("boo!", &[4, 5, 6]).extract().unwrap();
assert_eq!(config.int_or_str, Either::Right("boo!".into()));
assert_eq!(config.path_or_bytes, Either::Right(vec![4, 5, 6].into()));

let config: Config = Figment::from(figment::providers::Serialized::defaults(Config {
    int_or_str: Either::Left(10.into()),
    path_or_bytes: Either::Left("a/b/c".into()),
})).extract().unwrap();

assert_eq!(config.int_or_str, Either::Left(10.into()));
assert_eq!(config.path_or_bytes, Either::Left("a/b/c".into()));

let config: Config = Figment::from(figment::providers::Serialized::defaults(Config {
    int_or_str: Either::Right("hi".into()),
    path_or_bytes: Either::Right(vec![3, 7, 13]),
})).extract().unwrap();

assert_eq!(config.int_or_str, Either::Right("hi".into()));
assert_eq!(config.path_or_bytes, Either::Right(vec![3, 7, 13]));

Variants

Left(A)

The “left” variant.

Right(B)

The “right” variant.

Trait Implementations

impl<A: Clone, B: Clone> Clone for Either<A, B>[src]

impl<A: Debug, B: Debug> Debug for Either<A, B>[src]

impl<'de: 'b, 'b, A, B> Deserialize<'de> for Either<A, B> where
    A: Magic,
    B: Deserialize<'b>, 
[src]

impl<A: Eq, B: Eq> Eq for Either<A, B>[src]

impl<A: Hash, B: Hash> Hash for Either<A, B>[src]

impl<A: Ord, B: Ord> Ord for Either<A, B>[src]

impl<A: PartialEq, B: PartialEq> PartialEq<Either<A, B>> for Either<A, B>[src]

impl<A: PartialOrd, B: PartialOrd> PartialOrd<Either<A, B>> for Either<A, B>[src]

impl<A, B> Serialize for Either<A, B> where
    A: Serialize,
    B: Serialize
[src]

impl<A, B> StructuralEq for Either<A, B>[src]

impl<A, B> StructuralPartialEq for Either<A, B>[src]

Auto Trait Implementations

impl<A, B> RefUnwindSafe for Either<A, B> where
    A: RefUnwindSafe,
    B: RefUnwindSafe

impl<A, B> Send for Either<A, B> where
    A: Send,
    B: Send

impl<A, B> Sync for Either<A, B> where
    A: Sync,
    B: Sync

impl<A, B> Unpin for Either<A, B> where
    A: Unpin,
    B: Unpin

impl<A, B> UnwindSafe for Either<A, B> where
    A: UnwindSafe,
    B: UnwindSafe

Blanket Implementations

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

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

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

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

impl<T> From<T> for T[src]

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

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

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,