Expand description
§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
§In detail
The service container: Laravel’s container’s functionality, in Rust.
Every key (a port’s name, a class or an alias) holds a stack of entries: bindings (a factory, transient,
singleton or scoped) and instances. The top entry answers. Each entry has a Lifetime: Worker entries live as
long as the process; Request entries (everything PHP registers) are removed by Container::end_request, which
puts every key back on its worker-lifetime entry. An entry registered by a language bridge is foreign
(Container::is_foreign): that is the override flag, and a Rust binding never involves the bridge.
Container::epoch moves on any change to any binding, alias, extender or callback, and at every request start
and end. PortCell caches a resolved port against it, so a port reached through a proxy costs one compare when
nothing was rebound, and re-resolves (honouring the binding’s Sharing) when something was.
A container belongs to one thread (the worker’s, alongside PHP), so values are Rc.
Structs§
- Bridged
- A value with two faces: the port a Rust caller gets from
Container::make_port, and the value it came from in another language (a PHP object implementing the port’s twin), which that language’s bridge hands back. - Container
- See the crate docs.
- Needs
- The
when(..)->needs(..)of a contextual binding. - Params
- Parameters for
Container::make_with. - Port
Cell - A handle on the port
Pbound under a key. Holds the container weakly, so a container that owns, through an area or a handler, a value holding this cell is not kept alive by it. - Registering
- Returned by
Container::registering: registrations made while it lives have its lifetime. - When
- The
when(..)of a contextual binding.
Enums§
- Container
Error - Why a key could not be resolved. Every message names the key.
- Lifetime
- How long a registration lives.
- Sharing
- How a binding shares what it builds.
Traits§
- Autowirer
- Builds a key the container has no entry for (another language’s classes, through its reflection).