pub struct Tracked<'a, T, C = <T as Validate>::Constraint>where
T: Track + ?Sized,{ /* private fields */ }Expand description
Tracks accesses to a value.
Encapsulates a reference to a value and tracks all accesses to it. The only
methods accessible on Tracked<T> are those defined in an implementation
block or trait for T annotated with #[track]. For more details, see its
documentation.
Variance
Typically you can ignore the defaulted C parameter. However, due to
compiler limitations, this type will then be invariant over T. This limits
how it can be used. In particular, invariance prevents you from creating a
usable chain of tracked types.
struct Chain<'a> {
outer: Tracked<'a, Self>,
data: u32, // some data for the chain link
}However, this is sometimes a useful pattern (for example, it allows you to detect cycles in memoized recursive algorithms). If you want to create a tracked chain or need covariance for another reason, you need to manually specify the constraint type like so:
struct Chain<'a> {
outer: Tracked<'a, Self, <Chain<'static> as Validate>::Constraint>,
data: u32, // some data for the chain link
}Notice the 'static lifetime: This makes the compiler understand that no
strange business that depends on 'a is happening in the associated
constraint type. (In fact, all constraints are 'static.)