Struct ArrayRef
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§
§impl Array for ArrayRef
impl Array for ArrayRef
§fn len<T>(&self, _txn: &T) -> u32where
T: ReadTxn,
fn len<T>(&self, _txn: &T) -> u32where
T: ReadTxn,
Returns a number of elements stored in current array.
§fn insert<V>(
&self,
txn: &mut TransactionMut<'_>,
index: u32,
value: V,
) -> <V as Prelim>::Returnwhere
V: Prelim,
fn insert<V>(
&self,
txn: &mut TransactionMut<'_>,
index: u32,
value: V,
) -> <V as Prelim>::Returnwhere
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§fn insert_range<T, V>(
&self,
txn: &mut TransactionMut<'_>,
index: u32,
values: T,
)
fn insert_range<T, V>( &self, txn: &mut TransactionMut<'_>, index: u32, values: T, )
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§fn push_back<V>(
&self,
txn: &mut TransactionMut<'_>,
value: V,
) -> <V as Prelim>::Returnwhere
V: Prelim,
fn push_back<V>(
&self,
txn: &mut TransactionMut<'_>,
value: V,
) -> <V as Prelim>::Returnwhere
V: Prelim,
Inserts given
value at the end of the current array. Read more§fn push_front<V>(
&self,
txn: &mut TransactionMut<'_>,
content: V,
) -> <V as Prelim>::Returnwhere
V: Prelim,
fn push_front<V>(
&self,
txn: &mut TransactionMut<'_>,
content: V,
) -> <V as Prelim>::Returnwhere
V: Prelim,
Inserts given
value at the beginning of the current array. Read more§fn remove(&self, txn: &mut TransactionMut<'_>, index: u32)
fn remove(&self, txn: &mut TransactionMut<'_>, index: u32)
Removes a single element at provided
index.§fn remove_range(&self, txn: &mut TransactionMut<'_>, index: u32, len: u32)
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.§fn get<T>(&self, txn: &T, index: u32) -> Option<Out>where
T: ReadTxn,
fn get<T>(&self, txn: &T, index: u32) -> Option<Out>where
T: ReadTxn,
Retrieves a value stored at a given
index. Returns None when provided index was out
of the range of a current array.§fn get_as<T, V>(&self, txn: &T, index: u32) -> Result<V, Error>where
T: ReadTxn,
V: DeserializeOwned,
fn get_as<T, V>(&self, txn: &T, index: u32) -> Result<V, Error>where
T: ReadTxn,
V: DeserializeOwned,
Returns a value stored under a given
index within current map, deserializing it into
expected type if found. If value was not found, the Any::Null will be substituted and
deserialized instead (i.e. into instance of Option type, if so desired). Read more§fn move_to(&self, txn: &mut TransactionMut<'_>, source: u32, target: u32)
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§fn move_range_to(
&self,
txn: &mut TransactionMut<'_>,
start: u32,
assoc_start: Assoc,
end: u32,
assoc_end: Assoc,
target: u32,
)
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§impl AsRef<ArrayRef> for XmlElementRef
impl AsRef<ArrayRef> for XmlElementRef
§impl AsRef<ArrayRef> for XmlFragmentRef
impl AsRef<ArrayRef> for XmlFragmentRef
§impl DeepObservable for ArrayRef
impl DeepObservable for ArrayRef
§fn observe_deep<F>(&self, f: F) -> Arc<dyn Drop>
fn observe_deep<F>(&self, f: F) -> Arc<dyn Drop>
§fn observe_deep_with<K, F>(&self, key: K, f: F)
fn observe_deep_with<K, F>(&self, key: K, f: F)
§fn unobserve_deep<K>(&self, key: K) -> bool
fn unobserve_deep<K>(&self, key: K) -> bool
Unsubscribe a callback identified by a given key, that was previously subscribed using
Self::observe_deep_with.
§impl DefaultPrelim for ArrayRef
impl DefaultPrelim for ArrayRef
type Prelim = ArrayPrelim
§fn default_prelim() -> <ArrayRef as DefaultPrelim>::Prelim
fn default_prelim() -> <ArrayRef as DefaultPrelim>::Prelim
Returns an instance of Prelim-compatible type, which will turn into reference of a current
type after being integrated into the document store.
§impl IndexedSequence for ArrayRef
impl IndexedSequence for ArrayRef
§fn sticky_index<T>(
&self,
txn: &T,
index: u32,
assoc: Assoc,
) -> Option<StickyIndex>where
T: ReadTxn,
fn sticky_index<T>(
&self,
txn: &T,
index: u32,
assoc: Assoc,
) -> Option<StickyIndex>where
T: ReadTxn,
Returns a StickyIndex equivalent to a human-readable
index.
Returns None if index is beyond the length of current sequence.§impl Observable for ArrayRef
impl Observable for ArrayRef
type Event = ArrayEvent
§fn observe<F>(&self, f: F) -> Arc<dyn Drop>
fn observe<F>(&self, f: F) -> Arc<dyn Drop>
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
§fn observe_with<K, F>(&self, key: K, f: F)
fn observe_with<K, F>(&self, key: K, f: F)
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
impl Eq for ArrayRef
Auto Trait Implementations§
impl Freeze for ArrayRef
impl !RefUnwindSafe for ArrayRef
impl Send for ArrayRef
impl Sync for ArrayRef
impl Unpin for ArrayRef
impl !UnwindSafe for ArrayRef
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
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 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>
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