Enum ExplicitOrAuto
pub(crate) enum ExplicitOrAuto<T>where
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§
§impl<T> ExplicitOrAuto<T>where
T: NotAutoValue,
impl<T> ExplicitOrAuto<T>where
T: NotAutoValue,
pub 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());
pub 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.
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExplicitOrAuto<U>where
U: NotAutoValue,
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExplicitOrAuto<U>where
U: NotAutoValue,
Maps an ExplicitOrAuto<T>
to an ExplicitOrAuto<U>
by applying a function to a contained
value.
Trait Implementations§
§impl<T> Clone for ExplicitOrAuto<T>where
T: Clone + NotAutoValue,
impl<T> Clone for ExplicitOrAuto<T>where
T: Clone + NotAutoValue,
§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 more§impl<T> Debug for ExplicitOrAuto<T>where
T: Debug + NotAutoValue,
impl<T> Debug for ExplicitOrAuto<T>where
T: Debug + NotAutoValue,
§impl<T> Default for ExplicitOrAuto<T>where
T: NotAutoValue,
impl<T> Default for ExplicitOrAuto<T>where
T: NotAutoValue,
§fn default() -> ExplicitOrAuto<T>
fn default() -> ExplicitOrAuto<T>
§impl<'de, T> Deserialize<'de> for ExplicitOrAuto<T>where
T: NotAutoValue + Deserialize<'de>,
impl<'de, T> Deserialize<'de> for ExplicitOrAuto<T>where
T: NotAutoValue + Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ExplicitOrAuto<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ExplicitOrAuto<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<T> From<T> for ExplicitOrAuto<T>where
T: NotAutoValue,
impl<T> From<T> for ExplicitOrAuto<T>where
T: NotAutoValue,
§fn from(x: T) -> ExplicitOrAuto<T>
fn from(x: T) -> ExplicitOrAuto<T>
§impl<T> Hash for ExplicitOrAuto<T>where
T: Hash + NotAutoValue,
impl<T> Hash for ExplicitOrAuto<T>where
T: Hash + NotAutoValue,
§impl<T> Ord for ExplicitOrAuto<T>where
T: Ord + NotAutoValue,
impl<T> Ord for ExplicitOrAuto<T>where
T: Ord + NotAutoValue,
§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,
§impl<T> PartialEq for ExplicitOrAuto<T>where
T: PartialEq + NotAutoValue,
impl<T> PartialEq for ExplicitOrAuto<T>where
T: PartialEq + NotAutoValue,
§impl<T> PartialOrd for ExplicitOrAuto<T>where
T: PartialOrd + NotAutoValue,
impl<T> PartialOrd for ExplicitOrAuto<T>where
T: PartialOrd + NotAutoValue,
§impl<T> Serialize for ExplicitOrAuto<T>where
T: NotAutoValue + Serialize,
impl<T> Serialize for ExplicitOrAuto<T>where
T: NotAutoValue + Serialize,
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl<T> Copy for ExplicitOrAuto<T>where
T: Copy + NotAutoValue,
impl<T> Eq for ExplicitOrAuto<T>where
T: Eq + NotAutoValue,
impl<T> StructuralPartialEq for ExplicitOrAuto<T>where
T: NotAutoValue,
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> ⓘ
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