mutate_once

Struct MutOnce

Source
pub struct MutOnce<T> { /* private fields */ }
Expand description

A mutable memory location that is write-once and can be borrowed as plain &T.

Initially the value can be mutated through struct RefMut<T> obtained by get_mut method. When there is no RefMut alive, a shared reference &T can be taken by get_ref method. Once get_ref is called, get_mut must not be called again.

§Examples

use mutate_once::MutOnce;
struct Container {
    expensive: MutOnce<String>,
}
impl Container {
    fn expensive(&self) -> &str {
        if !self.expensive.is_fixed() {
            let mut ref_mut = self.expensive.get_mut();
            *ref_mut += "expensive";
            // Drop `ref_mut` before calling `get_ref`.
        }
        // A plain reference can be returned to the caller
        // unlike `Cell` or `RefCell`.
        self.expensive.get_ref()
    }
}
let container = Container { expensive: MutOnce::new(String::new()) };
assert_eq!(container.expensive(), "expensive");

Implementations§

Source§

impl<T> MutOnce<T>

Source

pub const fn new(value: T) -> Self

Creates a new MutOnce containing the given value.

Source

pub fn get_mut(&self) -> RefMut<'_, T>

Mutably borrows the wrapped value.

The borrow lasts until the returned RefMut gets dropped. This method must not be called if another RefMut is active or get_ref is ever called.

§Panics

Panics if the value is currently mutably borrowed or ever immutably borrowed.

§Examples
let mo = mutate_once::MutOnce::new(0);
*mo.get_mut() += 2;
*mo.get_mut() += 5;
assert_eq!(*mo.get_ref(), 7);

Panics if another mutable borrow is active:

let mo = mutate_once::MutOnce::new(0);
let mut ref_mut = mo.get_mut();
*mo.get_mut() += 7;     // Panics because `ref_mut` is still active.

Panics if get_ref is ever called:

let mo = mutate_once::MutOnce::new(0);
assert_eq!(*mo.get_ref(), 0);
*mo.get_mut() += 7;     // Panics because `get_ref` is called once.
Source

pub fn get_ref(&self) -> &T

Returns an immutable reference to the value.

This method must not be called while the value is mutably borrowed.

§Panics

Panics if the value is currently mutably borrowed.

§Examples
let mo = mutate_once::MutOnce::new(0);
*mo.get_mut() += 7;
assert_eq!(*mo.get_ref(), 7);

Panics if a mutable borrow is active:

let mo = mutate_once::MutOnce::new(0);
let mut ref_mut = mo.get_mut();
mo.get_ref();           // Panics because `ref_mut` is still active.
Source

pub fn is_fixed(&self) -> bool

Returns true if the value can be no longer mutated (in other words, if get_ref is ever called).

Source

pub fn into_inner(self) -> T

Consumes the MutOnce, returning the wrapped value.

Trait Implementations§

Source§

impl<T: Debug> Debug for MutOnce<T>

Source§

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

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

impl<T: Default> Default for MutOnce<T>

Source§

fn default() -> MutOnce<T>

Returns the “default value” for a type. Read more
Source§

impl<T> From<T> for MutOnce<T>

Source§

fn from(t: T) -> MutOnce<T>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Freeze for MutOnce<T>

§

impl<T> !RefUnwindSafe for MutOnce<T>

§

impl<T> Send for MutOnce<T>
where T: Send,

§

impl<T> !Sync for MutOnce<T>

§

impl<T> Unpin for MutOnce<T>
where T: Unpin,

§

impl<T> UnwindSafe for MutOnce<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<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.