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>
impl<T> ReadCell<T>
Sourcepub const fn new(value: T) -> ReadCell<T>
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);
Sourcepub fn into_inner(self) -> T
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: ?Sized> ReadCell<T>
impl<T: ?Sized> ReadCell<T>
Sourcepub const fn as_ptr(&self) -> *mut T
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();
Sourcepub fn get_mut(&mut self) -> &mut T
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);
Sourcepub fn from_ref(t: &T) -> &ReadCell<T>
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);
Sourcepub fn from_cell(t: &Cell<T>) -> &ReadCell<T>
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]>
impl<T> ReadCell<[T]>
Sourcepub fn as_slice_of_cells(&self) -> &[ReadCell<T>]
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]>
impl<T, const N: usize> ReadCell<[T; N]>
Sourcepub fn as_array_of_cells(&self) -> &[ReadCell<T>; N]
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();