pub struct LoroList { /* private fields */ }Expand description
LoroList container. It’s used to model arrays.
It can have sub containers.
Important: choose the right structure.
- Use
LoroListfor ordered collections where elements are appended/inserted/deleted. - Use
LoroMovableListwhen frequent reordering (drag-and-drop) is needed. - Use
LoroMapfor keyed records and coordinates.
// Bad: coordinates in a list can diverge under concurrency
// let coord = doc.get_list("coord");
// coord.insert(0, 10).unwrap(); // x
// coord.insert(1, 20).unwrap(); // y
// Good: use a map for labeled fields
// let coord = doc.get_map("coord");
// coord.insert("x", 10).unwrap();
// coord.insert("y", 20).unwrap();let doc = LoroDoc::new();
let list = doc.get_list("list");
list.insert(0, 123).unwrap();
list.insert(1, "h").unwrap();
assert_eq!(
doc.get_deep_value().to_json_value(),
json!({
"list": [123, "h"]
})
);Implementations§
Source§impl LoroList
impl LoroList
Sourcepub fn new() -> Self
pub fn new() -> Self
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 is_attached(&self) -> bool
pub fn is_attached(&self) -> bool
Whether the container is attached to a 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 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 values at the given position.
Sourcepub fn get(&self, index: usize) -> Option<ValueOrContainer>
pub fn get(&self, index: usize) -> Option<ValueOrContainer>
Get the value at the given position.
Sourcepub fn get_deep_value(&self) -> LoroValue
pub fn get_deep_value(&self) -> LoroValue
Get the deep value of the container.
Sourcepub fn get_value(&self) -> LoroValue
pub fn get_value(&self) -> LoroValue
Get the shallow value of the container.
This does not convert the state of sub-containers; instead, it represents them as LoroValue::Container.
Sourcepub fn pop(&self) -> LoroResult<Option<LoroValue>>
pub fn pop(&self) -> LoroResult<Option<LoroValue>>
Pop the last element 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 list.
Sourcepub fn for_each<I>(&self, f: I)where
I: FnMut(ValueOrContainer),
pub fn for_each<I>(&self, f: I)where
I: FnMut(ValueOrContainer),
Iterate over the elements of the list.
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 with the given type at the given index.
§Example
let doc = LoroDoc::new();
let list = doc.get_list("m");
let text = list.insert_container(0, LoroText::new()).unwrap();
text.insert(0, "12");
text.insert(0, "0");
assert_eq!(doc.get_deep_value().to_json_value(), json!({"m": ["012"]}));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_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);Sourcepub fn to_vec(&self) -> Vec<LoroValue>
pub fn to_vec(&self) -> Vec<LoroValue>
Converts the LoroList to a Vec of LoroValue.
This method unwraps the internal Arc and clones the data if necessary, returning a Vec containing all the elements of the LoroList as LoroValue.
§Returns
A Vec
§Example
use loro::{LoroDoc, LoroValue};
let doc = LoroDoc::new();
let list = doc.get_list("my_list");
list.insert(0, 1).unwrap();
list.insert(1, "hello").unwrap();
list.insert(2, true).unwrap();
let vec = list.to_vec();Sourcepub fn clear(&self) -> LoroResult<()>
pub fn clear(&self) -> LoroResult<()>
Delete all elements in the list.
Trait Implementations§
Source§impl ContainerTrait for LoroList
impl ContainerTrait for LoroList
Source§type Handler = ListHandler
type Handler = ListHandler
Source§fn id(&self) -> ContainerID
fn id(&self) -> ContainerID
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 is_attached(&self) -> bool
fn is_attached(&self) -> bool
Source§fn get_attached(&self) -> Option<Self>
fn get_attached(&self) -> Option<Self>
Source§fn try_from_container(container: Container) -> Option<Self>
fn try_from_container(container: Container) -> Option<Self>
Source§fn is_deleted(&self) -> bool
fn is_deleted(&self) -> bool
Source§fn subscribe(&self, callback: Subscriber) -> Option<Subscription>
fn subscribe(&self, callback: Subscriber) -> Option<Subscription>
Auto Trait Implementations§
impl Freeze for LoroList
impl RefUnwindSafe for LoroList
impl Send for LoroList
impl Sync for LoroList
impl Unpin for LoroList
impl UnwindSafe for LoroList
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);