You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The WithLifetime trait is a unsafe trait that let's us be generic over changing the lifetime of some type. This is equivalent to
let x:&'old = &T;&'new *(x as*constT)
However lifetimes can also be type parameters of generic types. For example T could be a Vec<&'a i32>, but in generic code you don't have access to the lifetime parameters. So we create this new trait that lets us transmute the lifetime. However the problem with this trait is that the output type is different then the input type, even though they are really the same type but with different lifetime parameters.
For example here is the implementation for a GcManaged reference.
This trait is only safe to implement for types that are managed by the GC. The output type must only be the input type with the lifetime set to 'new. And even then we have to take care when calling it. The Out GAT is constrained to the 'new lifetime, and that lifetime is a parameter of the trait bounds. Here is an example usage
pub(crate)fnbind<'ob>(&self, _:&'ob Context) -> <TasWithLifetime<'ob>>::OutwhereT:WithLifetime<'ob> + Copy,{// SAFETY: We are holding a reference to the contextunsafe{self.inner.with_lifetime()}}
Here the output of this functions is <T as WithLifetime<'ob>>::Out and the trait bounds specify the lifetime we want to transmute it to. Since T implements WithLifetime we know that is is safe to bind to the lifetime of the Context because it will not be dropped as long as that immutable reference exists.
The text was updated successfully, but these errors were encountered:
rune/src/core/object/tagged.rs
Lines 173 to 176 in 685e60c
The
WithLifetime
trait is a unsafe trait that let's us be generic over changing the lifetime of some type. This is equivalent toHowever lifetimes can also be type parameters of generic types. For example
T
could be aVec<&'a i32>
, but in generic code you don't have access to the lifetime parameters. So we create this new trait that lets us transmute the lifetime. However the problem with this trait is that the output type is different then the input type, even though they are really the same type but with different lifetime parameters.For example here is the implementation for a
GcManaged
reference.rune/src/core/object/tagged.rs
Lines 185 to 192 in 685e60c
why this is sound
This trait is only safe to implement for types that are managed by the GC. The output type must only be the input type with the lifetime set to
'new
. And even then we have to take care when calling it. TheOut
GAT is constrained to the'new
lifetime, and that lifetime is a parameter of the trait bounds. Here is an example usageHere the output of this functions is
<T as WithLifetime<'ob>>::Out
and the trait bounds specify the lifetime we want to transmute it to. Since T implementsWithLifetime
we know that is is safe to bind to the lifetime of theContext
because it will not be dropped as long as that immutable reference exists.The text was updated successfully, but these errors were encountered: