pub struct Cursor<'a, 't, T> { /* private fields */ }Expand description
A read-only cursor over a RecCell.
See RecCell::cursor for details.
Implementations§
Source§impl<'a, 't, T> Cursor<'a, 't, T>
impl<'a, 't, T> Cursor<'a, 't, T>
Sourcepub const fn into_inner(self) -> (&'a RecCell<'t, T>, &'a RecToken<'t>)
pub const fn into_inner(self) -> (&'a RecCell<'t, T>, &'a RecToken<'t>)
Returns the anchored cell and the token borrow held by this cursor.
§Examples
use rec_cell::{RecCell, RecToken};
RecToken::new(|token| {
let cell = RecCell::new(1);
let (cell, token) = cell.cursor(&token).into_inner();
assert_eq!(cell.borrow(token), &1);
});Sourcepub const fn get(&self) -> &T
pub const fn get(&self) -> &T
Returns the value at the cursor’s current cell.
§Examples
use rec_cell::{RecCell, RecToken};
RecToken::new(|token| assert_eq!(RecCell::new(1).cursor(&token).get(), &1));Sourcepub fn map<C: CellLike<'t>>(self, f: impl FnOnce(&T) -> &'a C) -> C::Cursor<'a>
pub fn map<C: CellLike<'t>>(self, f: impl FnOnce(&T) -> &'a C) -> C::Cursor<'a>
Consumes this cursor and maps its current value to another cell shape.
§Examples
use rec_cell::{RecCell, RecToken};
struct PointsToNext<'a, 't>(&'a RecCell<'t, i32>);
RecToken::new(|token| {
let next = RecCell::new(2);
let cell = RecCell::new(PointsToNext(&next));
assert_eq!(cell.cursor(&token).map(|next| next.0).get(), &2);
});Sourcepub fn try_map<C: CellLike<'t>, E>(
self,
f: impl FnOnce(&T) -> Result<&'a C, E>,
) -> Result<C::Cursor<'a>, E>
pub fn try_map<C: CellLike<'t>, E>( self, f: impl FnOnce(&T) -> Result<&'a C, E>, ) -> Result<C::Cursor<'a>, E>
Fallible version of Self::map.
§Examples
use rec_cell::{RecCell, RecToken};
struct PointsToNext<'a, 't>(Option<&'a RecCell<'t, i32>>);
RecToken::new(|token| {
let next = RecCell::new(2);
let cell = RecCell::new(PointsToNext(Some(&next)));
let mapped = cell.cursor(&token).try_map(|next| next.0.ok_or(()));
assert_eq!(mapped.unwrap().get(), &2);
let cell = RecCell::new(PointsToNext(None));
let mapped = cell.cursor(&token).try_map(|next| next.0.ok_or(()));
assert!(mapped.is_err());
});Sourcepub fn update(&mut self, f: impl FnOnce(&T) -> &'a RecCell<'t, T>)
pub fn update(&mut self, f: impl FnOnce(&T) -> &'a RecCell<'t, T>)
Updates this cursor to a cell selected from its current value.
§Examples
use rec_cell::{RecCell, RecToken};
use std::mem::MaybeUninit;
struct PointsToNext<'a, 't>(i32, &'a RecCell<'t, Self>);
RecToken::new(|token| {
let [mut a, mut b] = [const { MaybeUninit::<PointsToNext>::uninit() }; 2];
let ([a, b], token) = RecCell::new_cyclic(&mut a, token, |a| {
let b_value = PointsToNext(20, a);
let b = RecCell::from_mut(b.write(b_value));
let a_value = PointsToNext(10, b);
([a, b], a_value)
});
let mut cursor = a.cursor(&token);
assert_eq!(cursor.get().0, 10);
cursor.update(|v| v.1);
assert_eq!(cursor.get().0, 20);
cursor.update(|v| v.1);
assert_eq!(cursor.get().0, 10);
});Sourcepub fn try_update<E>(
&mut self,
f: impl FnOnce(&T) -> Result<&'a RecCell<'t, T>, E>,
) -> Result<(), E>
pub fn try_update<E>( &mut self, f: impl FnOnce(&T) -> Result<&'a RecCell<'t, T>, E>, ) -> Result<(), E>
Fallible version of Self::update.
§Examples
use rec_cell::{RecCell, RecToken};
use std::mem::MaybeUninit;
struct PointsToNext<'a, 't>(i32, Option<&'a RecCell<'t, Self>>);
RecToken::new(|token| {
let [mut a, mut b] = [const { MaybeUninit::<PointsToNext>::uninit() }; 2];
let b = RecCell::from_mut(b.write(PointsToNext(20, None)));
let a = RecCell::from_mut(a.write(PointsToNext(10, Some(&b))));
let mut cursor = a.cursor(&token);
assert_eq!(cursor.get().0, 10);
cursor.try_update(|v| v.1.ok_or(())).unwrap();
assert_eq!(cursor.get().0, 20);
// A failed update leaves the cursor unchanged.
cursor.try_update(|v| v.1.ok_or(())).unwrap_err();
assert_eq!(cursor.get().0, 20);
});Sourcepub fn map_cell<C: CellLike<'t>>(
self,
f: impl FnOnce(&'a RecCell<'t, T>) -> &'a C,
) -> C::Cursor<'a>
pub fn map_cell<C: CellLike<'t>>( self, f: impl FnOnce(&'a RecCell<'t, T>) -> &'a C, ) -> C::Cursor<'a>
Maps the current cell without borrowing its value.
§Examples
use rec_cell::{RecCell, RecToken};
RecToken::new(|token| {
let cell = RecCell::new(1);
// Construct a &[RecCell<i32>; 1] from a &RecCell<i32>.
// This does not require reading the value.
// The CellLike trait makes the result a cursor on [i32; 1].
let array_cursor = cell.cursor(&token).map_cell(core::array::from_ref);
assert_eq!(array_cursor.get(), &[1]);
});Sourcepub fn try_map_cell<C: CellLike<'t>, E>(
self,
f: impl FnOnce(&'a RecCell<'t, T>) -> Result<&'a C, E>,
) -> Result<C::Cursor<'a>, E>
pub fn try_map_cell<C: CellLike<'t>, E>( self, f: impl FnOnce(&'a RecCell<'t, T>) -> Result<&'a C, E>, ) -> Result<C::Cursor<'a>, E>
Fallible version of Self::map_cell.
§Examples
use rec_cell::{RecCell, RecToken};
RecToken::new(|token| {
let cell = RecCell::new(1);
let next = RecCell::new(2);
let cursor = cell.cursor(&token).try_map_cell(|_| Ok::<_, ()>(&next)).unwrap();
assert_eq!(cursor.get(), &2);
});Trait Implementations§
Auto Trait Implementations§
impl<'a, 't, T> !RefUnwindSafe for Cursor<'a, 't, T>
impl<'a, 't, T> !UnwindSafe for Cursor<'a, 't, T>
impl<'a, 't, T> Freeze for Cursor<'a, 't, T>
impl<'a, 't, T> Send for Cursor<'a, 't, T>
impl<'a, 't, T> Sync for Cursor<'a, 't, T>
impl<'a, 't, T> Unpin for Cursor<'a, 't, T>
impl<'a, 't, T> UnsafeUnpin for Cursor<'a, 't, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more