pub enum ExplicitOrAuto<T: NotAutoValue> {
Auto,
Explicit(T),
}
Expand description
A serializable value, or auto.
Used for implementing configuration options that can be explicitly initialized
with a placeholder for their “default” value using the
Auto
variant.
Unlike #[serde(default)] field: T
or #[serde(default)] field: Option<T>
,
fields of this type can be present in the serialized configuration
without being assigned a concrete value.
Important: the underlying type must implement NotAutoValue
.
This trait should be implemented using the impl_not_auto_value
,
and only for types that do not serialize to the same value as the
Auto
variant.
§Example
In the following serialized TOML config
foo = "auto"
foo
is set to Auto
, which indicates the
implementation should use a default (but not necessarily Default::default
)
value for the foo
option.
For example, f field foo
defaults to 13
if feature bar
is enabled,
and 9000
otherwise, a configuration with foo
set to "auto"
will
behave in the “default” way regardless of which features are enabled.
struct Foo(usize);
impl Default for Foo {
fn default() -> Foo {
if cfg!(feature = "bar") {
Foo(13)
} else {
Foo(9000)
}
}
}
impl Foo {
fn from_explicit_or_auto(foo: ExplicitOrAuto<Foo>) -> Self {
match foo {
// If Auto, choose a sensible default for foo
ExplicitOrAuto::Auto => Default::default(),
ExplicitOrAuto::Foo(foo) => foo,
}
}
}
struct Config {
foo: ExplicitOrAuto<Foo>,
}
Variants§
Implementations§
Source§impl<T: NotAutoValue> ExplicitOrAuto<T>
impl<T: NotAutoValue> ExplicitOrAuto<T>
Sourcepub fn into_value(self) -> Option<T>
pub fn into_value(self) -> Option<T>
Returns the explicitly set value, or None
.
use tor_config::ExplicitOrAuto;
fn calculate_default() -> usize { //...
let explicit_or_auto: ExplicitOrAuto<usize> = // ...
let _: usize = explicit_or_auto.into_value().unwrap_or_else(|| calculate_default());
Sourcepub fn as_value(&self) -> Option<&T>
pub fn as_value(&self) -> Option<&T>
Returns a reference to the explicitly set value, or None
.
Like ExplicitOrAuto::into_value
, except it returns a reference to the inner type.
Trait Implementations§
Source§impl<T: Clone + NotAutoValue> Clone for ExplicitOrAuto<T>
impl<T: Clone + NotAutoValue> Clone for ExplicitOrAuto<T>
Source§fn clone(&self) -> ExplicitOrAuto<T>
fn clone(&self) -> ExplicitOrAuto<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug + NotAutoValue> Debug for ExplicitOrAuto<T>
impl<T: Debug + NotAutoValue> Debug for ExplicitOrAuto<T>
Source§impl<T: NotAutoValue> Default for ExplicitOrAuto<T>
impl<T: NotAutoValue> Default for ExplicitOrAuto<T>
Source§fn default() -> ExplicitOrAuto<T>
fn default() -> ExplicitOrAuto<T>
Source§impl<'de, T> Deserialize<'de> for ExplicitOrAuto<T>where
T: Deserialize<'de> + NotAutoValue,
impl<'de, T> Deserialize<'de> for ExplicitOrAuto<T>where
T: Deserialize<'de> + NotAutoValue,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T: Hash + NotAutoValue> Hash for ExplicitOrAuto<T>
impl<T: Hash + NotAutoValue> Hash for ExplicitOrAuto<T>
Source§impl<T: Ord + NotAutoValue> Ord for ExplicitOrAuto<T>
impl<T: Ord + NotAutoValue> Ord for ExplicitOrAuto<T>
Source§fn cmp(&self, other: &ExplicitOrAuto<T>) -> Ordering
fn cmp(&self, other: &ExplicitOrAuto<T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq + NotAutoValue> PartialEq for ExplicitOrAuto<T>
impl<T: PartialEq + NotAutoValue> PartialEq for ExplicitOrAuto<T>
Source§impl<T: PartialOrd + NotAutoValue> PartialOrd for ExplicitOrAuto<T>
impl<T: PartialOrd + NotAutoValue> PartialOrd for ExplicitOrAuto<T>
Source§impl<T> Serialize for ExplicitOrAuto<T>where
T: Serialize + NotAutoValue,
impl<T> Serialize for ExplicitOrAuto<T>where
T: Serialize + NotAutoValue,
impl<T: Copy + NotAutoValue> Copy for ExplicitOrAuto<T>
impl<T: Eq + NotAutoValue> Eq for ExplicitOrAuto<T>
impl<T: NotAutoValue> StructuralPartialEq for ExplicitOrAuto<T>
Auto Trait Implementations§
impl<T> Freeze for ExplicitOrAuto<T>where
T: Freeze,
impl<T> RefUnwindSafe for ExplicitOrAuto<T>where
T: RefUnwindSafe,
impl<T> Send for ExplicitOrAuto<T>where
T: Send,
impl<T> Sync for ExplicitOrAuto<T>where
T: Sync,
impl<T> Unpin for ExplicitOrAuto<T>where
T: Unpin,
impl<T> UnwindSafe for ExplicitOrAuto<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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