[−][src]Enum figment::value::magic::Either
This is supported on crate feature
magic
only.(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)] 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()));
Variants
This is supported on crate feature
magic
only.The "left" variant.
This is supported on crate feature
magic
only.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]
A: Magic,
B: Deserialize<'b>,
fn deserialize<D>(de: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
impl<A: Eq, B: Eq> Eq for Either<A, B>
[src]
impl<A: Hash, B: Hash> Hash for Either<A, B>
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<A: Ord, B: Ord> Ord for Either<A, B>
[src]
fn cmp(&self, other: &Either<A, B>) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[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]
fn partial_cmp(&self, other: &Either<A, B>) -> Option<Ordering>
[src]
fn lt(&self, other: &Either<A, B>) -> bool
[src]
fn le(&self, other: &Either<A, B>) -> bool
[src]
fn gt(&self, other: &Either<A, B>) -> bool
[src]
fn ge(&self, other: &Either<A, B>) -> bool
[src]
impl<A, B> Serialize for Either<A, B> where
A: Serialize,
B: Serialize,
[src]
A: Serialize,
B: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
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,
A: RefUnwindSafe,
B: RefUnwindSafe,
impl<A, B> Send for Either<A, B> where
A: Send,
B: Send,
A: Send,
B: Send,
impl<A, B> Sync for Either<A, B> where
A: Sync,
B: Sync,
A: Sync,
B: Sync,
impl<A, B> Unpin for Either<A, B> where
A: Unpin,
B: Unpin,
A: Unpin,
B: Unpin,
impl<A, B> UnwindSafe for Either<A, B> where
A: UnwindSafe,
B: UnwindSafe,
A: UnwindSafe,
B: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,