Struct ReadCell

Source
pub struct ReadCell<T: ?Sized> { /* private fields */ }
Expand description

A possible mutable memory location. It provides only non-mutating subset of Cell API. This allows &ReadCell<T> share value with &Cell<T> and &T alike.

§Example

use std::cell::Cell;
use read_cell::ReadCell;

struct SomeStruct {
    regular_field: u8,
    special_field: Cell<u8>,
}

let my_struct = SomeStruct {
    regular_field: 0,
    special_field: Cell::new(1),
};

let new_value = 100;

let regular_field_read = ReadCell::from_ref(&my_struct.regular_field);
let special_field_read = ReadCell::from_cell(&my_struct.special_field);

assert_eq!(regular_field_read.get(), 0);
assert_eq!(special_field_read.get(), 1);

my_struct.special_field.set(new_value);
assert_eq!(special_field_read.get(), new_value);

Implementations§

Source§

impl<T> ReadCell<T>

Source

pub const fn new(value: T) -> ReadCell<T>

Creates a new ReadCell containing the given value.

§Examples
use read_cell::ReadCell;

let c = ReadCell::new(5);
Source

pub fn into_inner(self) -> T

Unwraps the value.

§Examples
use read_cell::ReadCell;

let c = ReadCell::new(5);
let five = c.into_inner();

assert_eq!(five, 5);
Source§

impl<T: Copy> ReadCell<T>

Source

pub fn get(&self) -> T

Returns a copy of the contained value.

§Examples
use read_cell::ReadCell;

let c = ReadCell::new(5);

let five = c.get();
Source§

impl<T: ?Sized> ReadCell<T>

Source

pub const fn as_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data in this cell.

§Examples
use read_cell::ReadCell;

let c = ReadCell::new(5);

let ptr = c.as_ptr();
Source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

This call borrows ReadCell mutably (at compile-time) which guarantees that we possess the only reference.

However be cautious: this method expects self to be mutable, which is generally not the case when using a ReadCell. If you require interior mutability by reference, consider using RefCell which provides run-time checked mutable borrows through its borrow_mut method.

§Examples
use read_cell::ReadCell;

let mut c = ReadCell::new(5);
*c.get_mut() += 1;

assert_eq!(c.get(), 6);
Source

pub fn from_ref(t: &T) -> &ReadCell<T>

Returns a &ReadCell<T> from a &T

§Examples
use read_cell::ReadCell;

let slice: &[i32] = &[1, 2, 3];
let cell_slice: &ReadCell<[i32]> = ReadCell::from_ref(slice);
let slice_cell: &[ReadCell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);
Source

pub fn from_cell(t: &Cell<T>) -> &ReadCell<T>

Returns a &ReadCell<T> from a &Cell<T>

§Examples
use std::cell::Cell;
use read_cell::ReadCell;

let slice: &Cell<[i32]> = &Cell::new([1, 2, 3]);
let cell_slice: &ReadCell<[i32]> = ReadCell::from_cell(slice);
let slice_cell: &[ReadCell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);
Source§

impl<T> ReadCell<[T]>

Source

pub fn as_slice_of_cells(&self) -> &[ReadCell<T>]

Returns a &[ReadCell<T>] from a &ReadCell<[T]>

§Examples
use read_cell::ReadCell;

let slice: &[i32] = &[1, 2, 3];
let cell_slice: &ReadCell<[i32]> = ReadCell::from_ref(slice);
let slice_cell: &[ReadCell<i32>] = cell_slice.as_slice_of_cells();

assert_eq!(slice_cell.len(), 3);
Source§

impl<T, const N: usize> ReadCell<[T; N]>

Source

pub fn as_array_of_cells(&self) -> &[ReadCell<T>; N]

Returns a &[ReadCell<T>; N] from a &ReadCell<[T; N]>

§Examples
use read_cell::ReadCell;

let mut array: [i32; 3] = [1, 2, 3];
let cell_array: &ReadCell<[i32; 3]> = ReadCell::from_ref(&array);
let array_cell: &[ReadCell<i32>; 3] = cell_array.as_array_of_cells();

Trait Implementations§

Source§

impl<T: Copy> Clone for ReadCell<T>

Source§

fn clone(&self) -> ReadCell<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Default> Default for ReadCell<T>

Source§

fn default() -> ReadCell<T>

Creates a ReadCell<T>, with the Default value for T.

Source§

impl<T> From<T> for ReadCell<T>

Source§

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

Creates a new ReadCell<T> containing the given value.

Source§

impl<T: Ord + Copy> Ord for ReadCell<T>

Source§

fn cmp(&self, other: &ReadCell<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<T: PartialEq + Copy> PartialEq for ReadCell<T>

Source§

fn eq(&self, other: &ReadCell<T>) -> bool

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

fn ne(&self, other: &Rhs) -> bool

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

impl<T: PartialOrd + Copy> PartialOrd for ReadCell<T>

Source§

fn partial_cmp(&self, other: &ReadCell<T>) -> Option<Ordering>

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

fn lt(&self, other: &ReadCell<T>) -> bool

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

fn le(&self, other: &ReadCell<T>) -> bool

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

fn gt(&self, other: &ReadCell<T>) -> bool

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

fn ge(&self, other: &ReadCell<T>) -> bool

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

impl<T: Eq + Copy> Eq for ReadCell<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ReadCell<T>

§

impl<T> !RefUnwindSafe for ReadCell<T>

§

impl<T> Send for ReadCell<T>
where T: Send + ?Sized,

§

impl<T> !Sync for ReadCell<T>

§

impl<T> Unpin for ReadCell<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for ReadCell<T>
where T: UnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.