#![allow(unused)]fnmain() {
structFoo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
where
T: Convert<'a, 'b>;
traitConvert<'a, 'b>: Sized {
fncast(&'aself) -> &'bSelf;
}
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
fncast(&'longself) -> &'short T {
self
}
}
// errorfnbadboi<'in_, 'out, T>(
x: Foo<'in_, 'out, T>,
sadness: &'in_ T
) -> &'out T {
sadness.cast()
}
}
ⓘ
In this example, we tried to set a value with an incompatible lifetime to
another one ('in_ is unrelated to 'out). We can solve this issue in
two different ways:
Either we make 'in_ live at least as long as 'out:
#![allow(unused)]fnmain() {
structFoo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
where
T: Convert<'a, 'b>;
traitConvert<'a, 'b>: Sized {
fncast(&'aself) -> &'bSelf;
}
impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
fncast(&'longself) -> &'short T {
self
}
}
fnbadboi<'in_: 'out, 'out, T>(
x: Foo<'in_, 'out, T>,
sadness: &'in_ T
) -> &'out T {
sadness.cast()
}
}