Struct cola::Text

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

A range of text inserted into a Replica.

Despite the name, this type does not contain the text string itself, only the ReplicaId of the Replica that inserted it and its temporal range in it. These can be accessed via the inserted_by and temporal_range methods respectively.

Implementations§

source§

impl Text

source

pub fn inserted_by(&self) -> ReplicaId

Returns the ReplicaId of the Replica that inserted this text.

Examples
let mut replica1 = Replica::new(1, 0);

let insertion = replica1.inserted(0, 1);

assert_eq!(insertion.text().inserted_by(), replica1.id());
source

pub fn temporal_range(&self) -> Range<Length>

Returns the temporal range of this text in the Replica that inserted it.

Each Replica keeps an internal character clock that starts at zero and is incremented each time the inserted method is called by an amount equal to the length of the inserted text.

Since the insertion history of a single Replica is linear and immutable, this clock can be used to uniquely identify each character in the document without knowing what the actual text associated with its insertion is.

Note that this range has absolutely nothing to do with the spatial offset at which the text was inserted. Its start and end simply refer to the values of the character clock of the Replica that inserted the Text before and after the insertion.

It’s up to you to decide how to map these temporal ranges to the actual text contents inserted by the various peers.

Examples
let mut replica1 = Replica::new(1, 0);

// Peer 1 inserts 1, 2, 3 and 4 characters at the start of the
// document.
let insertion1 = replica1.inserted(0, 1);
let insertion2 = replica1.inserted(0, 2);
let insertion3 = replica1.inserted(0, 3);
let insertion4 = replica1.inserted(0, 4);

// Notice how:
// - the temporal range of the first insertion starts at zero;
// - the start of each range is equal to the end of the previous one;
// - the length of each range matches the one passed to
//   `replica1.inserted`.

assert_eq!(insertion1.text().temporal_range(), 0..1);
assert_eq!(insertion2.text().temporal_range(), 1..3);
assert_eq!(insertion3.text().temporal_range(), 3..6);
assert_eq!(insertion4.text().temporal_range(), 6..10);

Trait Implementations§

source§

impl Clone for Text

source§

fn clone(&self) -> Text

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 Text

source§

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

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

impl<'de> Deserialize<'de> for Text

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Hash for Text

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Text

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl Serialize for Text

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Text

source§

impl StructuralEq for Text

source§

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§

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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,