Movatterモバイル変換


[0]ホーム

URL:


Cell

std::cell

StructCell 

1.0.0 ·Source
pub struct Cell<T>
where T: ?Sized,
{/* private fields */ }
Expand description

A mutable memory location.

§Memory layout

Cell<T> has the samememory layout and caveats asUnsafeCell<T>. In particular, this means thatCell<T> has the same in-memory representation as its inner typeT.

§Examples

In this example, you can see thatCell<T> enables mutation inside animmutable struct. In other words, it enables “interior mutability”.

usestd::cell::Cell;structSomeStruct {    regular_field: u8,    special_field: Cell<u8>,}letmy_struct = SomeStruct {    regular_field:0,    special_field: Cell::new(1),};letnew_value =100;// ERROR: `my_struct` is immutable// my_struct.regular_field = new_value;// WORKS: although `my_struct` is immutable, `special_field` is a `Cell`,// which can always be mutatedmy_struct.special_field.set(new_value);assert_eq!(my_struct.special_field.get(), new_value);

See themodule-level documentation for more.

Implementations§

Source§

impl<T>Cell<T>

1.0.0 (const: 1.24.0) ·Source

pub const fnnew(value: T) ->Cell<T>

Creates a newCell containing the given value.

§Examples
usestd::cell::Cell;letc = Cell::new(5);
1.0.0 (const:unstable) ·Source

pub fnset(&self, val: T)

Sets the contained value.

§Examples
usestd::cell::Cell;letc = Cell::new(5);c.set(10);
1.17.0 ·Source

pub fnswap(&self, other: &Cell<T>)

Swaps the values of twoCells.

The difference withstd::mem::swap is that this function doesn’trequire a&mut reference.

§Panics

This function will panic ifself andother are differentCells that partially overlap.(Using just standard library methods, it is impossible to create such partially overlappingCells.However, unsafe code is allowed to e.g. create two&Cell<[i32; 2]> that partially overlap.)

§Examples
usestd::cell::Cell;letc1 = Cell::new(5i32);letc2 = Cell::new(10i32);c1.swap(&c2);assert_eq!(10, c1.get());assert_eq!(5, c2.get());
1.17.0 (const: 1.88.0) ·Source

pub const fnreplace(&self, val: T) -> T

Replaces the contained value withval, and returns the old contained value.

§Examples
usestd::cell::Cell;letcell = Cell::new(5);assert_eq!(cell.get(),5);assert_eq!(cell.replace(10),5);assert_eq!(cell.get(),10);
1.17.0 (const: 1.83.0) ·Source

pub const fninto_inner(self) -> T

Unwraps the value, consuming the cell.

§Examples
usestd::cell::Cell;letc = Cell::new(5);letfive = c.into_inner();assert_eq!(five,5);
Source§

impl<T>Cell<T>
where T:Copy,

1.0.0 (const: 1.88.0) ·Source

pub const fnget(&self) -> T

Returns a copy of the contained value.

§Examples
usestd::cell::Cell;letc = Cell::new(5);letfive = c.get();
1.88.0 (const:unstable) ·Source

pub fnupdate(&self, f: implFnOnce(T) -> T)

Updates the contained value using a function.

§Examples
usestd::cell::Cell;letc = Cell::new(5);c.update(|x| x +1);assert_eq!(c.get(),6);
Source§

impl<T>Cell<T>
where T: ?Sized,

1.12.0 (const: 1.32.0) ·Source

pub const fnas_ptr(&self) ->*mut T

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

§Examples
usestd::cell::Cell;letc = Cell::new(5);letptr = c.as_ptr();
1.11.0 (const: 1.88.0) ·Source

pub const fnget_mut(&mut self) ->&mut T

Returns a mutable reference to the underlying data.

This call borrowsCell mutably (at compile-time) which guaranteesthat we possess the only reference.

However be cautious: this method expectsself to be mutable, which isgenerally not the case when using aCell. If you require interiormutability by reference, consider usingRefCell which providesrun-time checked mutable borrows through itsborrow_mut method.

§Examples
usestd::cell::Cell;letmutc = Cell::new(5);*c.get_mut() +=1;assert_eq!(c.get(),6);
1.37.0 (const: 1.88.0) ·Source

pub const fnfrom_mut(t:&mut T) -> &Cell<T>

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

§Examples
usestd::cell::Cell;letslice:&mut[i32] =&mut[1,2,3];letcell_slice:&Cell<[i32]> = Cell::from_mut(slice);letslice_cell:&[Cell<i32>] = cell_slice.as_slice_of_cells();assert_eq!(slice_cell.len(),3);
Source§

impl<T>Cell<T>
where T:Default,

1.17.0 (const:unstable) ·Source

pub fntake(&self) -> T
where T:Default,

Takes the value of the cell, leavingDefault::default() in its place.

§Examples
usestd::cell::Cell;letc = Cell::new(5);letfive = c.take();assert_eq!(five,5);assert_eq!(c.into_inner(),0);
Source§

impl<T>Cell<[T]>

1.37.0 (const: 1.88.0) ·Source

pub const fnas_slice_of_cells(&self) -> &[Cell<T>]

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

§Examples
usestd::cell::Cell;letslice:&mut[i32] =&mut[1,2,3];letcell_slice:&Cell<[i32]> = Cell::from_mut(slice);letslice_cell:&[Cell<i32>] = cell_slice.as_slice_of_cells();assert_eq!(slice_cell.len(),3);
Source§

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

