relaxed-ref 0.1.0

Reference types that don't require `T: 'a` to be well-formed, with the bound statically checked at the use site. Useful in GAT-based type families and similar contexts.
Documentation

relaxed-ref

This crate provides RelaxedRef<'a, T> and RelaxedRefMut<'a, T>, relaxed versions of &'a T and &'a mut T, respectively, that are well-formed without requiring T: 'a. Instead, this bound is statically verified at the use site. This is useful in GAT-based type families and other contexts where imposing T: 'a on the surrounding interface is not always possible or desirable.

Motivation

The reference types &'a T and &'a mut T require T: 'a to hold. In some contexts, it is not possible to express this condition due to interface constraints. This commonly occurs with type families based on generic associated types (GATs):

trait ValueFamily {
    type Of<T>;
}

struct RefFamily<'a>(PhantomData<&'a ()>);
impl<'a> ValueFamily for RefFamily<'a> {
    // error[E0309]: the parameter type `T` may not live long enough
    type Of<T> = &'a T;
}

In cases where modifying ValueFamily is impossible or undesirable, we can replace type Of<T> = &'a T with type Of<T> = RelaxedRef<'a, T>, since the latter is well-formed regardless of whether T: 'a holds:

impl<'a> ValueFamily for RefFamily<'a> {
    type Of<T> = RelaxedRef<'a, T>; // OK
}

Documentation

The API documentation and usage examples are available on docs.rs.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.