pub trait CovariantForLt {
type Of<'lt>
where Self: 'lt;
// Required method
fn covariant_cast<'smol, 'humongous: 'smol>(
it: Self::Of<'humongous>
) -> Self::Of<'smol>;
}
Available on
advanced
only.Expand description
Same as crate::ForLifetime
, but for enforcing covariance of
Self::Of<'_>
(over '_
).
Example
use ::higher_kinded_types::extra_arities::*;
// 👇
fn higher_kinded_api<'caller, T : CovariantForLt>(
caller: T::Of<'caller>,
from_str: impl FnOnce(&str) -> T::Of<'_>,
) -> bool
where
for<'callee>
T::Of<'callee> : PartialEq
,
{
let local: String = ::std::fs::read_to_string(file!()).expect("demo");
let callee: T::Of<'_> = from_str(&local);
let comparison = {
callee == T::covariant_cast(caller) // 👈
};
comparison
}
new_For_type! {
type StrRef = For!(#![covariant]<'r> = &'r str);
}
higher_kinded_api::<StrRef>("…", |s| s);
Counter-example
ⓘ
use ::higher_kinded_types::extra_arities::*;
new_For_type! {
type NotCov = For!(#![covariant]<'r> = &'r mut &'r str);
}
yields:
ⓘ
error: lifetime may not live long enough
--> src/lib.rs:126:1
|
6 | / new_For_type! {
7 | | type NotCov = For!(#![covariant]<'r> = &'r mut &'r str);
8 | | }
| | ^
| | |
| | lifetime `'if_you_are_getting_this_error` defined here
| |_lifetime `'it_means_your_type_is_not_covariant` defined here
| associated function was supposed to return data with lifetime `'it_means_your_type_is_not_covariant` but it is returning data with lifetime `'if_you_are_getting_this_error`
|
= help: consider adding the following bound: `'if_you_are_getting_this_error: 'it_means_your_type_is_not_covariant`
= note: requirement occurs because of a mutable reference to `&str`
= note: mutable references are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
= note: this error originates in the macro `$crate::ඞFor` which comes from the expansion of the macro `new_For_type` (in Nightly builds, run with -Z macro-backtrace for more info)
Required Associated Types§
Required Methods§
sourcefn covariant_cast<'smol, 'humongous: 'smol>(
it: Self::Of<'humongous>
) -> Self::Of<'smol>
fn covariant_cast<'smol, 'humongous: 'smol>( it: Self::Of<'humongous> ) -> Self::Of<'smol>
The actual “proof” which higher-kinded callees dealing with implementors of this trait can use in order to take advantage of variance.