Skip to main content

Cursor

Struct Cursor 

Source
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>

Source

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);
});
Source

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));
Source

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);
});
Source

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());
});
Source

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);
});
Source

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);
});
Source

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]);
});
Source

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§

Source§

impl<T> Debug for Cursor<'_, '_, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>
where T: Send + Sync,

§

impl<'a, 't, T> Sync for Cursor<'a, 't, T>
where T: Send + Sync,

§

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> 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> 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.