Skip to main content

Inject

Struct Inject 

Source
pub struct Inject<T>(pub Arc<T>)
where
    T: ?Sized;
Expand description

A wrapper around Arc<T> that can be extracted from a ResolveContext.

T may be unsized (dyn Trait) for trait-object injection set up with bind!(dyn Trait => Concrete). For sized concrete types the standard Extract impl applies; for dyn Trait the generated provider code uses ctx.resolve_external::<Arc<dyn Trait>>() directly (no Extract impl needed — and no orphan-rule violation).

§Examples

// Concrete injectable type
pub struct UserService { db: Inject<Database> }

// Trait-object injection (set up with bind!)
pub struct NotificationService { mailer: Inject<dyn Mailer> }

// Axum handler destructuring pattern
async fn handler(Inject(svc): Inject<UserService>) -> impl IntoResponse { ... }

Tuple Fields§

§0: Arc<T>

Implementations§

Source§

impl<T> Inject<T>
where T: ?Sized,

Source

pub fn new(value: Arc<T>) -> Inject<T>

Create a new Inject from an Arc<T>.

Source

pub fn into_inner(self) -> Arc<T>

Consume the wrapper and return the inner Arc<T>.

Source

pub fn inner(&self) -> &Arc<T>

Borrow the inner Arc<T>.

Source

pub fn arc(&self) -> Arc<T>

Clone the inner Arc<T>.

Source

pub fn ptr_eq(&self, other: &Inject<T>) -> bool

Returns true if both Inject<T> values point to the same heap allocation.

Useful for asserting singleton semantics in tests without going through Arc::ptr_eq(a.inner(), b.inner()).

Trait Implementations§

Source§

impl<T> AsRef<T> for Inject<T>
where T: ?Sized,

Source§

fn as_ref(&self) -> &T

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

impl<T> Borrow<T> for Inject<T>
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> Clone for Inject<T>
where T: ?Sized,

Source§

fn clone(&self) -> Inject<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Inject<T>
where T: Debug + ?Sized,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Inject<T>
where T: ?Sized,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Inject<T> as Deref>::Target

Dereferences the value.
Source§

impl<T> Eq for Inject<T>
where T: Eq,

Source§

impl<T> Extract for Inject<T>
where T: Send + Sync + 'static,

Extract implementation for Inject<T> where T: Sized + Send + Sync + 'static.

Resolution order:

  1. Try resolve_external::<Arc<T>>() — finds InjectableArcFactory entries submitted by #[injectable] macros. These entries call resolve_singleton_arc internally, so singletons are properly cached.
  2. Fall back to resolve_external::<T>() — finds DynProvider<T> registrations for external types, then wraps the result in Arc::new.
Source§

fn extract<'life0, 'async_trait>( ctx: &'life0 ResolveContext, ) -> Pin<Box<dyn Future<Output = Result<Inject<T>, InjectableError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Inject<T>: 'async_trait,

Extract a value from the given resolution context. Read more
Source§

impl<T> From<Arc<T>> for Inject<T>
where T: ?Sized,

Source§

fn from(arc: Arc<T>) -> Inject<T>

Converts to this type from the input type.
Source§

impl<T> From<Inject<T>> for Arc<T>
where T: ?Sized,

Source§

fn from(inject: Inject<T>) -> Arc<T>

Converts to this type from the input type.
Source§

impl<T> Hash for Inject<T>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> PartialEq for Inject<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Inject<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Inject<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Inject<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for Inject<T>
where T: Sync + Send + ?Sized,

§

impl<T> Sync for Inject<T>
where T: Sync + Send + ?Sized,

§

impl<T> Unpin for Inject<T>
where T: ?Sized,

§

impl<T> UnsafeUnpin for Inject<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Inject<T>
where T: RefUnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.