Struct loro::LoroMovableList

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

LoroMovableList container

It is used to model movable ordered lists.

Using a combination of insert and delete operations, one can simulate set and move operations on a List. However, this approach fails in concurrent editing scenarios. For example, if the same element is set or moved concurrently, the simulation would result in the deletion of the original element and the insertion of two new elements, which does not meet expectations.

Implementations§

source§

impl LoroMovableList

source

pub fn new() -> LoroMovableList

Create a new container that is detached from the document.

The edits on a detached container will not be persisted. To attach the container to the document, please insert it into an attached container.

source

pub fn id(&self) -> ContainerID

Get the container id.

source

pub fn insert(&self, pos: usize, v: impl Into<LoroValue>) -> LoroResult<()>

Insert a value at the given position.

source

pub fn delete(&self, pos: usize, len: usize) -> LoroResult<()>

Delete the value at the given position.

source

pub fn get(&self, index: usize) -> Option<Either<LoroValue, Container>>

Get the value at the given position.

source

pub fn len(&self) -> usize

Get the length of the list.

source

pub fn is_empty(&self) -> bool

Whether the list is empty.

source

pub fn get_value(&self) -> LoroValue

Get the shallow value of the list.

It will not convert the state of sub-containers, but represent them as LoroValue::Container.

source

pub fn get_deep_value(&self) -> LoroValue

Get the deep value of the list.

It will convert the state of sub-containers into a nested JSON value.

source

pub fn pop(&self) -> LoroResult<Option<Either<LoroValue, Container>>>

Pop the last element of the list.

source

pub fn push(&self, v: impl Into<LoroValue>) -> LoroResult<()>

Push a value to the end of the list.

source

pub fn push_container<C: ContainerTrait>(&self, child: C) -> LoroResult<C>

Push a container to the end of the list.

source

pub fn set(&self, pos: usize, value: impl Into<LoroValue>) -> LoroResult<()>

Set the value at the given position.

source

pub fn mov(&self, from: usize, to: usize) -> LoroResult<()>

Move the value at the given position to the given position.

source

pub fn insert_container<C: ContainerTrait>( &self, pos: usize, child: C ) -> LoroResult<C>

Insert a container at the given position.

source

pub fn set_container<C: ContainerTrait>( &self, pos: usize, child: C ) -> LoroResult<C>

Set the container at the given position.

source

pub fn log_internal_state(&self)

Log the internal state of the list.

source

pub fn get_cursor(&self, pos: usize, side: Side) -> Option<Cursor>

Get the cursor at the given position.

Using “index” to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT.

Loro optimizes State metadata by not storing the IDs of deleted elements. This approach complicates tracking cursors since they rely on these IDs. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay.

§Example
use loro::LoroDoc;
use loro_internal::cursor::Side;

let doc = LoroDoc::new();
let list = doc.get_movable_list("list");
list.insert(0, 0).unwrap();
let cursor = list.get_cursor(0, Side::Middle).unwrap();
assert_eq!(doc.get_cursor_pos(&cursor).unwrap().current.pos, 0);
list.insert(0, 0).unwrap();
assert_eq!(doc.get_cursor_pos(&cursor).unwrap().current.pos, 1);
list.insert(0, 0).unwrap();
list.insert(0, 0).unwrap();
assert_eq!(doc.get_cursor_pos(&cursor).unwrap().current.pos, 3);
list.insert(4, 0).unwrap();
assert_eq!(doc.get_cursor_pos(&cursor).unwrap().current.pos, 3);

Trait Implementations§

source§

impl Clone for LoroMovableList

source§

fn clone(&self) -> LoroMovableList

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 ContainerTrait for LoroMovableList

§

type Handler = MovableListHandler

The handler of the container.
source§

fn to_container(&self) -> Container

Convert the container to a Container.
source§

fn to_handler(&self) -> Self::Handler

Convert the container to a handler.
source§

fn from_handler(handler: Self::Handler) -> Self

Convert the handler to a container.
source§

fn try_from_container(container: Container) -> Option<Self>
where Self: Sized,

Try to convert the container to the handler.
source§

fn is_attached(&self) -> bool

Whether the container is attached to a document.
source§

fn get_attached(&self) -> Option<Self>
where Self: Sized,

If a detached container is attached, this method will return its corresponding attached handler.
source§

impl Debug for LoroMovableList

source§

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

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

impl Default for LoroMovableList

source§

fn default() -> Self

Returns the “default value” for a type. 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> 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
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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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

impl<T> ZeroElement for T
where T: Default,