Development Documentation (main branch) - For stable release docs, see docs.rs/eidetica

Doc

Struct Doc 

pub struct Doc { /* private fields */ }
Expand description

A Yrs document type. Documents are the most important units of collaborative resources management. All shared collections live within a scope of their corresponding documents. All updates are generated on per-document basis (rather than individual shared type). All operations on shared collections happen via Transaction, which lifetime is also bound to a document.

Document manages so-called root types, which are top-level shared types definitions (as opposed to recursively nested types).

§Example

use yrs::{Doc, ReadTxn, StateVector, Text, Transact, Update};
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;

let doc = Doc::new();
let root = doc.get_or_insert_text("root-type-name");
let mut txn = doc.transact_mut(); // all Yrs operations happen in scope of a transaction
root.push(&mut txn, "hello world"); // append text to our collaborative document

// in order to exchange data with other documents we first need to create a state vector
let remote_doc = Doc::new();
let mut remote_txn = remote_doc.transact_mut();
let state_vector = remote_txn.state_vector().encode_v1();

// now compute a differential update based on remote document's state vector
let update = txn.encode_diff_v1(&StateVector::decode_v1(&state_vector).unwrap());

// both update and state vector are serializable, we can pass the over the wire
// now apply update to a remote document
remote_txn.apply_update(Update::decode_v1(update.as_slice()).unwrap());

Implementations§

§

impl Doc

pub fn new() -> Doc

Creates a new document with a randomized client identifier.

pub fn with_client_id(client_id: u64) -> Doc

Creates a new document with a specified client_id. It’s up to a caller to guarantee that this identifier is unique across all communicating replicas of that document.

pub fn with_options(options: Options) -> Doc

Creates a new document with a configured set of Options.

pub fn client_id(&self) -> u64

A unique client identifier, that’s also a unique identifier of current document replica and it’s subdocuments.

Default: randomly generated.

pub fn guid(&self) -> Arc<str>

A globally unique identifier, that’s also a unique identifier of current document replica, and unlike Doc::client_id it’s not shared with its subdocuments.

Default: randomly generated UUID v4.

pub fn collection_id(&self) -> Option<Arc<str>>

Returns a unique collection identifier, if defined.

Default: None.

pub fn skip_gc(&self) -> bool

Informs if current document is skipping garbage collection on deleted collections on transaction commit.

Default: false.

pub fn auto_load(&self) -> bool

If current document is subdocument, it will automatically for a document to load.

Default: false.

pub fn should_load(&self) -> bool

Whether the document should be synced by the provider now. This is toggled to true when you call Doc::load

Default value: true.

pub fn offset_kind(&self) -> OffsetKind

Returns encoding used to count offsets and lengths in text operations.

pub fn get_or_insert_text<N>(&self, name: N) -> TextRef
where N: Into<Arc<str>>,

Returns a TextRef data structure stored under a given name. Text structures are used for collaborative text editing: they expose operations to append and remove chunks of text, which are free to execute concurrently by multiple peers over remote boundaries.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a text (in such case a sequence component of complex data type will be interpreted as a list of text chunks).

§Panics

This method requires exclusive access to an underlying document store. If there is another transaction in process, it will panic. It’s advised to define all root shared types during the document creation.

pub fn get_or_insert_map<N>(&self, name: N) -> MapRef
where N: Into<Arc<str>>,

Returns a MapRef data structure stored under a given name. Maps are used to store key-value pairs associated. These values can be primitive data (similar but not limited to a JavaScript Object Notation) as well as other shared types (Yrs maps, arrays, text structures etc.), enabling to construct a complex recursive tree structures.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a map (in such case a map component of complex data type will be interpreted as native map).

§Panics

This method requires exclusive access to an underlying document store. If there is another transaction in process, it will panic. It’s advised to define all root shared types during the document creation.

pub fn get_or_insert_array<N>(&self, name: N) -> ArrayRef
where N: Into<Arc<str>>,

Returns an ArrayRef data structure stored under a given name. Array structures are used for storing a sequences of elements in ordered manner, positioning given element accordingly to its index.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as an array (in such case a sequence component of complex data type will be interpreted as a list of inserted values).

§Panics

This method requires exclusive access to an underlying document store. If there is another transaction in process, it will panic. It’s advised to define all root shared types during the document creation.

pub fn get_or_insert_xml_fragment<N>(&self, name: N) -> XmlFragmentRef
where N: Into<Arc<str>>,

