speedyspec-container 0.2.0

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.
Documentation
  • Coverage
  • 61.73%
    50 out of 81 items documented1 out of 65 items with examples
  • Size
  • Source code size: 44.0 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 760.0 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SpeedySpec/speedyspec-platform
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jacobsantos

speedyspec-container

crates.io docs.rs MIT

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_port and make_port: a port (a trait object) bound by key and resolved as Rc<dyn Port>.
  • Lifetime::Worker and Lifetime::Request: Container::end_request drops every request-lifetime entry and puts each key back on its worker entry.
  • Foreign entries (bind_foreign, is_foreign): what a language bridge such as speedyspec-container-php registers 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