pub trait SharedData: Debug {
type Key: DataKey;
type Item: Clone;
type ItemRef<'b>: Borrow<Self::Item>
where Self: 'b;
// Required methods
fn contains_key(&self, key: &Self::Key) -> bool;
fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>;
// Provided methods
fn with_ref<V>(
&self,
key: &Self::Key,
f: impl FnOnce(&Self::Item) -> V,
) -> Option<V>
where Self: Sized { ... }
fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item> { ... }
}Expand description
Trait for shared data
By design, all methods take only &self and only allow immutable access to
data.
Required Associated Types§
Sourcetype ItemRef<'b>: Borrow<Self::Item>
where
Self: 'b
type ItemRef<'b>: Borrow<Self::Item> where Self: 'b
A borrow of the item type
This type must support Borrow over Self::Item. This is, for
example, supported by Self::Item and &Self::Item.
It is also recommended (but not required) that the type support
std::ops::Deref: this allows easier usage of Self::borrow.
TODO(spec): once Rust supports some form of specialization, AsRef will
presumably get blanket impls over T and &T, and will then be more
appropriate to use than Borrow.
Required Methods§
Sourcefn contains_key(&self, key: &Self::Key) -> bool
fn contains_key(&self, key: &Self::Key) -> bool
Check whether a key has data
Sourcefn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>
fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>
Borrow an item by key
Returns None if key has no associated item.
Depending on the implementation, this may involve some form of lock
such as RefCell::borrow or Mutex::lock. The implementation should
panic on lock failure, not return None.
Provided Methods§
Sourcefn with_ref<V>(
&self,
key: &Self::Key,
f: impl FnOnce(&Self::Item) -> V,
) -> Option<V>where
Self: Sized,
fn with_ref<V>(
&self,
key: &Self::Key,
f: impl FnOnce(&Self::Item) -> V,
) -> Option<V>where
Self: Sized,
Access a borrow of an item
This is a convenience method over Self::borrow.
Sourcefn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>
fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>
Get data by key (clone)
Returns None if key has no associated item.
This has a default implementation over Self::borrow.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.