pub fn reference<'original, 'unbounded, Ref: Reference<'original, 'unbounded>>(
reference: Ref
) -> Ref::Unbounded
Expand description
Unbinds the lifetime in a reference (&T
or &mut T
). This
disconnects the input lifetime from the output lifetime, so use of the
output reference will no longer cause input reference to be kept “live” (as
defined in RFC 2094: non-lexical lifetimes). The output reference is
given an unbounded lifetime, so the borrow checker is free to coerce
it to whatever is expected.
The output lifetime may still be implicitly bound by any references within
the referenced type T
. You may need to unbind these as well, or replace
them with 'static
.
Safety
This is unsound and unsafe, even though it is marked as a safe function! Invoking this functions risks incurring the wrath of the optimizer. Rust references are not pointers. For edutainment purposes only.
Examples
Here we create two (aliasing) unbounded mutable references at once, and then continue to use them even after the original value is dropped (and its true lifetime has ended).
let mut x = 0;
let mut_1 = ::unbounded::reference(&mut x);
let mut_2 = ::unbounded::reference(&mut x);
drop(x);
*mut_1 = 1;
*mut_2 = 2;
assert_eq!(*mut_1, *mut_2, "I hope LLVM is in a good mood!");