pub struct FunctionHandle<'a, O> { /* private fields */ }Expand description
A handle for managing function-based dependency overrides.
FunctionHandle provides a fluent API for setting overrides on specific
injectable functions. This is useful in tests where you want to mock
specific dependency factories while leaving others unaffected.
§Type Parameters
'a- Lifetime of the borrowedInjectionContextO- The output type of the function (the dependency type)
§Examples
use reinhardt_di::{InjectionContext, SingletonScope};
use std::sync::Arc;
let singleton = Arc::new(SingletonScope::new());
let ctx = InjectionContext::builder(singleton).build();
// Set override using fluent API
ctx.dependency(create_database).override_with(Database::mock());Implementations§
Source§impl<'a, O: Clone + Send + Sync + 'static> FunctionHandle<'a, O>
impl<'a, O: Clone + Send + Sync + 'static> FunctionHandle<'a, O>
Sourcepub fn override_with(&self, value: O) -> &Self
pub fn override_with(&self, value: O) -> &Self
Sets an override value for this function.
When the injectable function is invoked, this override value will be returned instead of calling the actual function implementation.
§Arguments
value- The value to return when this function is called
§Returns
A reference to self for method chaining.
§Examples
ctx.dependency(create_database)
.override_with(mock_database);Sourcepub fn clear_override(&self) -> &Self
pub fn clear_override(&self) -> &Self
Clears the override for this function.
After calling this method, the actual function implementation will be used when resolving this dependency.
§Returns
A reference to self for method chaining.
§Examples
// Set override
ctx.dependency(create_database).override_with(mock_database);
// Later, clear it
ctx.dependency(create_database).clear_override();Sourcepub fn has_override(&self) -> bool
pub fn has_override(&self) -> bool
Sourcepub fn get_override(&self) -> Option<O>
pub fn get_override(&self) -> Option<O>
Auto Trait Implementations§
impl<'a, O> Freeze for FunctionHandle<'a, O>
impl<'a, O> RefUnwindSafe for FunctionHandle<'a, O>where
O: RefUnwindSafe,
impl<'a, O> Send for FunctionHandle<'a, O>where
O: Send,
impl<'a, O> Sync for FunctionHandle<'a, O>where
O: Sync,
impl<'a, O> Unpin for FunctionHandle<'a, O>where
O: Unpin,
impl<'a, O> UnsafeUnpin for FunctionHandle<'a, O>
impl<'a, O> UnwindSafe for FunctionHandle<'a, O>where
O: 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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
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 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>
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