pub enum Either<A, B> {
Left(A),
Right(B),
}
Expand description
(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
sourceimpl<'de: 'b, 'b, A, B> Deserialize<'de> for Either<A, B>where
A: Magic,
B: Deserialize<'b>,
impl<'de: 'b, 'b, A, B> Deserialize<'de> for Either<A, B>where
A: Magic,
B: Deserialize<'b>,
sourcefn deserialize<D>(de: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(de: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<A: Ord, B: Ord> Ord for Either<A, B>
impl<A: Ord, B: Ord> Ord for Either<A, B>
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<A: PartialEq, B: PartialEq> PartialEq<Either<A, B>> for Either<A, B>
impl<A: PartialEq, B: PartialEq> PartialEq<Either<A, B>> for Either<A, B>
sourceimpl<A: PartialOrd, B: PartialOrd> PartialOrd<Either<A, B>> for Either<A, B>
impl<A: PartialOrd, B: PartialOrd> PartialOrd<Either<A, B>> for Either<A, B>
sourcefn partial_cmp(&self, other: &Either<A, B>) -> Option<Ordering>
fn partial_cmp(&self, other: &Either<A, B>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<A: Eq, B: Eq> Eq for Either<A, B>
impl<A, B> StructuralEq for Either<A, B>
impl<A, B> StructuralPartialEq for Either<A, B>
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.