supply 0.1.0

Provider API for arbitrary number of lifetimes.
Documentation

Supply

Provider API for arbitrary number of lifetimes.

Version docs.rs Version Crates.io License Crates.io

supply implements an API similar to that proposed in RFC 3192. It allows accessing data based on type tags. Expanding on RFC 3192, supply allows accessing data with an arbitrary number of lifetimes. Additionally, supply allows for multiple values to be requested at once.

use supply::{Provider, ProviderDyn, Want, LtList, Lt1, tag::AddLt, RequestExt as _};

struct Person<'a> {
    name: &'a str,
    age: u8,
}

// Implementing Provider allows requesting data from a Person value.
impl<'a> Provider for Person<'a> {
    type Lifetimes = Lt1<'a>;

    fn provide<'r>(&'r self, want: &mut dyn Want<'r, Lt1<'a>>) {
        // Provide the name and age fields.
        want.provide_tag::<AddLt<&AddLt<str>>>(self.name)
            .provide_value(self.age);
    }
}

// Make an example person to request data from.
let name = String::from("bob");

let provided_name;
{
    let person = Person {
        name: &name,
        age: 42,
    };

    // Convert to a trait object to show Provider is object safe.
    let provider: &dyn ProviderDyn<Lt1> = &person;

    // Request the person's name.
    provided_name = <AddLt<&AddLt<str>>>::request(provider);
    assert_eq!(provided_name, Some("bob"));

    // Request the person's age.
    let provided_age = u8::request(provider);
    assert_eq!(provided_age, Some(42));

    // Request something Person doesn't provide.
    // We just don't get a value from the request.
    let provided_something = f32::request(provider);
    assert_eq!(provided_something, None);
};

// Because the name was tagged as &'a str we can still access it here.
assert_eq!(provided_name, Some("bob"));

no_std Support

This crate is #![no_std] by default, it can be used anywhere Rust can.

Minimum Supported Rust Version

Requires Rust 1.79.0.

This crate follows the "Latest stable Rust" policy. The listed MSRV won't be changed unless needed. However, updating the MSRV anywhere up to the latest stable at time of release is allowed.

Contributing

Contributions in any form (issues, pull requests, etc.) to this project must adhere to Rust's Code of Conduct.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in supply by you shall be licensed as below, without any additional terms or conditions.

License

This project is licensed under either of

at your option.