Expand description
§Qubit SPI
Typed service provider infrastructure for Qubit Rust crates.
This crate provides the small service-provider layer that many Qubit crates
need when a base crate owns a trait and extension crates provide optional
implementations. A ServiceProvider supplies stable names, aliases,
runtime availability, priority, and a factory method. A
ProviderRegistry resolves providers by name, by automatic priority, or
through an explicit fallback chain.
§Examples
Register a provider and create a service by name:
use std::fmt::Debug;
use qubit_spi::{
ProviderCreateError,
ProviderDescriptor,
ProviderRegistry,
ProviderRegistryError,
ServiceProvider,
ServiceSpec,
};
trait Greeter: Debug + Send + Sync {
fn greet(&self) -> &'static str;
}
#[derive(Debug)]
struct EnglishGreeter;
impl Greeter for EnglishGreeter {
fn greet(&self) -> &'static str {
"hello"
}
}
#[derive(Debug)]
struct EnglishProvider;
#[derive(Debug)]
struct GreeterSpec;
impl ServiceSpec for GreeterSpec {
type Config = ();
type Service = dyn Greeter;
}
impl ServiceProvider<GreeterSpec> for EnglishProvider {
fn descriptor(&self) -> Result<ProviderDescriptor, ProviderRegistryError> {
ProviderDescriptor::new("english")?.with_aliases(&["en"])
}
fn create_box(&self, _config: &()) -> Result<Box<dyn Greeter>, ProviderCreateError> {
Ok(Box::new(EnglishGreeter))
}
}
let mut registry = ProviderRegistry::<GreeterSpec>::new();
registry
.register(EnglishProvider)
.expect("provider names should be unique");
let greeter = registry
.create_box("en", &())
.expect("registered provider should create a greeter");
assert_eq!("hello", greeter.greet());Structs§
- Provider
Descriptor - Stable provider metadata used for registration and selection.
- Provider
Name - Stable provider id or alias accepted by a registry.
- Provider
Registry - Registry of providers for one service specification.
Enums§
- Provider
Availability - Availability of a provider in the current runtime environment.
- Provider
Create Error - Error returned by one provider while creating a service.
- Provider
Failure - Failure recorded for one provider candidate.
- Provider
Registry Error - Error returned by provider registries.
- Provider
Selection - Provider candidates used by registry selection.
Traits§
- Service
Provider - Factory contract for one service implementation.
- Service
Spec - Type-level description of one pluggable service family.