Struct automerge::Automerge

source ·
pub struct Automerge { /* private fields */ }
Expand description

An automerge document which does not manage transactions for you.

§Creating, loading, merging and forking documents

A new document can be created with Self::new(), which will create a document with a random ActorId. Existing documents can be loaded with Self::load(), or Self::load_with().

If you have two documents and you want to merge the changes from one into the other you can use Self::merge() or Self::merge_and_log_patches().

If you have a document you want to split into two concurrent threads of execution you can use Self::fork(). If you want to split a document from ealier in its history you can use Self::fork_at().

§Reading values

Self implements ReadDoc, which provides methods for reading values from the document.

§Modifying a document (Transactions)

Automerge provides an interface for viewing and modifying automerge documents which does not manage transactions for you. To create changes you use either Automerge::transaction() or Automerge::transact() (or the _with variants).

§Sync

This type implements crate::sync::SyncDoc

Implementations§

source§

impl Automerge

source

pub fn new() -> Self

Create a new document with a random actor id.

source

pub fn is_empty(&self) -> bool

Whether this document has any operations

source

pub fn with_actor(self, actor: ActorId) -> Self

Set the actor id for this document.

source

pub fn set_actor(&mut self, actor: ActorId) -> &mut Self

Set the actor id for this document.

source

pub fn get_actor(&self) -> &ActorId

Get the current actor id of this document.

source

pub fn transaction(&mut self) -> Transaction<'_>

Start a transaction.

source

pub fn transaction_log_patches( &mut self, patch_log: PatchLog ) -> Transaction<'_>

Start a transaction which records changes in a PatchLog

source

pub fn transaction_at( &mut self, patch_log: PatchLog, heads: &[ChangeHash] ) -> Transaction<'_>

Start a transaction isolated at a given heads

source

