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
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: ReadTxn>(&self, _txn: &T) -> u32
fn len<T: ReadTxn>(&self, _txn: &T) -> u32
Returns a number of entries stored within current map.
source§fn keys<'a, T: ReadTxn + 'a>(&'a self, txn: &'a T) -> Keys<'a, &'a T, T> ⓘ
fn keys<'a, T: ReadTxn + 'a>(&'a self, txn: &'a T) -> Keys<'a, &'a T, T> ⓘ
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: ReadTxn + 'a>(&'a self, txn: &'a T) -> Values<'a, &'a T, T> ⓘ
fn values<'a, T: ReadTxn + 'a>(&'a self, txn: &'a T) -> Values<'a, &'a T, T> ⓘ
Returns an iterator that enables to traverse over all values stored within current map.
source§fn iter<'a, T: ReadTxn + 'a>(&'a self, txn: &'a T) -> MapIter<'a, &'a T, T> ⓘ
fn iter<'a, T: ReadTxn + 'a>(&'a self, txn: &'a T) -> MapIter<'a, &'a T, T> ⓘ
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::Return
fn insert<K, V>( &self, txn: &mut TransactionMut<'_>, key: K, value: V ) -> V::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: ReadTxn>(&self, txn: &T, key: &str) -> Option<Value>
fn get<T: ReadTxn>(&self, txn: &T, key: &str) -> Option<Value>
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: ReadTxn>(&self, _txn: &T, key: &str) -> bool
fn contains_key<T: ReadTxn>(&self, _txn: &T, key: &str) -> bool
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
source§impl PartialEq for MapRef
impl PartialEq for MapRef
impl Eq for MapRef
Auto Trait Implementations§
impl Freeze for MapRef
impl !RefUnwindSafe for MapRef
impl !Send for MapRef
impl !Sync for MapRef
impl Unpin for MapRef
impl !UnwindSafe 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