Key

Enum Key 

Source
pub enum Key {
    Type(TypeId, &'static str),
    Trait(&'static str),
    MultiTrait(&'static str, usize),
    TypeNamed(TypeId, &'static str, &'static str),
    TraitNamed(&'static str, &'static str),
    MultiTraitNamed(&'static str, &'static str, usize),
}
Expand description

Key for service storage and lookup.

Keys uniquely identify services in the container, supporting both unnamed and named service registrations. Each key type serves a specific purpose in the DI system’s service resolution mechanism.

§Key Types

  • Type: Concrete types (structs, enums, primitives)
  • Trait: Single trait implementations
  • MultiTrait: Multiple trait implementations with indexing
  • Named variants: All above with additional string names

§Examples

use ferrous_di::{ServiceCollection, Resolver, Key};
use std::sync::Arc;

// Concrete type keys
let mut services = ServiceCollection::new();
services.add_singleton(42u32);
services.add_named_singleton("config_port", 8080u32);

// Trait keys  
trait Logger: Send + Sync {
    fn log(&self, msg: &str);
}

struct ConsoleLogger;
impl Logger for ConsoleLogger {
    fn log(&self, msg: &str) {
        println!("LOG: {}", msg);
    }
}

services.add_singleton_trait(Arc::new(ConsoleLogger) as Arc<dyn Logger>);

let provider = services.build();

// Resolution uses keys internally
let number = provider.get_required::<u32>(); // Uses Type key
let port = provider.get_named_required::<u32>("config_port"); // Uses TypeNamed key  
let logger = provider.get_required_trait::<dyn Logger>(); // Uses Trait key

assert_eq!(*number, 42);
assert_eq!(*port, 8080);
logger.log("Service resolution successful");

Variants§

§

Type(TypeId, &'static str)

Concrete type key with TypeId and name for diagnostics

Used for registering and resolving concrete types like String, Database, custom structs, etc. The TypeId provides fast lookup while the name helps with debugging.

§

Trait(&'static str)

Single trait binding key

Used for registering and resolving trait objects like dyn Logger. Only stores the trait name since traits don’t have TypeId.

§

MultiTrait(&'static str, usize)

Multi-trait binding with index

Used when multiple implementations are registered for the same trait. The index distinguishes between different implementations.

§

TypeNamed(TypeId, &'static str, &'static str)

Named concrete type key with TypeId, typename, and name

Like Type but with an additional string name for cases where multiple instances of the same type need different registrations.

§

TraitNamed(&'static str, &'static str)

Named single trait binding key with trait name and service name

Like Trait but with an additional string name for different implementations of the same trait.

§

MultiTraitNamed(&'static str, &'static str, usize)

Named multi-trait binding with trait name, service name, and index

Combination of MultiTrait and naming for complex scenarios with multiple named implementations of the same trait.

Implementations§

Source§

impl Key

Source

pub fn display_name(&self) -> &'static str

Get the type or trait name for display

Returns the human-readable type or trait name for debugging and error messages. This is the std::any::type_name result.

§Examples
use ferrous_di::Key;
use std::any::TypeId;

let type_key = Key::Type(TypeId::of::<String>(), "alloc::string::String");
assert_eq!(type_key.display_name(), "alloc::string::String");

let trait_key = Key::Trait("dyn core::fmt::Debug");
assert_eq!(trait_key.display_name(), "dyn core::fmt::Debug");

let named_key = Key::TypeNamed(TypeId::of::<u32>(), "u32", "port");
assert_eq!(named_key.display_name(), "u32");
Source

pub fn service_name(&self) -> Option<&'static str>

Get the service name for named services, or None for unnamed services

Returns the service name for keys that represent named service registrations, or None for unnamed services.

§Examples
use ferrous_di::Key;
use std::any::TypeId;

// Unnamed services return None
let unnamed_key = Key::Type(TypeId::of::<String>(), "alloc::string::String");
assert_eq!(unnamed_key.service_name(), None);

let trait_key = Key::Trait("dyn core::fmt::Debug");
assert_eq!(trait_key.service_name(), None);

// Named services return Some(name)
let named_type = Key::TypeNamed(TypeId::of::<u32>(), "u32", "database_port");
assert_eq!(named_type.service_name(), Some("database_port"));

let named_trait = Key::TraitNamed("dyn myapp::Logger", "console_logger");
assert_eq!(named_trait.service_name(), Some("console_logger"));

Trait Implementations§

Source§

impl Clone for Key

Source§

fn clone(&self) -> Key

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Key

Source§

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

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

impl Hash for Key

Source§

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

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 Ord for Key

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Key

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Key

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Key

Auto Trait Implementations§

§

impl Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnwindSafe for Key

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