1.91.0 (const: 1.91.0) ·Source

pub const fnas_array_of_cells(&self) -> &[Cell<T>;N]

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

§Examples
usestd::cell::Cell;letmutarray: [i32;3] = [1,2,3];letcell_array:&Cell<[i32;3]> = Cell::from_mut(&mutarray);letarray_cell:&[Cell<i32>;3] = cell_array.as_array_of_cells();
Source§

impl<T>Cell<T>
where T:CloneFromCell,

Source

pub fnget_cloned(&self) ->Cell<T>

🔬This is a nightly-only experimental API. (cell_get_cloned #145329)

Get a clone of theCell that contains a copy of the original value.

This allows a cheaplyClone-able type like anRc to be stored in aCell, exposing thecheaperclone() method.

§Examples
#![feature(cell_get_cloned)]usecore::cell::Cell;usestd::rc::Rc;letrc = Rc::new(1usize);letc1 = Cell::new(rc);letc2 = c1.get_cloned();assert_eq!(*c2.into_inner(),1);

Trait Implementations§

1.0.0 ·Source§

impl<T>Clone forCell<T>
where T:Copy,

Source§

fnclone(&self) ->Cell<T>

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

fnclone_from(&mut self, source: &Self)

Performs copy-assignment fromsource.Read more
1.0.0 ·Source§

impl<T>Debug forCell<T>
where T:Copy +Debug,

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.0.0 (const:unstable) ·Source§

impl<T>Default forCell<T>
where T:Default,

Source§

fndefault() ->Cell<T>

Creates aCell<T>, with theDefault value for T.

1.12.0 (const:unstable) ·Source§

impl<T>From<T> forCell<T>

Source§

fnfrom(t: T) ->Cell<T>

Creates a newCell<T> containing the given value.

1.10.0 ·Source§

impl<T>Ord forCell<T>
where T:Ord +Copy,

Source§

fncmp(&self, other: &Cell<T>) ->Ordering

This method returns anOrdering betweenself andother.Read more
1.21.0 ·Source§

fnmax(self, other: Self) -> Self
where Self:Sized,

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

fnmin(self, other: Self) -> Self
where Self:Sized,

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

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

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

impl<T>PartialEq forCell<T>
where T:PartialEq +Copy,

Source§

fneq(&self, other: &Cell<T>) ->bool

Tests forself andother values to be equal, and is used by==.
1.0.0 ·Source§

fnne(&self, other:&Rhs) ->bool

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

impl<T>PartialOrd forCell<T>
where T:PartialOrd +Copy,

Source§

fnpartial_cmp(&self, other: &Cell<T>) ->Option<Ordering>

This method returns an ordering betweenself andother values if one exists.Read more
Source§

fnlt(&self, other: &Cell<T>) ->bool

Tests less than (forself andother) and is used by the< operator.Read more
Source§

fnle(&self, other: &Cell<T>) ->bool

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

fngt(&self, other: &Cell<T>) ->bool

Tests greater than (forself andother) and is used by the>operator.Read more
Source§

fnge(&self, other: &Cell<T>) ->bool

Tests greater than or equal to (forself andother) and is used bythe>= operator.Read more
Source§

impl<T, U>CoerceUnsized<Cell<U>> forCell<T>
where T:CoerceUnsized<U>,

Source§

impl<T, U>DispatchFromDyn<Cell<U>> forCell<T>
where T:DispatchFromDyn<U>,

1.2.0 ·Source§

impl<T>Eq forCell<T>
where T:Eq +Copy,

Source§

impl<T>PinCoerceUnsized forCell<T>
where T: ?Sized,

1.0.0 ·Source§

impl<T>Send forCell<T>
where T:Send + ?Sized,

1.0.0 ·Source§

impl<T> !Sync forCell<T>
where T: ?Sized,

Auto Trait Implementations§

§

impl<T> !Freeze forCell<T>

§

impl<T> !RefUnwindSafe forCell<T>

§

impl<T>Unpin forCell<T>
where T:Unpin + ?Sized,

§

impl<T>UnwindSafe forCell<T>
where T:UnwindSafe + ?Sized,

Blanket Implementations§

Source§

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

Source§

fntype_id(&self) ->TypeId

Gets theTypeId ofself.Read more
Source§

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

Source§

fnborrow(&self) ->&T

Immutably borrows from an owned value.Read more
Source§

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

Source§

fnborrow_mut(&mut self) ->&mut T

Mutably borrows from an owned value.Read more
Source§

impl<T>CloneToUninit for T
where T:Clone,

Source§

unsafe fnclone_to_uninit(&self, dest:*mutu8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment fromself todest.Read more
Source§

impl<T>From<!> for T

Source§

fnfrom(t:!) -> T

Converts to this type from the input type.
Source§

impl<T>From<T> for T

Source§

fnfrom(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fninto(self) -> U

CallsU::from(self).

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

Source§

impl<T>ToOwned for T
where T:Clone,

Source§

typeOwned = T

The resulting type after obtaining ownership.
Source§

fnto_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning.Read more
Source§

fnclone_into(&self, target:&mut T)

Uses borrowed data to replace owned data, usually by cloning.Read more
Source§

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

Source§

typeError =Infallible

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

fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

typeError = <U asTryFrom<T>>::Error

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

fntry_into(self) ->Result<U, <U asTryFrom<T>>::Error>

Performs the conversion.

[8]ページ先頭

©2009-2026 Movatter.jp