osiris/types/
traits.rs

1use core::borrow::Borrow;
2
3pub trait Get<Idx: ?Sized> {
4    type Output: ?Sized;
5
6    fn get<K: Borrow<Idx>>(&self, index: K) -> Option<&Self::Output>;
7}
8
9pub trait GetMut<Idx: ?Sized>: Get<Idx> {
10    fn get_mut<K: Borrow<Idx>>(&mut self, index: K) -> Option<&mut Self::Output>;
11
12    // Getting multiple disjoint mutable references at once
13    fn get2_mut<K: Borrow<Idx>>(
14        &mut self,
15        index1: K,
16        index2: K,
17    ) -> (Option<&mut Self::Output>, Option<&mut Self::Output>);
18    fn get3_mut<K: Borrow<Idx>>(
19        &mut self,
20        index1: K,
21        index2: K,
22        index3: K,
23    ) -> (
24        Option<&mut Self::Output>,
25        Option<&mut Self::Output>,
26        Option<&mut Self::Output>,
27    );
28}
29
30pub trait ToIndex {
31    fn to_index<Q: Borrow<Self>>(index: Option<Q>) -> usize;
32}
33
34impl ToIndex for usize {
35    fn to_index<Q: Borrow<Self>>(index: Option<Q>) -> usize {
36        index.map_or(0, |i| *i.borrow())
37    }
38}
39
40pub trait Project<P> {
41    fn project(&self) -> Option<&P>;
42    fn project_mut(&mut self) -> Option<&mut P>;
43}