pub fn transact<F, O, E>(&mut self, f: F) -> Result<O, E>
where F: FnOnce(&mut Transaction<'_>) -> Result<O, E>,

Run a transaction on this document in a closure, automatically handling commit or rollback afterwards.

source

pub fn transact_with<F, O, E, C>(&mut self, c: C, f: F) -> Result<O, E>
where F: FnOnce(&mut Transaction<'_>) -> Result<O, E>, C: FnOnce(&O) -> CommitOptions,

Like Self::transact() but with a function for generating the commit options.

source

pub fn transact_and_log_patches<F, O, E>( &mut self, text_rep: TextRepresentation, f: F ) -> Result<O, E>
where F: FnOnce(&mut Transaction<'_>) -> Result<O, E>,

Run a transaction on this document in a closure, collecting patches, automatically handling commit or rollback afterwards.

The collected patches are available in the return value of Transaction::commit()

source

pub fn transact_and_log_patches_with<F, O, E, C>( &mut self, text_rep: TextRepresentation, c: C, f: F ) -> Result<O, E>
where F: FnOnce(&mut Transaction<'_>) -> Result<O, E>, C: FnOnce(&O) -> CommitOptions,

Like Self::transact_and_log_patches() but with a function for generating the commit options

source

pub fn empty_commit(&mut self, opts: CommitOptions) -> ChangeHash

Generate an empty change

The main reason to do this is if you want to create a “merge commit”, which is a change that has all the current heads of the document as dependencies.

source

pub fn fork(&self) -> Self

Fork this document at the current point for use by a different actor.

This will create a new actor ID for the forked document

source

pub fn fork_at(&self, heads: &[ChangeHash]) -> Result<Self, AutomergeError>

Fork this document at the given heads

This will create a new actor ID for the forked document

source

pub fn load(data: &[u8]) -> Result<Self, AutomergeError>

Load a document.

source

pub fn load_unverified_heads(data: &[u8]) -> Result<Self, AutomergeError>

Load a document without verifying the head hashes

This is useful for debugging as it allows you to examine a corrupted document.

source

pub fn load_with( data: &[u8], on_error: OnPartialLoad, mode: VerificationMode, patch_log: &mut PatchLog ) -> Result<Self, AutomergeError>

👎Deprecated since 0.5.2: Use load_with_options instead

Load a document, with options

§Arguments
  • data - The data to load
  • on_error - What to do if the document is only partially loaded. This can happen if some prefix of data contains valid data.
  • mode - Whether to verify the head hashes after loading
  • patch_log - A PatchLog to log the changes required to materialize the current state of the document once loaded
source

pub fn load_with_options<'a, 'b>( data: &'a [u8], options: LoadOptions<'b> ) -> Result<Self, AutomergeError>

Load a document, with options

§Arguments
  • data - The data to load
  • options - The options to use when loading
source

pub fn make_patches(&self, patch_log: &mut PatchLog) -> Vec<Patch>

Create the patches from a PatchLog

See the documentation for PatchLog for more details on this

source

pub fn current_state(&self, text_rep: TextRepresentation) -> Vec<Patch>

Get a set of Patches which materialize the current state of the document

This is a convienence method for doc.diff(&[], current_heads)

source

pub fn load_incremental(&mut self, data: &[u8]) -> Result<usize, AutomergeError>

Load an incremental save of a document.

Unlike Self::load() this imports changes into an existing document. It will work with both the output of Self::save() and Self::save_after()

The return value is the number of ops which were applied, this is not useful and will change in future.

source

pub fn load_incremental_log_patches( &mut self, data: &[u8], patch_log: &mut PatchLog ) -> Result<usize, AutomergeError>

Like Self::load_incremental() but log the changes to the current state of the document to PatchLog

source

pub fn apply_changes( &mut self, changes: impl IntoIterator<Item = Change> ) -> Result<(), AutomergeError>

Apply changes to this document.

This is idempotent in the sense that if a change has already been applied it will be ignored.

source

pub fn apply_changes_log_patches<I: IntoIterator<Item = Change>>( &mut self, changes: I, patch_log: &mut PatchLog ) -> Result<(), AutomergeError>

Like Self::apply_changes() but log the resulting changes to the current state of the document to patch_log

source

pub fn merge( &mut self, other: &mut Self ) -> Result<Vec<ChangeHash>, AutomergeError>

Takes all the changes in other which are not in self and applies them

source

pub fn merge_and_log_patches( &mut self, other: &mut Self, patch_log: &mut PatchLog ) -> Result<Vec<ChangeHash>, AutomergeError>

Takes all the changes in other which are not in self and applies them whilst logging the resulting changes to the current state of the document to patch_log

source

pub fn save_with_options(&self, options: SaveOptions) -> Vec<u8>

Save the entirety of this document in a compact form.

source

pub fn save(&self) -> Vec<u8>

Save the entirety of this document in a compact form.

source

pub fn save_and_verify(&self) -> Result<Vec<u8>, AutomergeError>

Save the document and attempt to load it before returning - slow!

source

pub fn save_nocompress(&self) -> Vec<u8>

Save this document, but don’t run it through DEFLATE afterwards

source

pub fn save_after(&self, heads: &[ChangeHash]) -> Vec<u8>

Save the changes since the given heads

The output of this will not be a compressed document format, but a series of individual changes. This is useful if you know you have only made a small change since the last Self::save() and you want to immediately send it somewhere (e.g. you’ve inserted a single character in a text object).

source

pub fn get_last_local_change(&self) -> Option<&Change>

Get the last change this actor made to the document.

source

pub fn dump(&self)

source

pub fn diff( &self, before_heads: &[ChangeHash], after_heads: &[ChangeHash], text_rep: TextRepresentation ) -> Vec<Patch>

Create patches representing the change in the current state of the document between the before and after heads. If the arguments are reverse it will observe the same changes in the opposite order.

source

pub fn get_heads(&self) -> Vec<ChangeHash>

Get the heads of this document.

source

pub fn get_changes(&self, have_deps: &[ChangeHash]) -> Vec<&Change>

source

pub fn get_changes_added<'a>(&self, other: &'a Self) -> Vec<&'a Change>

Get changes in other that are not in self

source

pub fn hash_for_opid(&self, exid: &ExId) -> Option<ChangeHash>

Get the hash of the change that contains the given opid.

Returns None if the opid:

  • is the root object id
  • does not exist in this document
source

pub fn hydrate(&self, heads: Option<&[ChangeHash]>) -> Value

Trait Implementations§

source§

impl Clone for Automerge

source§

fn clone(&self) -> Automerge

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Automerge

source§

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

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

impl Default for Automerge

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl ReadDoc for Automerge

source§

fn parents<O: AsRef<ExId>>(&self, obj: O) -> Result<Parents<'_>, AutomergeError>

Get the parents of an object in the document tree. Read more
source§

fn parents_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Parents<'_>, AutomergeError>

Get the parents of the object obj as at heads Read more
source§

fn keys<O: AsRef<ExId>>(&self, obj: O) -> Keys<'_>

Get the keys of the object obj. Read more
source§

fn keys_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> Keys<'_>

Get the keys of the object obj as at heads Read more
source§

fn map_range<'a, O: AsRef<ExId>, R: RangeBounds<String> + 'a>( &'a self, obj: O, range: R ) -> MapRange<'a, R>

Iterate over the keys and values of the map obj in the given range. Read more
source§

fn map_range_at<'a, O: AsRef<ExId>, R: RangeBounds<String> + 'a>( &'a self, obj: O, range: R, heads: &[ChangeHash] ) -> MapRange<'a, R>

Iterate over the keys and values of the map obj in the given range as at heads Read more
source§

fn list_range<O: AsRef<ExId>, R: RangeBounds<usize>>( &self, obj: O, range: R ) -> ListRange<'_, R>

Iterate over the indexes and values of the list or text obj in the given range. Read more
source§

fn list_range_at<O: AsRef<ExId>, R: RangeBounds<usize>>( &self, obj: O, range: R, heads: &[ChangeHash] ) -> ListRange<'_, R>

Iterate over the indexes and values of the list or text obj in the given range as at heads Read more
source§

fn values<O: AsRef<ExId>>(&self, obj: O) -> Values<'_>

Iterate over the values in a map, list, or text object Read more
source§

fn values_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> Values<'_>

Iterate over the values in a map, list, or text object as at heads Read more
source§

fn length<O: AsRef<ExId>>(&self, obj: O) -> usize

Get the length of the given object. Read more
source§

fn length_at<O: AsRef<ExId>>(&self, obj: O, heads: &[ChangeHash]) -> usize

Get the length of the given object as at heads Read more
source§

fn text<O: AsRef<ExId>>(&self, obj: O) -> Result<String, AutomergeError>

Get the string represented by the given text object.
source§

fn spans<O: AsRef<ExId>>(&self, obj: O) -> Result<Spans<'_>, AutomergeError>

Return the sequence of text and block markers in the text object obj
source§

fn spans_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Spans<'_>, AutomergeError>

Return the sequence of text and block markers in the text object obj as at heads
source§

fn get_cursor<O: AsRef<ExId>>( &self, obj: O, position: usize, at: Option<&[ChangeHash]> ) -> Result<Cursor, AutomergeError>

Obtain the stable address (Cursor) for a usize position in a Sequence (either ObjType::List or ObjType::Text). Read more
source§

fn get_cursor_position<O: AsRef<ExId>>( &self, obj: O, cursor: &Cursor, at: Option<&[ChangeHash]> ) -> Result<usize, AutomergeError>

Translate Cursor in a Sequence into an absolute position of type usize. Read more
source§

fn text_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<String, AutomergeError>

Get the string represented by the given text object as at heads, see Self::text()
source§

fn marks<O: AsRef<ExId>>(&self, obj: O) -> Result<Vec<Mark<'_>>, AutomergeError>

Get all marks on a current sequence
source§

fn marks_at<O: AsRef<ExId>>( &self, obj: O, heads: &[ChangeHash] ) -> Result<Vec<Mark<'_>>, AutomergeError>

Get all marks on a sequence at a given heads
source§

fn hydrate<O: AsRef<ExId>>( &self, obj: O, heads: Option<&[ChangeHash]> ) -> Result<Value, AutomergeError>

source§

fn get_marks<O: AsRef<ExId>>( &self, obj: O, index: usize, heads: Option<&[ChangeHash]> ) -> Result<MarkSet, AutomergeError>

source§

fn get<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P ) -> Result<Option<(Value<'_>, ExId)>, AutomergeError>

Get a value out of the document. Read more
source§

fn get_at<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P, heads: &[ChangeHash] ) -> Result<Option<(Value<'_>, ExId)>, AutomergeError>

Get the value of the given key as at heads, see Self::get()
source§

fn get_all<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P ) -> Result<Vec<(Value<'_>, ExId)>, AutomergeError>

Get all conflicting values out of the document at this prop that conflict. Read more
source§

fn get_all_at<O: AsRef<ExId>, P: Into<Prop>>( &self, obj: O, prop: P, heads: &[ChangeHash] ) -> Result<Vec<(Value<'_>, ExId)>, AutomergeError>

Get all possibly conflicting values for a key as at heads Read more
source§

fn object_type<O: AsRef<ExId>>(&self, obj: O) -> Result<ObjType, AutomergeError>

Get the type of this object, if it is an object.
source§

fn get_missing_deps(&self, heads: &[ChangeHash]) -> Vec<ChangeHash>

Get the hashes of the changes in this document that aren’t transitive dependencies of the given heads.
source§

fn get_change_by_hash(&self, hash: &ChangeHash) -> Option<&Change>

Get a change by its hash.
source§

impl SyncDoc for Automerge

source§

fn generate_sync_message(&self, sync_state: &mut State) -> Option<Message>

Generate a sync message for the remote peer represented by sync_state Read more
source§

fn receive_sync_message( &mut self, sync_state: &mut State, message: Message ) -> Result<(), AutomergeError>

Apply a received sync message to this document and sync_state
source§

fn receive_sync_message_log_patches( &mut self, sync_state: &mut State, message: Message, patch_log: &mut PatchLog ) -> Result<(), AutomergeError>

Apply a received sync message to this document and sync_state, logging any changes that are made to patch_log 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
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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

§

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

§

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

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more