Skip to main content

FunctionHandle

Struct FunctionHandle 

Source
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 borrowed InjectionContext
  • O - 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>

Source

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);
Source

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();
Source

pub fn has_override(&self) -> bool

Checks if an override exists for this function.

§Returns

true if an override is set, false otherwise.

§Examples
assert!(!ctx.dependency(create_database).has_override());
ctx.dependency(create_database).override_with(mock_database);
assert!(ctx.dependency(create_database).has_override());
Source

pub fn get_override(&self) -> Option<O>

Gets the current override value for this function, if any.

§Returns

Some(value) if an override is set, None otherwise.

§Examples
ctx.dependency(create_database).override_with(mock_database.clone());
let value = ctx.dependency(create_database).get_override();
assert_eq!(value, Some(mock_database));
Source

pub fn func_ptr(&self) -> usize

Returns the function pointer address for this handle.

This is primarily useful for debugging or advanced use cases.

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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more