SharedData

Trait SharedData 

Source
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§

Source

type Key: DataKey

Key type

Source

type Item: Clone

Item type

Source

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§

Source

fn contains_key(&self, key: &Self::Key) -> bool

Check whether a key has data

Source

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§

Source

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.

Source

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.

Implementations on Foreign Types§

Source§

impl<T: Clone + Debug + 'static> SharedData for [T]

Source§

type Key = usize

Source§

type Item = T

Source§

type ItemRef<'b> = &'b T

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &usize) -> Option<Self::Item>

Source§

impl<T: Clone + Debug + 'static> SharedData for Vec<T>

Source§

type Key = usize

Source§

type Item = T

Source§

type ItemRef<'b> = &'b T

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &usize) -> Option<Self::Item>

Source§

impl<T: SharedData + ?Sized> SharedData for &T

Source§

type Key = <T as SharedData>::Key

Source§

type Item = <T as SharedData>::Item

Source§

type ItemRef<'b> = <T as SharedData>::ItemRef<'b> where Self: 'b

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>

Source§

impl<T: SharedData + ?Sized> SharedData for &mut T

Source§

type Key = <T as SharedData>::Key

Source§

type Item = <T as SharedData>::Item

Source§

type ItemRef<'b> = <T as SharedData>::ItemRef<'b> where Self: 'b

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>

Source§

impl<T: SharedData + ?Sized> SharedData for Box<T>

Source§

type Key = <T as SharedData>::Key

Source§

type Item = <T as SharedData>::Item

Source§

type ItemRef<'b> = <T as SharedData>::ItemRef<'b> where Self: 'b

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>

Source§

impl<T: SharedData + ?Sized> SharedData for Rc<T>

Source§

type Key = <T as SharedData>::Key

Source§

type Item = <T as SharedData>::Item

Source§

type ItemRef<'b> = <T as SharedData>::ItemRef<'b> where Self: 'b

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>

Source§

impl<T: SharedData + ?Sized> SharedData for Arc<T>

Source§

type Key = <T as SharedData>::Key

Source§

type Item = <T as SharedData>::Item

Source§

type ItemRef<'b> = <T as SharedData>::ItemRef<'b> where Self: 'b

Source§

fn contains_key(&self, key: &Self::Key) -> bool

Source§

fn borrow(&self, key: &Self::Key) -> Option<Self::ItemRef<'_>>

Source§

fn get_cloned(&self, key: &Self::Key) -> Option<Self::Item>

Implementors§

Source§

impl<A: ListData + 'static> SharedData for UnsafeFilteredList<A>

Source§

type Key = <A as SharedData>::Key

Source§

type Item = <A as SharedData>::Item

Source§

type ItemRef<'b> = <A as SharedData>::ItemRef<'b> where A: 'b