#[repr(transparent)]pub struct Text(_);
Expand description
A shared data type used for collaborative text editing. It enables multiple users to add and remove chunks of text in efficient manner. This type is internally represented as a mutable double-linked list of text chunks - an optimization occurs during Transaction::commit, which allows to squash multiple consecutively inserted characters together as a single chunk of text even between transaction boundaries in order to preserve more efficient memory model.
Text structure internally uses UTF-8 encoding and its length is described in a number of bytes rather than individual characters (a single UTF-8 code point can consist of many bytes).
Like all Yrs shared data types, Text is resistant to the problem of interleaving (situation when characters inserted one after another may interleave with other peers concurrent inserts after merging all updates together). In case of Yrs conflict resolution is solved by using unique document id to determine correct and consistent ordering.
Implementations
sourceimpl Text
impl Text
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Converts context of this text data structure into a single string value.
sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Returns a number of characters visible in a current text data structure.
sourcepub fn insert(&self, txn: &mut Transaction, index: u32, chunk: &str)
pub fn insert(&self, txn: &mut Transaction, index: u32, chunk: &str)
Inserts a chunk
of text at a given index
.
If index
is 0
, this chunk
will be inserted at the beginning of a current text.
If index
is equal to current data structure length, this chunk
will be appended at
the end of it.
This method will panic if provided index
is greater than the length of a current text.
sourcepub fn insert_with_attributes(
&self,
txn: &mut Transaction,
index: u32,
chunk: &str,
attributes: Attrs
)
pub fn insert_with_attributes(
&self,
txn: &mut Transaction,
index: u32,
chunk: &str,
attributes: Attrs
)
Inserts a chunk
of text at a given index
.
If index
is 0
, this chunk
will be inserted at the beginning of a current text.
If index
is equal to current data structure length, this chunk
will be appended at
the end of it.
Collection of supplied attributes
will be used to wrap provided text chunk
range with a
formatting blocks.
This method will panic if provided index
is greater than the length of a current text.
sourcepub fn insert_embed(&self, txn: &mut Transaction, index: u32, content: Any)
pub fn insert_embed(&self, txn: &mut Transaction, index: u32, content: Any)
Inserts an embed content
at a given index
.
If index
is 0
, this content
will be inserted at the beginning of a current text.
If index
is equal to current data structure length, this embed
will be appended at
the end of it.
This method will panic if provided index
is greater than the length of a current text.
sourcepub fn insert_embed_with_attributes(
&self,
txn: &mut Transaction,
index: u32,
embed: Any,
attributes: Attrs
)
pub fn insert_embed_with_attributes(
&self,
txn: &mut Transaction,
index: u32,
embed: Any,
attributes: Attrs
)
Inserts an embed content
of text at a given index
.
If index
is 0
, this content
will be inserted at the beginning of a current text.
If index
is equal to current data structure length, this chunk
will be appended at
the end of it.
Collection of supplied attributes
will be used to wrap provided text content
range with
a formatting blocks.
This method will panic if provided index
is greater than the length of a current text.
sourcepub fn push(&self, txn: &mut Transaction, chunk: &str)
pub fn push(&self, txn: &mut Transaction, chunk: &str)
Appends a given chunk
of text at the end of a current text structure.
sourcepub fn remove_range(&self, txn: &mut Transaction, index: u32, len: u32)
pub fn remove_range(&self, txn: &mut Transaction, index: u32, len: u32)
Removes up to a len
characters from a current text structure, starting at given index
.
This method panics in case when not all expected characters were removed (due to
insufficient number of characters to remove) or index
is outside of the bounds of text.
sourcepub fn format(
&self,
txn: &mut Transaction,
index: u32,
len: u32,
attributes: Attrs
)
pub fn format(
&self,
txn: &mut Transaction,
index: u32,
len: u32,
attributes: Attrs
)
Wraps an existing piece of text within a range described by index
-len
parameters with
formatting blocks containing provided attributes
metadata.
sourcepub fn observe<F>(&mut self, f: F) -> Subscription<TextEvent> where
F: Fn(&Transaction, &TextEvent) + 'static,
pub fn observe<F>(&mut self, f: F) -> Subscription<TextEvent> where
F: Fn(&Transaction, &TextEvent) + 'static,
Subscribes a given callback to be triggered whenever current text is changed. A callback is triggered whenever a transaction gets committed. This function does not trigger if changes have been observed by nested shared collections.
All text changes can be tracked by using TextEvent::delta method: keep in mind that delta contains collection of individual characters rather than strings.
Returns an [Observer] which, when dropped, will unsubscribe current callback.
sourcepub fn unobserve(&mut self, subscription_id: SubscriptionId)
pub fn unobserve(&mut self, subscription_id: SubscriptionId)
Unsubscribes a previously subscribed event callback identified by given subscription_id
.
pub fn diff(&self, txn: &mut Transaction) -> Vec<Diff>
sourcepub fn diff_range(
&self,
txn: &mut Transaction,
hi: Option<&Snapshot>,
lo: Option<&Snapshot>
) -> Vec<Diff>
pub fn diff_range(
&self,
txn: &mut Transaction,
hi: Option<&Snapshot>,
lo: Option<&Snapshot>
) -> Vec<Diff>
Returns the Delta representation of this YText type.
Trait Implementations
impl Eq for Text
impl StructuralEq for Text
impl StructuralPartialEq for Text
Auto Trait Implementations
impl !RefUnwindSafe for Text
impl !Send for Text
impl !Sync for Text
impl Unpin for Text
impl !UnwindSafe for Text
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> DeepObservable for T where
T: AsMut<Branch>,
impl<T> DeepObservable for T where
T: AsMut<Branch>,
sourcefn observe_deep<F>(&mut self, f: F) -> Subscription<Events> where
F: 'static + Fn(&Transaction, &Events),
fn observe_deep<F>(&mut self, f: F) -> Subscription<Events> where
F: 'static + Fn(&Transaction, &Events),
sourcefn unobserve_deep(&mut self, subscription_id: u32)
fn unobserve_deep(&mut self, subscription_id: u32)
Unobserves callback identified by subscription_id
(which can be obtained by consuming
Subscription using into
cast). Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more