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 AsRef<MapRef> for XmlHookRef
impl AsRef<MapRef> for XmlHookRef
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§fn observe_deep_with<K, F>(&self, key: K, f: F)
fn observe_deep_with<K, F>(&self, key: K, f: F)
Source§fn unobserve_deep<K: Into<Origin>>(&self, key: K) -> bool
fn unobserve_deep<K: Into<Origin>>(&self, key: K) -> bool
Unsubscribe a callback identified by a given key, that was previously subscribed using
Self::observe_deep_with.
Source§impl DefaultPrelim for MapRef
impl DefaultPrelim for MapRef
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.
fn into_iter<'a, T: ReadTxn + 'a>(self, txn: &'a T) -> MapIntoIter<'a, T> ⓘ
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 try_update<K, V>(
&self,
txn: &mut TransactionMut<'_>,
key: K,
value: V,
) -> bool
fn try_update<K, V>( &self, txn: &mut TransactionMut<'_>, key: K, value: V, ) -> bool
Tries to update a value stored under a given
key within current map, if it’s different
from the current one. Returns true if the value was updated, false otherwise. Read moreSource§fn get_or_init<K, V>(&self, txn: &mut TransactionMut<'_>, key: K) -> V
fn get_or_init<K, V>(&self, txn: &mut TransactionMut<'_>, key: K) -> V
Returns an existing instance of a type stored under a given
key within current map.
If the given entry was not found, has been deleted or its type is different from expected,
that entry will be reset to a given type and its reference will be returned.Source§fn remove(&self, txn: &mut TransactionMut<'_>, key: &str) -> Option<Out>
fn remove(&self, txn: &mut TransactionMut<'_>, key: &str) -> Option<Out>
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<Out>
fn get<T: ReadTxn>(&self, txn: &T, key: &str) -> Option<Out>
Returns a value stored under a given
key within current map, or None if no entry
with such key existed.Source§fn get_as<T, V>(&self, txn: &T, key: &str) -> Result<V, Error>where
T: ReadTxn,
V: DeserializeOwned,
fn get_as<T, V>(&self, txn: &T, key: &str) -> Result<V, Error>where
T: ReadTxn,
V: DeserializeOwned,
Returns a value stored under a given
key 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 moreSource§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§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 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