Struct tor_config::Flatten

source ·
pub struct Flatten<T, U>(pub T, pub U);
Expand description

Helper for flattening deserialisation, compatible with serde_ignored

A combination of two structs T and U.

The serde representation flattens both structs into a single, larger, struct.

Furthermore, unlike plain use of #[serde(flatten)], serde_ignored will still detect fields which appear in serde input but which form part of neither T nor U.

T and U must both be Flattenable. Usually that trait should be derived with the Flattenable macro.

If it’s desired to combine more than two structs, Flatten can be nested.

§Limitations

Field name overlaps are not detected. Fields which appear in both structs will be processed as part of T during deserialization. They will be internally presented as duplicate fields during serialization, with the outcome depending on the data format implementation.

§Example

use serde::{Serialize, Deserialize};
use derive_deftly::Deftly;
use tor_config::{Flatten, derive_deftly_template_Flattenable};

#[derive(Serialize, Deserialize, Debug, Deftly, Eq, PartialEq)]
#[derive_deftly(Flattenable)]
struct A {
    a: i32,
}

#[derive(Serialize, Deserialize, Debug, Deftly, Eq, PartialEq)]
#[derive_deftly(Flattenable)]
struct B {
    b: String,
}

let combined: Flatten<A,B> = toml::from_str(r#"
    a = 42
    b = "hello"
"#).unwrap();

assert_eq!(
   combined,
   Flatten(A { a: 42 }, B { b: "hello".into() }),
);

Tuple Fields§

§0: T§1: U

Trait Implementations§

source§

impl<T: Clone, U: Clone> Clone for Flatten<T, U>

source§

fn clone(&self) -> Flatten<T, U>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, U: Debug> Debug for Flatten<T, U>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default, U: Default> Default for Flatten<T, U>

source§

fn default() -> Flatten<T, U>

Returns the “default value” for a type. Read more
source§

impl<'de, T, U> Deserialize<'de> for Flatten<T, U>
where T: Deserialize<'de> + Flattenable, U: Deserialize<'de> + Flattenable,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T, U> Flattenable for Flatten<T, U>
where T: Flattenable, U: Flattenable,

Flatten may be nested

source§

fn has_field(f: &str) -> bool

Does this type have a field named s ?
source§

impl<T: Hash, U: Hash> Hash for Flatten<T, U>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: Ord, U: Ord> Ord for Flatten<T, U>

source§

fn cmp(&self, other: &Flatten<T, U>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq, U: PartialEq> PartialEq for Flatten<T, U>

source§

fn eq(&self, other: &Flatten<T, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd, U: PartialOrd> PartialOrd for Flatten<T, U>

source§

fn partial_cmp(&self, other: &Flatten<T, U>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, U> Serialize for Flatten<T, U>
where T: Serialize, U: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Copy, U: Copy> Copy for Flatten<T, U>

source§

impl<T: Eq, U: Eq> Eq for Flatten<T, U>

source§

impl<T, U> StructuralPartialEq for Flatten<T, U>

Auto Trait Implementations§

§

impl<T, U> Freeze for Flatten<T, U>
where T: Freeze, U: Freeze,

§

impl<T, U> RefUnwindSafe for Flatten<T, U>

§

impl<T, U> Send for Flatten<T, U>
where T: Send, U: Send,

§

impl<T, U> Sync for Flatten<T, U>
where T: Sync, U: Sync,

§

impl<T, U> Unpin for Flatten<T, U>
where T: Unpin, U: Unpin,

§

impl<T, U> UnwindSafe for Flatten<T, U>
where T: UnwindSafe, U: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

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