I know we can cast from reference -> raw pointer -> address (usize
), but can we do it backwards especially with lifetime annotations? For example, I have following code:
struct Name<'a> {
name: &'a str,
}
impl<'a> Name<'a> {
fn to_addr<'b>(&'b self) -> usize { /* ... */ }
fn from_addr<'b>(address: usize) -> &'b Name<'a> {
// assuming the address is valid,
// is this even possible to return an reference with both lifetimes?
}
}
as_ref
method on the pointers is also an option. – Aldred