Attribute Macro persian_rug::constraints
source ยท #[constraints]
Expand description
Add the type constraints necessary for an impl using persian-rug.
Rust currently requires all relevant constraints to be written out
for every impl using a given type. For persian-rug in particular,
there are typically many constraints of a simple kind: for every
type owned by the given Context
, there must be an Owner
implementation for the context and there must be a matching
Contextual
implementation for the type. This macro simply
generates these constraints for you.
The attribute takes two types of argument:
context
specifies the name of the type of the context.access(...)
specifies the types that this impl requires to exist within that context. Typically each type requires some other types to also exist in its context for it to be well-formed. This argument needs to be given the transitive closure of all such types, both direct and indirect dependencies of the impl itself. It is unfortunately not possible at present to find the indirect dependencies automatically.
Example:
use persian_rug::{contextual, Context, Mutator, Proxy};
#[contextual(C)]
struct Foo<C: Context> {
_marker: core::marker::PhantomData<C>,
a: i32
}
struct Bar<C: Context> {
foo: Proxy<Foo<C>>
}
#[persian_rug::constraints(context = C, access(Foo<C>))]
impl<C> Bar<C> {
pub fn new<M: Mutator<Context=C>>(foo: Foo<C>, mut mutator: M) -> Self {
Self { foo: mutator.add(foo) }
}
}