pub struct Ref<'a, T: 'a + ?Sized> { /* private fields */ }
Expand description
A wrapper referencing a value in an Obstack
.
A Ref
value owns the value it references, and will invoke drop
on the value when the Ref
goes out of scope. Effectively a Ref
is a Box
that uses an Obstack
rather than the heap.
The inherent methods of Ref
are all associated functions, which means you have to call them
as e.g. Ref::unwrap(value)
instead of value.unwrap()
. This avoids conflicts with methods of
the inner type T
.
Implementations§
Source§impl<'a, T: ?Sized> Ref<'a, T>
impl<'a, T: ?Sized> Ref<'a, T>
Sourcepub fn unwrap(this: Self) -> Twhere
T: Sized,
pub fn unwrap(this: Self) -> Twhere
T: Sized,
Returns the owned value, consuming the Ref
.
This allows the value to taken out of the Obstack
and used even after it goes out of
scope:
fn f() -> String {
let stack = Obstack::new();
let r = stack.push(String::from("foo"));
Ref::unwrap(r)
}
assert_eq!(f(), "foo");
Since obstacks only free memory when they go out of scope, the bytes_used
remains
unchanged:
let r = stack.push(String::new());
let used = stack.bytes_used();
let inner = Ref::unwrap(r);
assert_eq!(used, stack.bytes_used());
Trait Implementations§
Source§impl<'a, T> BorrowMut<T> for Ref<'a, T>
impl<'a, T> BorrowMut<T> for Ref<'a, T>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<'a, T> Ord for Ref<'a, T>where
T: Ord,
impl<'a, T> Ord for Ref<'a, T>where
T: Ord,
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'a, T> PartialOrd for Ref<'a, T>where
T: PartialOrd,
impl<'a, T> PartialOrd for Ref<'a, T>where
T: PartialOrd,
impl<'a, T> Eq for Ref<'a, T>where
T: Eq,
Auto Trait Implementations§
impl<'a, T> Freeze for Ref<'a, T>where
T: ?Sized,
impl<'a, T> RefUnwindSafe for Ref<'a, T>where
T: RefUnwindSafe + ?Sized,
impl<'a, T> Send for Ref<'a, T>
impl<'a, T> Sync for Ref<'a, T>
impl<'a, T> Unpin for Ref<'a, T>where
T: ?Sized,
impl<'a, T> !UnwindSafe for Ref<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more