devela::sys::mem::cell

Trait ExtCellOption

Source
pub trait ExtCellOption<T>: Sealed {
    // Required methods
    fn modify<F: FnOnce(T) -> T>(&self, func: F);
    fn modify_ret<F: FnOnce(T) -> T>(&self, func: F) -> Option<T> 
       where T: Clone;
    fn modify_mut<R, F: FnOnce(&mut T) -> R>(&self, func: F) -> Option<R> ;
}
Expand description

A trait that provides additional methods for Cell<Option>.

Required Methods§

Source

fn modify<F: FnOnce(T) -> T>(&self, func: F)

Modifies the value inside the Cell<Option<T>> by applying the provided closure to a mutable reference of the current value if present.

This method extracts the value, applies the function, and stores the result back.

§Example
use devela::{Cell, ExtCellOption};

let cell = Cell::new(Some(10));
cell.modify(|x| x + 5);
assert_eq![cell.get(), Some(15)];
Source

fn modify_ret<F: FnOnce(T) -> T>(&self, func: F) -> Option<T>
where T: Clone,

Modifies the value inside the Cell<Option<T>> by applying the provided function and returns the old contained value.

§Example
use devela::{Cell, ExtCellOption};

let cell = Cell::new(Some(10));
let old = cell.modify_ret(|x| x + 5);
assert_eq![old, Some(10)];
assert_eq![cell.get(), Some(15)];
Source

fn modify_mut<R, F: FnOnce(&mut T) -> R>(&self, func: F) -> Option<R>

Modifies the value inside the Cell<Option<T>> by applying the provided closure to a mutable reference of the current value if present, and returns a result.

This method allows in-place modification via a mutable reference, returning any value.

§Example
use devela::{Cell, ExtCellOption};

let cell = Cell::new(Some(10));
let result = cell.modify_mut(|x| {
    let old = *x;
    *x *= 2;
    old
});

assert_eq![result, Some(10)];
assert_eq![cell.get(), Some(20)];

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> ExtCellOption<T> for Cell<Option<T>>