Dynamically request arbitrarily-typed values from providers with borrowed data.
Motivation
Rust's Any trait allows for downcasting to access the underlying type of a dyn object. Unfortunately, this is limited to 'static types (types that contain no borrowed data) because there's no way to distinguish types with different lifetimes by their TypeIds.
This crate provides the dynamically-sized Query type with lifetime parameters and an internal tag indicating what type of value it's meant to hold,
potentially containing those lifetimes.
The Provide and ProvideRef traits supply values to queries, the latter being dyn-capable.
Prior Art
Concepts
Lifetime lists
Lists of lifetime variables are represented by types implementing Lt.
You can describe a lifetime list with the Lt! macro:
use Lt;
/// Lifetime list for retrieving values from `'data` with an argument that
/// borrows `'input`.
type CustomLifetimes<'data, 'input> = Lt!;
/// Prepend a `'local` lifetime to lifetime list `L`.
type PrependLifetime<'local, L> = Lt!;
Type functions
TypeFn implementations describe a type that is parameterized over an arbitrary
lifetime list.
A simple type function can be described with the TypeFn! macro:
type RefPair<A, B> = TypeFn!;
Resource tags
ResourceTag implementations describe how values may be provided to a Query.
The trait has two associated TypeFns: Arg, which determines what values are needed to request the resource,
and Out, which defines the type of the resource value.
The [define_tag!] macro provides a convenient way to declare resource tags:
define_tag!
Providers
Supplies values with the lifetime variables in L to the Query object passed to the
provide() method.
Supplies values from a reference to Self with the lifetime of the reference and the lifetime
variables in L.
A reference to a Provide implementation (say, &'x impl ProvideRef<L> or &'x mut impl ProvideRef<L>) automatically implements Provide<'x, ..L> for all 'x.
Examples
Dynamic field access
use ;
define_tag!
/// Dynamically gets a reference to the field with name `key` and type `T`, if
/// provided.
Sized + 'static>
// Retrieve fields from a value of type MyData:
let data = MyData ;
assert_eq!;
assert_eq!;
assert_eq!;
// Use () when you need an empty provider:
assert!;
Features
"alloc"
Adds the ProvideBox trait.
Adds trait implementations for Rc, Box, and String,
and enables additional provided values for std types.