Struct yrs::types::array::ArrayRef

source ·
pub struct ArrayRef(/* private fields */);
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 ArrayRef 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, ArrayRef 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.

§Example

use yrs::{Array, Doc, Map, MapPrelim, Transact, Any, any};
use yrs::types::ToJson;

let doc = Doc::new();
let array = doc.get_or_insert_array("array");
let mut txn = doc.transact_mut();

// insert single scalar value
array.insert(&mut txn, 0, "value");
array.remove_range(&mut txn, 0, 1);

assert_eq!(array.len(&txn), 0);

// insert multiple values at once
array.insert_range(&mut txn, 0, ["a", "b", "c"]);
assert_eq!(array.len(&txn), 3);

// get value
let value = array.get(&txn, 1);
assert_eq!(value, Some("b".into()));

// insert nested shared types
let map = array.insert(&mut txn, 1, MapPrelim::from([("key1", "value1")]));
map.insert(&mut txn, "key2", "value2");

assert_eq!(array.to_json(&txn), any!([
  "a",
  { "key1": "value1", "key2": "value2" },
  "b",
  "c"
]));

Trait Implementations§

source§

impl Array for ArrayRef

source§

fn len<T: ReadTxn>(&self, _txn: &T) -> u32

Returns a number of elements stored in current array.
source§

fn insert<V>( &self, txn: &mut TransactionMut<'_>, index: u32, value: V ) -> V::Return
where V: Prelim,

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. Read more
source§

fn insert_range<T, V>( &self, txn: &mut TransactionMut<'_>, 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. Read more
source§

fn push_back<V>(&self, txn: &mut TransactionMut<'_>, value: V) -> V::Return
where V: Prelim,

Inserts given value at the end of the current array. Read more
source§

fn push_front<V>(&self, txn: &mut TransactionMut<'_>, content: V) -> V::Return
where V: Prelim,

Inserts given value at the beginning of the current array. Read more
source§

fn remove(&self, txn: &mut TransactionMut<'_>, index: u32)

Removes a single element at provided index.
source§

fn remove_range(&self, txn: &mut TransactionMut<'_>, 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.
source§

fn get<T: ReadTxn>(&self, txn: &T, 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.
source§

fn move_to(&self, txn: &mut TransactionMut<'_>, source: u32, target: u32)

Moves element found at source index into target index position. Both indexes refer to a current state of the document. Read more
source§

fn move_range_to( &self, txn: &mut TransactionMut<'_>, start: u32, assoc_start: Assoc, end: u32, assoc_end: Assoc, target: u32 )

Moves all elements found within start..end indexes range (both side inclusive) into new position pointed by target index. All elements inserted concurrently by other peers inside of moved range will be moved as well after synchronization (although it make take more than one sync roundtrip to achieve convergence). Read more
source§

fn iter<'a, T: ReadTxn + 'a>(&self, txn: &'a T) -> ArrayIter<&'a T, T>

Returns an iterator, that can be used to lazely traverse over all values stored in a current array.
source§

impl AsRef<Branch> for ArrayRef

source§

fn as_ref(&self) -> &Branch

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for ArrayRef

source§

fn clone(&self) -> ArrayRef

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 ArrayRef

source§

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

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

impl DeepObservable for ArrayRef

source§

fn observe_deep<F>(&self, f: F) -> Subscription
where F: Fn(&TransactionMut<'_>, &Events) + 'static,

Subscribe a callback f for all events emitted by this and nested collaborative types. Callback is accepting transaction which triggered that event and event itself, wrapped within an Event structure. Read more
source§

impl From<BranchPtr> for ArrayRef

source§

fn from(inner: BranchPtr) -> Self

Converts to this type from the input type.
source§

impl IndexedSequence for ArrayRef

source§

fn sticky_index( &self, txn: &mut TransactionMut<'_>, index: u32, assoc: Assoc ) -> Option<StickyIndex>

Returns a StickyIndex equivalent to a human-readable index. Returns None if index is beyond the length of current sequence.
source§

impl Into<ArrayRef> for XmlElementRef

source§

fn into(self) -> ArrayRef

Converts this type into the (usually inferred) input type.
source§

impl Observable for ArrayRef

§

type Event = ArrayEvent

source§

fn observe<F>(&self, f: F) -> Subscription
where F: Fn(&TransactionMut<'_>, &Self::Event) + 'static, Event: AsRef<Self::Event>,

Subscribes a given callback to be triggered whenever current y-type 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. Read more
source§

impl PartialEq for ArrayRef

source§

fn eq(&self, other: &Self) -> 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 RootRef for ArrayRef

source§

fn type_ref() -> TypeRef

source§

fn root<N: Into<Arc<str>>>(name: N) -> Root<Self>

Create a logical collaborative collection reference to a root-level type with a given name
source§

impl SharedRef for ArrayRef

source§

fn hook(&self) -> Hook<Self>

Returns a logical descriptor of a current shared collection.
source§

impl ToJson for ArrayRef

source§

fn to_json<T: ReadTxn>(&self, txn: &T) -> Any

Converts all contents of a current type into a JSON-like representation.
source§

impl TryFrom<ItemPtr> for ArrayRef

§

type Error = ItemPtr

The type returned in the event of a conversion error.
source§

fn try_from(value: ItemPtr) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Value> for ArrayRef

§

type Error = Value

The type returned in the event of a conversion error.
source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for ArrayRef

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, 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> 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.