Struct loro::LoroMovableList
source · pub struct LoroMovableList { /* private fields */ }
Expand description
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
impl LoroMovableList
sourcepub fn new() -> LoroMovableList
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.
sourcepub fn id(&self) -> ContainerID
pub fn id(&self) -> ContainerID
Get the container id.
sourcepub fn insert(&self, pos: usize, v: impl Into<LoroValue>) -> LoroResult<()>
pub fn insert(&self, pos: usize, v: impl Into<LoroValue>) -> LoroResult<()>
Insert a value at the given position.
sourcepub fn delete(&self, pos: usize, len: usize) -> LoroResult<()>
pub fn delete(&self, pos: usize, len: usize) -> LoroResult<()>
Delete the value at the given position.
sourcepub fn get(&self, index: usize) -> Option<Either<LoroValue, Container>>
pub fn get(&self, index: usize) -> Option<Either<LoroValue, Container>>
Get the value at the given position.
sourcepub fn get_value(&self) -> LoroValue
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.
sourcepub fn get_deep_value(&self) -> LoroValue
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.
sourcepub fn pop(&self) -> LoroResult<Option<Either<LoroValue, Container>>>
pub fn pop(&self) -> LoroResult<Option<Either<LoroValue, Container>>>
Pop the last element of the list.
sourcepub fn push(&self, v: impl Into<LoroValue>) -> LoroResult<()>
pub fn push(&self, v: impl Into<LoroValue>) -> LoroResult<()>
Push a value to the end of the list.
sourcepub fn push_container<C: ContainerTrait>(&self, child: C) -> LoroResult<C>
pub fn push_container<C: ContainerTrait>(&self, child: C) -> LoroResult<C>
Push a container to the end of the list.
sourcepub fn set(&self, pos: usize, value: impl Into<LoroValue>) -> LoroResult<()>
pub fn set(&self, pos: usize, value: impl Into<LoroValue>) -> LoroResult<()>
Set the value at the given position.
sourcepub fn mov(&self, from: usize, to: usize) -> LoroResult<()>
pub fn mov(&self, from: usize, to: usize) -> LoroResult<()>
Move the value at the given position to the given position.
sourcepub fn insert_container<C: ContainerTrait>(
&self,
pos: usize,
child: C
) -> LoroResult<C>
pub fn insert_container<C: ContainerTrait>( &self, pos: usize, child: C ) -> LoroResult<C>
Insert a container at the given position.
sourcepub fn set_container<C: ContainerTrait>(
&self,
pos: usize,
child: C
) -> LoroResult<C>
pub fn set_container<C: ContainerTrait>( &self, pos: usize, child: C ) -> LoroResult<C>
Set the container at the given position.
sourcepub fn log_internal_state(&self)
pub fn log_internal_state(&self)
Log the internal state of the list.
sourcepub fn get_cursor(&self, pos: usize, side: Side) -> Option<Cursor>
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
impl Clone for LoroMovableList
source§fn clone(&self) -> LoroMovableList
fn clone(&self) -> LoroMovableList
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl ContainerTrait for LoroMovableList
impl ContainerTrait for LoroMovableList
§type Handler = MovableListHandler
type Handler = MovableListHandler
source§fn to_container(&self) -> Container
fn to_container(&self) -> Container
source§fn to_handler(&self) -> Self::Handler
fn to_handler(&self) -> Self::Handler
source§fn from_handler(handler: Self::Handler) -> Self
fn from_handler(handler: Self::Handler) -> Self
source§fn try_from_container(container: Container) -> Option<Self>where
Self: Sized,
fn try_from_container(container: Container) -> Option<Self>where
Self: Sized,
source§fn is_attached(&self) -> bool
fn is_attached(&self) -> bool
source§fn get_attached(&self) -> Option<Self>where
Self: Sized,
fn get_attached(&self) -> Option<Self>where
Self: Sized,
source§impl Debug for LoroMovableList
impl Debug for LoroMovableList
Auto Trait Implementations§
impl Freeze for LoroMovableList
impl RefUnwindSafe for LoroMovableList
impl Send for LoroMovableList
impl Sync for LoroMovableList
impl Unpin for LoroMovableList
impl UnwindSafe for LoroMovableList
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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