pub struct MapRef(/* private fields */);Expand description
Collection used to store key-value entries in an unordered manner. Keys are always represented as UTF-8 strings. Values can be any value type supported by Yrs: JSON-like primitives as well as shared data types.
In terms of conflict resolution, MapRef uses logical last-write-wins principle, meaning the past updates are automatically overridden and discarded by newer ones, while concurrent updates made by different peers are resolved into a single value using document id seniority to establish order.
§Example
use yrs::{any, Doc, Map, MapPrelim, Transact};
use yrs::types::ToJson;
let doc = Doc::new();
let map = doc.get_or_insert_map("map");
let mut txn = doc.transact_mut();
// insert value
map.insert(&mut txn, "key1", "value1");
// insert nested shared type
let nested = map.insert(&mut txn, "key2", MapPrelim::from([("inner", "value2")]));
nested.insert(&mut txn, "inner2", 100);
assert_eq!(map.to_json(&txn), any!({
"key1": "value1",
"key2": {
"inner": "value2",
"inner2": 100
}
}));
// get value
assert_eq!(map.get(&txn, "key1"), Some("value1".into()));
// remove entry
map.remove(&mut txn, "key1");
assert_eq!(map.get(&txn, "key1"), None);Trait Implementations§
Source§impl DeepObservable for MapRef
impl DeepObservable for MapRef
Source§fn observe_deep<F>(&self, f: F) -> Subscription
fn observe_deep<F>(&self, f: F) -> Subscription
impl Eq for MapRef
Source§impl Into<MapRef> for XmlElementRef
impl Into<MapRef> for XmlElementRef
Source§impl Into<MapRef> for XmlHookRef
impl Into<MapRef> for XmlHookRef
Source§impl Map for MapRef
impl Map for MapRef
Source§fn len<T>(&self, _txn: &T) -> u32where
T: ReadTxn,
fn len<T>(&self, _txn: &T) -> u32where
T: ReadTxn,
Returns a number of entries stored within current map.
Source§fn keys<'a, T>(&'a self, txn: &'a T) -> Keys<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
fn keys<'a, T>(&'a self, txn: &'a T) -> Keys<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
Returns an iterator that enables to traverse over all keys of entries stored within
current map. These keys are not ordered.
Source§fn values<'a, T>(&'a self, txn: &'a T) -> Values<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
fn values<'a, T>(&'a self, txn: &'a T) -> Values<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
Returns an iterator that enables to traverse over all values stored within current map.
Source§fn iter<'a, T>(&'a self, txn: &'a T) -> MapIter<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
fn iter<'a, T>(&'a self, txn: &'a T) -> MapIter<'a, &'a T, T> ⓘwhere
T: ReadTxn + 'a,
Returns an iterator that enables to traverse over all entries - tuple of key-value pairs -
stored within current map.
Source§fn insert<K, V>(
&self,
txn: &mut TransactionMut<'_>,
key: K,
value: V,
) -> <V as Prelim>::Return
fn insert<K, V>( &self, txn: &mut TransactionMut<'_>, key: K, value: V, ) -> <V as Prelim>::Return
Inserts a new
value under given key into current map. Returns an integrated value.Source§fn remove(&self, txn: &mut TransactionMut<'_>, key: &str) -> Option<Value>
fn remove(&self, txn: &mut TransactionMut<'_>, key: &str) -> Option<Value>
Removes a stored within current map under a given
key. Returns that value or None if
no entry with a given key was present in current map. Read moreSource§fn get<T>(&self, txn: &T, key: &str) -> Option<Value>where
T: ReadTxn,
fn get<T>(&self, txn: &T, key: &str) -> Option<Value>where
T: ReadTxn,
Returns a value stored under a given
key within current map, or None if no entry
with such key existed.Source§fn contains_key<T>(&self, _txn: &T, key: &str) -> boolwhere
T: ReadTxn,
fn contains_key<T>(&self, _txn: &T, key: &str) -> boolwhere
T: ReadTxn,
Checks if an entry with given
key can be found within current map.Source§fn clear(&self, txn: &mut TransactionMut<'_>)
fn clear(&self, txn: &mut TransactionMut<'_>)
Clears the contents of current map, effectively removing all of its entries.
Source§impl Observable for MapRef
impl Observable for MapRef
type Event = MapEvent
Source§fn observe<F>(&self, f: F) -> Subscription
fn observe<F>(&self, f: F) -> Subscription
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
Auto Trait Implementations§
impl !RefUnwindSafe for MapRef
impl !Send for MapRef
impl !Sync for MapRef
impl !UnwindSafe for MapRef
impl Freeze for MapRef
impl Unpin for MapRef
impl UnsafeUnpin for MapRef
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
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>
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