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>

  • The methods of Dependency are very similar to those of std::option::Option, as dependencies are optionally present.
  • However, a Dependency on a type with a DependencyDefault also implements the AsRef, Deref and Borrow traits. Event if a value has not been explicitly registered for it, the Dependency will still be able to as_ref, deref and borrow this default value.
source

pub fn new() -> Self

Creates a optional reference to the dependency of type T.

source

pub fn is_some(&self) -> bool

Returns true if the dependency is a Some value.

source

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.

source

pub fn is_none(&self) -> bool

Returns true if the dependency is a None value.

source

pub fn as_slice(&self) -> &[T]

Returns a slice of the dependency value, if any. If this is None, an empty slice is returned.

source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the dependency value, if any.

source

pub fn expect(&self, msg: &str) -> &T

Returns the dependency Some value.

§Panics

Panics if the dependency is a None with a custom panic message provided by msg.

source

pub fn unwrap(&self) -> &T

Returns the contained Some value.

§Panics

Panics if the dependency value equals None.

source

pub fn unwrap_or(&self, default: T) -> Ref<'_, T>

Returns the dependency Some value or a provided default.

source

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.

source

pub fn unwrap_or_default(&self) -> Ref<'_, T>
where T: Default,

Returns the dependency Some value or a default.

source

pub fn map<U, F>(&self, f: F) -> Option<U>
where F: FnOnce(&T) -> U,

Maps to Option<U> by applying a function to a dependency value (if Some) or returns None (if None).

source

pub fn inspect<F>(&self, f: F) -> Option<&T>
where F: FnOnce(&T),

Calls the provided closure with a reference to the dependency value (if Some).

source

pub fn map_or<U, F>(&self, default: U, f: F) -> U
where F: FnOnce(&T) -> U,

Returns the provided default result (if None), or applies a function to the dependency value (if Some).

source

pub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(&T) -> U,

Computes a default function result (if None), or applies a different function to the dependency value (if Some).

source

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.

source

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.

source

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


  1. Once the Try trait is stabilized it will be implemented for Dependency

source

pub fn and<U>(&self, rhs: Option<U>) -> Option<U>

Returns None if the dependency is None, otherwise returns rhs.

source

pub fn and_then<U, F: FnOnce(&T) -> Option<U>>(&self, f: F) -> Option<U>

Returns None if the dependency is None, otherwise calls f with the dependency value and returns the result.

source

pub fn filter<P>(&self, predicate: P) -> Option<&T>
where P: FnOnce(&T) -> bool,

Returns None if the dependency is None, otherwise calls predicate with the dependency value and returns:

source

pub fn or(&self, rhs: Option<T>) -> Option<Ref<'_, T>>

Returns the dependency if it is Some, otherwise returns rhs.

source

pub fn or_else<F>(&self, f: F) -> Option<Ref<'_, T>>
where F: FnOnce() -> Option<T>,

Returns the dependency if it is Some, otherwise calls f and returns the result.

source

pub fn xor(&self, rhs: Option<T>) -> Option<Ref<'_, T>>

Returns Some if only one of

  • the dependency, or
  • rhs

is Some, otherwise returns None.

source

pub fn copied(&self) -> Option<T>
where T: Copy,

Maps the dependency to an Option<T> by copying the contents of the option.

source

pub fn cloned(&self) -> Option<T>
where T: Clone,

Maps the dependency to an Option<T> by cloning the contents of the option.

source§

impl<T: DependencyDefault> Dependency<T>

source

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.

Trait Implementations§

source§

impl<T: DependencyDefault> AsRef<T> for Dependency<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: DependencyDefault> Borrow<T> for Dependency<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> Default for Dependency<T>

source§

fn default() -> Self

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

impl<T: DependencyDefault> Deref for Dependency<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> !Freeze for Dependency<T>

§

impl<T> !RefUnwindSafe for Dependency<T>

§

impl<T> !Send for Dependency<T>

§

impl<T> !Sync for Dependency<T>

§

impl<T> Unpin for Dependency<T>

§

impl<T> UnwindSafe for Dependency<T>
where T: RefUnwindSafe,

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.