Returns a XmlFragmentRef data structure stored under a given name. XML elements represent nodes of XML document. They can contain attributes (key-value pairs, both of string type) and other nested XML elements or text values, which are stored in their insertion order.

If no structure under defined name existed before, it will be created and returned instead.

If a structure under defined name already existed, but its type was different it will be reinterpreted as a XML element (in such case a map component of complex data type will be interpreted as map of its attributes, while a sequence component - as a list of its child XML nodes).

§Panics

This method requires exclusive access to an underlying document store. If there is another transaction in process, it will panic. It’s advised to define all root shared types during the document creation.

pub fn observe_update_v1<F>( &self, f: F, ) -> Result<Arc<dyn Drop>, TransactionAcqError>
where F: Fn(&TransactionMut<'_>, &UpdateEvent) + 'static,

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v1 encoding and can be decoded using [Update::decode_v1] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Returns a subscription, which will unsubscribe function when dropped.

pub fn observe_update_v1_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&TransactionMut<'_>, &UpdateEvent) + 'static,

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v1 encoding and can be decoded using [Update::decode_v1] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Provided key will be used to identify a subscription, which will be used to unsubscribe.

pub fn unobserve_update_v1<K>( &self, key: K, ) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_update_v2<F>( &self, f: F, ) -> Result<Arc<dyn Drop>, TransactionAcqError>
where F: Fn(&TransactionMut<'_>, &UpdateEvent) + 'static,

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v2 encoding and can be decoded using [Update::decode_v2] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Returns a subscription, which will unsubscribe function when dropped.

pub fn observe_update_v2_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&TransactionMut<'_>, &UpdateEvent) + 'static,

Subscribe callback function for any changes performed within transaction scope. These changes are encoded using lib0 v2 encoding and can be decoded using [Update::decode_v2] if necessary or passed to remote peers right away. This callback is triggered on function commit.

Provided key will be used to identify a subscription, which will be used to unsubscribe.

pub fn unobserve_update_v2<K>( &self, key: K, ) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_transaction_cleanup<F>( &self, f: F, ) -> Result<Arc<dyn Drop>, TransactionAcqError>
where F: Fn(&TransactionMut<'_>, &TransactionCleanupEvent) + 'static,

Subscribe callback function to updates on the Doc. The callback will receive state updates and deletions when a document transaction is committed.

pub fn observe_transaction_cleanup_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&TransactionMut<'_>, &TransactionCleanupEvent) + 'static,

Subscribe callback function to updates on the Doc. The callback will receive state updates and deletions when a document transaction is committed.

pub fn unobserve_transaction_cleanup<K>( &self, key: K, ) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_after_transaction_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&mut TransactionMut<'_>) + 'static,

pub fn unobserve_after_transaction<K>( &self, key: K, ) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_subdocs<F>( &self, f: F, ) -> Result<Arc<dyn Drop>, TransactionAcqError>
where F: Fn(&TransactionMut<'_>, &SubdocsEvent) + 'static,

Subscribe callback function, that will be called whenever a subdocuments inserted in this Doc will request a load.

pub fn observe_subdocs_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&TransactionMut<'_>, &SubdocsEvent) + 'static,

Subscribe callback function, that will be called whenever a subdocuments inserted in this Doc will request a load.

pub fn unobserve_subdocs<K>(&self, key: K) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_destroy<F>( &self, f: F, ) -> Result<Arc<dyn Drop>, TransactionAcqError>
where F: Fn(&TransactionMut<'_>, &Doc) + 'static,

Subscribe callback function, that will be called whenever a [DocRef::destroy] has been called.

pub fn unobserve_destroy<K>(&self, key: K) -> Result<bool, TransactionAcqError>
where K: Into<Origin>,

pub fn observe_destroy_with<K, F>( &self, key: K, f: F, ) -> Result<(), TransactionAcqError>
where K: Into<Origin>, F: Fn(&TransactionMut<'_>, &Doc) + 'static,

Subscribe callback function, that will be called whenever a [DocRef::destroy] has been called.

pub fn load<T>(&self, parent_txn: &mut T)
where T: WriteTxn,

Sends a load request to a parent document. Works only if current document is a sub-document of a document.

pub fn destroy<T>(&self, parent_txn: &mut T)
where T: WriteTxn,

Starts destroy procedure for a current document, triggering an “destroy” callback and invalidating all event callback subscriptions.

pub fn parent_doc(&self) -> Option<Doc>

If current document has been inserted as a sub-document, returns a reference to a parent document, which contains it.

pub fn branch_id(&self) -> Option<BranchID>

pub fn ptr_eq(a: &Doc, b: &Doc) -> bool

Trait Implementations§

§

impl<'doc> AsyncTransact<'doc> for Doc

§

type Read = AcquireTransaction<'doc>

§

type Write = AcquireTransactionMut<'doc>

§

fn transact(&'doc self) -> <Doc as AsyncTransact<'doc>>::Read

§

fn transact_mut(&'doc self) -> <Doc as AsyncTransact<'doc>>::Write

§

fn transact_mut_with<T>( &'doc self, origin: T, ) -> <Doc as AsyncTransact<'doc>>::Write
where T: Into<Origin>,

Creates and returns a read-write capable transaction with an origin classifier attached. This transaction can be used to mutate the contents of underlying document store and upon dropping or committing it may subscription callbacks. Read more
§

impl Clone for Doc

§

fn clone(&self) -> Doc

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Doc

§

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

Formats the value using the given formatter. Read more
§

impl Default for Doc

§

fn default() -> Doc

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

impl Display for Doc

§

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

Formats the value using the given formatter. Read more
§

impl PartialEq for Doc

§

fn eq(&self, other: &Doc) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Prelim for Doc

§

type Return = Doc

Type of a value to be returned as a result of inserting this Prelim type instance. Use Unused if none is necessary.
§

fn into_content( self, txn: &mut TransactionMut<'_>, ) -> (ItemContent, Option<Doc>)

This method is used to create initial content required in order to create a block item. A supplied ptr can be used to identify block that is about to be created to store the returned content. Read more
§

fn integrate(self, _txn: &mut TransactionMut<'_>, _inner_ref: BranchPtr)

Method called once an original item filled with content from Self::into_content has been added to block store. This method is used by complex types such as maps or arrays to append the original contents of prelim struct into YMap, YArray etc.
§

impl ToJson for Doc

§

fn to_json<T>(&self, txn: &T) -> Any
where T: ReadTxn,

Converts all contents of a current type into a JSON-like representation.
§

impl Transact for Doc

§

fn try_transact(&self) -> Result<Transaction<'_>, TransactionAcqError>

Creates and returns a lightweight read-only transaction. Read more
§

fn try_transact_mut(&self) -> Result<TransactionMut<'_>, TransactionAcqError>

Creates and returns a read-write capable transaction. This transaction can be used to mutate the contents of underlying document store and upon dropping or committing it may subscription callbacks. Read more
§

fn try_transact_mut_with<T>( &self, origin: T, ) -> Result<TransactionMut<'_>, TransactionAcqError>
where T: Into<Origin>,

Creates and returns a read-write capable transaction with an origin classifier attached. This transaction can be used to mutate the contents of underlying document store and upon dropping or committing it may subscription callbacks. Read more
§

fn transact_mut_with<T>(&self, origin: T) -> TransactionMut<'_>
where T: Into<Origin>,

Creates and returns a read-write capable transaction with an origin classifier attached. This transaction can be used to mutate the contents of underlying document store and upon dropping or committing it may subscription callbacks. Read more
§

fn transact(&self) -> Transaction<'_>

Creates and returns a lightweight read-only transaction. Read more
§

fn transact_mut(&self) -> TransactionMut<'_>

Creates and returns a read-write capable transaction. This transaction can be used to mutate the contents of underlying document store and upon dropping or committing it may subscription callbacks. Read more
§

impl TryFrom<ItemPtr> for Doc

§

type Error = ItemPtr

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

fn try_from(item: ItemPtr) -> Result<Doc, <Doc as TryFrom<ItemPtr>>::Error>

Performs the conversion.
§

impl TryFrom<Out> for Doc

§

type Error = Out

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

fn try_from(value: Out) -> Result<Doc, <Doc as TryFrom<Out>>::Error>

Performs the conversion.
§

impl Send for Doc

§

impl Sync for Doc

Auto Trait Implementations§

§

impl Freeze for Doc

§

impl !RefUnwindSafe for Doc

§

impl Unpin for Doc

§

impl !UnwindSafe for Doc

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

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> ToStringFallible for T
where T: Display,

§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<M> Meta for M
where M: Default,