Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction { /* private fields */ }
Expand description

A batch of edits, validated together and applied as a unit.

Opened against a model snapshot and carrying that snapshot’s revision, so a commit against a model that has since changed is refused rather than applied to state the caller never saw.

Implementations§

Source§

impl Transaction

Source

pub fn new(model: &Model) -> Self

Open a transaction against a model.

Source

pub fn revision(&self) -> u64

The model revision this transaction was opened against.

Source

pub fn edits(&self) -> &[Edit]

The staged edits, in the order they were added. Truncations are not edits: see Transaction::truncations.

Source

pub fn is_empty(&self) -> bool

Whether anything is staged.

Source

pub fn len(&self) -> usize

Number of staged edits; truncations and the header are not counted.

Source

pub fn create(&mut self, entity: Entity) -> EntityId

Stage a new entity, returning the id reserved for it.

The id is allocated from the transaction, not the model, so several creates in one batch cannot collide and the caller can reference a newly created entity from another edit in the same transaction before anything is written.

Source

pub fn stage(&mut self, edit: Edit) -> &mut Self

Stage an already-constructed edit.

Transaction::create allocates ids and is what an author should use. This exists for replaying a batch that was built elsewhere – deserialized, or produced by another process – where the edit list is data rather than a sequence of calls. Preflight checks it exactly the same way, so a replayed batch cannot bypass validation.

Source

pub fn set_attribute( &mut self, id: EntityId, slot: usize, value: Value, ) -> &mut Self

Stage an attribute write.

Source

pub fn retype( &mut self, id: EntityId, type_name: impl Into<Arc<str>>, ) -> &mut Self

Stage a type change.

Source

pub fn remove(&mut self, id: EntityId) -> &mut Self

Stage a removal.

Source

pub fn set_header(&mut self, header: Header) -> &mut Self

Stage a replacement of the file header, applied after the edits.

Source

pub fn truncate(&mut self, id: EntityId, len: usize) -> &mut Self

Stage shortening id’s record to its first len attributes, applied after every edit and before the header. Staging id again replaces the length.

The record may be one the batch creates. A len at or past the record’s length at that point drops nothing. A Edit::SetAttribute at a slot of len or more is refused (Conflict::WriteBeyondTruncation); references held only in dropped slots do not keep a removed entity alive.

Source

pub fn truncations(&self) -> impl Iterator<Item = (EntityId, usize)> + '_

The staged truncations as (id, len), in ascending id.

Source

pub fn header(&self) -> Option<&Header>

The staged header replacement, if any.

Source

pub fn preflight(&self, model: &Model) -> Vec<Conflict>

Check every edit without touching the model.

Returns every conflict found rather than the first: an author fixing a batch wants the whole list, and stopping at the first turns one review into N round trips.

Conflicts are ordered by the edit that produced them, so the report reads in the order the caller wrote the batch.

Source

pub fn commit(self, model: &mut Model) -> Result<Applied, Vec<Conflict>>

Validate and apply, or report every conflict and change nothing.

On Err the model is untouched: preflight runs to completion before the first write, so a rejected transaction cannot leave partial state.

Source

pub fn commit_reversible( self, model: &mut Model, ) -> Result<(Applied, Inverse), Vec<Conflict>>

Transaction::commit, also returning the Inverse that restores the model exactly. A refused commit changes nothing and returns the conflicts only.

Trait Implementations§

Source§

impl Clone for Transaction

Source§

fn clone(&self) -> Transaction

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Transaction

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_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§

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.