Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction<'a, D: Clone, X: Clone = ()> { /* private fields */ }
Expand description

A compound edit in progress (Store::begin_transaction); derefs to the Store.

While it is open the store’s history is a scratch one holding only the transaction’s own steps (so undo inside it goes back no further than its start, and limits never push out an older step); the real history is set aside untouched. Transaction::commit puts it back and records one step label from the state at the start to the current one, or nothing if the revision is the one at the start. Dropping it uncommitted puts back the state at the start exactly: document, extra state, revision, history, merge key, saved revision.

Transactions nest: an inner one commits as one step into the outer one’s scratch history (folded again by the outer commit) and rolls back only its own edits (a savepoint).

Implementations§

Source§

impl<D: Clone, X: Clone> Transaction<'_, D, X>

Source

pub fn commit(self)

Fold the transaction’s edits into one undo step (none if nothing changed).

Source

pub fn rollback(self)

Put everything back as it was at the start (the same as dropping it).

Source

pub fn changed(&self) -> bool

Something changed since the start (a commit would record a step).

Methods from Deref<Target = Store<D, X>>§

Source

pub fn doc(&self) -> &Arc<D> ⓘ

The current document.

Source

pub fn snapshot(&self) -> Arc<D> ⓘ

The current document, to keep or send to another thread (an Arc clone).

Source

pub fn revision(&self) -> Revision

Source

pub fn extra(&self) -> &X

Source

pub fn extra_mut(&mut self) -> &mut X

Change the extra state without making an undo step.

Source

pub fn history(&self) -> &History<D, X>

Source

pub fn history_mut(&mut self) -> &mut History<D, X>

The history, to change its limits or squash steps.

Source

pub fn edit<R>(&mut self, label: &str, f: impl FnOnce(&mut D) -> R) -> R

Apply an undoable edit called label.

Source

pub fn try_edit<R, E>( &mut self, label: &str, f: impl FnOnce(&mut D) -> Result<R, E>, ) -> Result<R, E>

Apply an edit that may fail; on error (or a panic in f) nothing changes.

Source

pub fn edit_merged<R>( &mut self, label: &str, merge_key: &str, f: impl FnOnce(&mut D) -> R, ) -> R

Store::edit that folds into the previous step when that step has the same label and was made with the same merge_key with nothing in between (one undo step per drag).

Source

pub fn try_edit_with<R, E>( &mut self, label: &str, merge_key: Option<&str>, f: impl FnOnce(&mut D, &mut X) -> Result<R, E>, ) -> Result<R, E>

The general edit: optional merge key, access to the extra state, may fail. On error (or a panic in f) the document, the extra state, the revision and the history are unchanged.

Source

pub fn commit(&mut self, label: &str, merge_key: Option<&str>, doc: Arc<D>)

Make doc, built elsewhere (a command’s working copy, a job’s result), the new state as one undo step label, without copying it. merge_key as in Store::edit_merged. Committing the current snapshot itself (Arc::ptr_eq) changes nothing.

Source

pub fn transaction<T, E>( &mut self, label: &str, f: impl FnOnce(&mut Store<D, X>) -> Result<T, E>, ) -> Result<T, E>

Run f, which may make any number of edits, as one undo step called label.

On Ok the edits fold into one step (none when they changed nothing); the steps before the transaction are untouched and the redo line is dropped only if something changed. On Err, or a panic in f, the document, extra state, revision, history (redo line and merge key included) and saved state are put back as they were. See Transaction.

Source

pub fn begin_transaction(&mut self, label: &str) -> Transaction<'_, D, X>

Start a Transaction called label: edit through it, then Transaction::commit; dropping it without committing (an early return, a panic) rolls everything back.

Source

pub fn undo(&mut self) -> Option<String>

Undo the last step; returns its label.

Source

pub fn redo(&mut self) -> Option<String>

Redo the next undone step; returns its label.

Source

pub fn redo_branch(&mut self, index: usize) -> Option<String>

Redo into branch index of History::branches (it must grow from the current state).

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

pub fn replace(&mut self, doc: D, extra: X)

Replace the whole document (New, Open, Revert): clears the history, counts as saved.

Source

pub fn is_dirty(&self) -> bool

The document differs from the last saved (or loaded) state.

Source

pub fn mark_saved(&mut self)

The current state was saved.

Source

pub fn mark_saved_at(&mut self, revision: Revision)

Record that the state with revision was saved (a background save of an older snapshot).

Source

pub fn saved_revision(&self) -> Revision

Source

pub fn subscribe_snapshots(&mut self) -> Arc<SnapshotCell<D>> ⓘ

A cell other threads read the latest snapshot from without locking; the store publishes every change to it. The same cell is returned on every call.

Trait Implementations§

Source§

impl<D: Clone + Debug, X: Clone + Debug> Debug for Transaction<'_, D, X>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<D: Clone, X: Clone> Deref for Transaction<'_, D, X>

Source§

type Target = Store<D, X>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Store<D, X>

Dereferences the value.
Source§

impl<D: Clone, X: Clone> DerefMut for Transaction<'_, D, X>

Source§

fn deref_mut(&mut self) -> &mut Store<D, X>

Mutably dereferences the value.
Source§

impl<D: Clone, X: Clone> Drop for Transaction<'_, D, X>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a, D, X = ()> !RefUnwindSafe for Transaction<'a, D, X>

§

impl<'a, D, X = ()> !UnwindSafe for Transaction<'a, D, X>

§

impl<'a, D, X> Forget for Transaction<'a, D, X>

§

impl<'a, D, X> Freeze for Transaction<'a, D, X>
where &'a mut Store<D, X>: Freeze, Option<Scope<D, X>>: Freeze,

§

impl<'a, D, X> Move for Transaction<'a, D, X>

§

impl<'a, D, X> Send for Transaction<'a, D, X>
where &'a mut Store<D, X>: Send, Option<Scope<D, X>>: Send,

§

impl<'a, D, X> Sync for Transaction<'a, D, X>
where &'a mut Store<D, X>: Sync, Option<Scope<D, X>>: Sync,

§

impl<'a, D, X> Unpin for Transaction<'a, D, X>
where &'a mut Store<D, X>: Unpin, Option<Scope<D, X>>: Unpin,

§

impl<'a, D, X> UnsafeUnpin for Transaction<'a, D, X>
where &'a mut Store<D, X>: UnsafeUnpin, Option<Scope<D, X>>: UnsafeUnpin,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.