Struct composable::dependencies::Dependency
source · pub struct Dependency<T: 'static> { /* private fields */ }Expand description
A wrapper type for accessing dependencies
Implementations§
source§impl<T> Dependency<T>
impl<T> Dependency<T>
- The methods of
Dependencyare very similar to those ofstd::option::Option, as dependencies are optionally present. - However, a
Dependencyon a type with aDependencyDefaultalso implements theAsRef,DerefandBorrowtraits. Event if a value has not been explicitly registered for it, theDependencywill still be able toas_ref,derefandborrowthis default value.
sourcepub fn is_some_and(&self, f: impl FnOnce(&T) -> bool) -> bool
pub fn is_some_and(&self, f: impl FnOnce(&T) -> bool) -> bool
Returns true if the dependency is a Some and the value inside of it matches a predicate.
sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice of the dependency value, if any. If this is None, an empty slice is returned.
sourcepub fn unwrap_or(&self, default: T) -> Ref<'_, T>
pub fn unwrap_or(&self, default: T) -> Ref<'_, T>
Returns the dependency Some value or a provided default.
sourcepub fn unwrap_or_else<F>(&self, f: F) -> Ref<'_, T>where
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(&self, f: F) -> Ref<'_, T>where
F: FnOnce() -> T,
Returns the dependency Some value or computes it from a closure.
sourcepub fn unwrap_or_default(&self) -> Ref<'_, T>where
T: Default,
pub fn unwrap_or_default(&self) -> Ref<'_, T>where
T: Default,
Returns the dependency Some value or a default.
sourcepub fn inspect<F>(&self, f: F) -> Option<&T>
pub fn inspect<F>(&self, f: F) -> Option<&T>
Calls the provided closure with a reference to the dependency value (if Some).
sourcepub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
pub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
sourcepub fn ok_or<E>(&self, err: E) -> Result<&T, E>
pub fn ok_or<E>(&self, err: E) -> Result<&T, E>
Transforms into a Result<&T, E>, mapping Some to
Ok and None to Err.
sourcepub fn ok_or_else<E, F>(&self, err: F) -> Result<&T, E>where
F: FnOnce() -> E,
pub fn ok_or_else<E, F>(&self, err: F) -> Result<&T, E>where
F: FnOnce() -> E,
Transforms into a Result<&T, E>, mapping Some to
Ok and None to Err.
sourcepub fn as_deref(&self) -> Option<&T>
pub fn as_deref(&self) -> Option<&T>
Converts into a Option<&T>.
§Note
This is the preferred method for producing an Option to use with the
question mark operator.1
sourcepub fn or(&self, rhs: Option<T>) -> Option<Ref<'_, T>>
pub fn or(&self, rhs: Option<T>) -> Option<Ref<'_, T>>
Returns the dependency if it is Some, otherwise returns rhs.
sourcepub fn or_else<F>(&self, f: F) -> Option<Ref<'_, T>>
pub fn or_else<F>(&self, f: F) -> Option<Ref<'_, T>>
Returns the dependency if it is Some, otherwise calls f and returns the result.
source§impl<T: DependencyDefault> Dependency<T>
impl<T: DependencyDefault> Dependency<T>
sourcepub fn static_ref() -> &'static T
pub fn static_ref() -> &'static T
§SAFETY
A DependencyDefault, once fetched, will last for the life of the process.
Holding this reference is not advised as it will not reflect further overrides of this dependency.