#[repr(transparent)]pub struct Array(_);
Expand description
A collection used to store data in an indexed sequence structure. This type is internally implemented as a double linked list, which may squash values inserted directly one after another into single list node upon transaction commit.
Reading a root-level type as an YArray means treating its sequence components as a list, where every countable element becomes an individual entity:
- JSON-like primitives (booleans, numbers, strings, JSON maps, arrays etc.) are counted individually.
- Text chunks inserted by [Text] data structure: each character becomes an element of an array.
- Embedded and binary values: they count as a single element even though they correspond of multiple bytes.
Like all Yrs shared data types, YArray is resistant to the problem of interleaving (situation when elements 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 Array
impl Array
sourcepub fn insert<V: Prelim>(&self, txn: &mut Transaction, index: u32, value: V)
pub fn insert<V: Prelim>(&self, txn: &mut Transaction, index: u32, value: V)
Inserts a value
at the given index
. Inserting at index 0
is equivalent to prepending
current array with given value
, while inserting at array length is equivalent to appending
that value at the end of it.
Using index
value that’s higher than current array length results in panic.
sourcepub fn insert_range<T, V>(&self, txn: &mut Transaction, index: u32, values: T) where
T: IntoIterator<Item = V>,
V: Into<Any>,
pub fn insert_range<T, V>(&self, txn: &mut Transaction, index: u32, values: T) where
T: IntoIterator<Item = V>,
V: Into<Any>,
Inserts multiple values
at the given index
. Inserting at index 0
is equivalent to
prepending current array with given values
, while inserting at array length is equivalent
to appending that value at the end of it.
Using index
value that’s higher than current array length results in panic.
sourcepub fn push_back<V: Prelim>(&self, txn: &mut Transaction, value: V)
pub fn push_back<V: Prelim>(&self, txn: &mut Transaction, value: V)
Inserts given value
at the end of the current array.
sourcepub fn push_front<V: Prelim>(&self, txn: &mut Transaction, content: V)
pub fn push_front<V: Prelim>(&self, txn: &mut Transaction, content: V)
Inserts given value
at the beginning of the current array.
sourcepub fn remove(&self, txn: &mut Transaction, index: u32)
pub fn remove(&self, txn: &mut Transaction, index: u32)
Removes a single element at provided index
.
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 a range of elements from current array, starting at given index
up until
a particular number described by len
has been deleted. This method panics in case when
not all expected elements were removed (due to insufficient number of elements in an array)
or index
is outside of the bounds of an array.
sourcepub fn get(&self, index: u32) -> Option<Value>
pub fn get(&self, index: u32) -> Option<Value>
Retrieves a value stored at a given index
. Returns None
when provided index was out
of the range of a current array.
sourcepub fn iter(&self) -> ArrayIter<'_>ⓘNotable traits for ArrayIter<'b>impl<'b> Iterator for ArrayIter<'b> type Item = Value;
pub fn iter(&self) -> ArrayIter<'_>ⓘNotable traits for ArrayIter<'b>impl<'b> Iterator for ArrayIter<'b> type Item = Value;
Returns an iterator, that can be used to lazely traverse over all values stored in a current array.
sourcepub fn to_json(&self) -> Any
pub fn to_json(&self) -> Any
Converts all contents of current array into a JSON-like representation.
sourcepub fn observe<F>(&mut self, f: F) -> Subscription<ArrayEvent> where
F: Fn(&Transaction, &ArrayEvent) + 'static,
pub fn observe<F>(&mut self, f: F) -> Subscription<ArrayEvent> where
F: Fn(&Transaction, &ArrayEvent) + 'static,
Subscribes a given callback to be triggered whenever current array 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 array changes can be tracked by using [Event::delta] method.
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
.
Trait Implementations
impl Eq for Array
impl StructuralEq for Array
impl StructuralPartialEq for Array
Auto Trait Implementations
impl !RefUnwindSafe for Array
impl !Send for Array
impl !Sync for Array
impl Unpin for Array
impl !UnwindSafe for Array
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