speedyspec-container
A service container with the functionality of Laravel's: bindings, singletons, scoped instances, aliases, extenders, contextual bindings, tags and resolving callbacks, with worker and request lifetimes.
Install
Run this in your project folder. cargo add speedyspec-container
speedyspec-container
A service container with the functionality of Laravel's: bindings, singletons, scoped instances, aliases, extenders, contextual bindings, tags and resolving callbacks, with worker and request lifetimes.
Part of the SpeedySpec platform.
Role: platform. Seam: container.
What it gives you
Container: bindings, singletons, scoped instances, aliases, extenders, contextual bindings (when), tags and resolving callbacks, with the functionality of Laravel's container.bind_port,singleton_portandmake_port: a port (a trait object) bound by key and resolved asRc<dyn Port>.Lifetime::WorkerandLifetime::Request:Container::end_requestdrops every request-lifetime entry and puts each key back on its worker entry.- Foreign entries (
bind_foreign,is_foreign): what a language bridge such asspeedyspec-container-phpregisters to override a Rust binding.
Install
cargo add speedyspec-container
Requirements
Rust 1.99.
Example
Bind a port to a named factory and resolve it.
use std::rc::Rc;
use speedyspec_container::{Container, ContainerError, Params};
trait Greeter {
fn greet(&self, name: &str) -> String;
}
struct English;
impl Greeter for English {
fn greet(&self, name: &str) -> String {
format!("Hello, {name}")
}
}
fn english(_: &Container, _: &Params) -> Result<Rc<dyn Greeter>, ContainerError> {
Ok(Rc::new(English))
}
let container = Container::new();
container.singleton_port::<dyn Greeter>("greeter", english);
let greeter = container.make_port::<dyn Greeter>("greeter").unwrap();
assert_eq!(greeter.greet("Ada"), "Hello, Ada");
Where it fits
Every seam's contracts crate names its ports by key, and the runtime binds an implementation to each key here. PHP
overrides a binding through speedyspec-container-php, for one request. speedyspec-cqrs resolves its handlers
from this container. See the guide's container seam.
Versioning
SemVer, 0.x: a minor version may break the API. MSRV is Rust 1.99. SpeedySpec's own version (ADR-028) is the PHP runtime's major, not this crate's.
License
MIT