Struct Ref

Source
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>

Source

pub fn unwrap(this: Self) -> T
where 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> AsMut<T> for Ref<'a, T>

Source§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<'a, T> AsRef<T> for Ref<'a, T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, T> Borrow<T> for Ref<'a, T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T> BorrowMut<T> for Ref<'a, T>

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> Debug for Ref<'a, T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Deref for Ref<'a, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'a, T> DerefMut for Ref<'a, T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<'a, T> Display for Ref<'a, T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a, T: ?Sized> Drop for Ref<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T> Ord for Ref<'a, T>
where T: Ord,

Source§

fn cmp(&self, other: &Ref<'a, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, T> PartialEq for Ref<'a, T>
where T: PartialEq<T>,

Source§

fn eq(&self, other: &Ref<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Ref<'a, T>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T> PartialOrd for Ref<'a, T>
where T: PartialOrd,

Source§

fn partial_cmp(&self, other: &Ref<'a, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn le(&self, other: &Ref<'a, T>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn lt(&self, other: &Ref<'a, T>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn gt(&self, other: &Ref<'a, T>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Ref<'a, T>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T> Pointer for Ref<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

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>
where T: Send + ?Sized,

§

impl<'a, T> Sync for Ref<'a, T>
where T: Sync + ?Sized,

§

impl<'a, T> Unpin for Ref<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for Ref<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.