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
impl Key
Sourcepub fn display_name(&self) -> &'static str
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");Sourcepub fn service_name(&self) -> Option<&'static str>
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"));