scsys_core/cont/traits/
entry.rs1pub trait RawEntry<'a> {
9 type Key;
10 type Value;
11
12 private!();
13 fn key(&self) -> &Self::Key;
15 fn value(&self) -> Option<&Self::Value>;
17 fn value_mut(&mut self) -> Option<&mut Self::Value>;
19}
20pub trait Entry<'a>: RawEntry<'a> {
23 fn or_insert(self, value: Self::Value) -> &'a mut Self::Value;
25 fn or_insert_with<F>(self, f: F) -> &'a mut Self::Value
28 where
29 F: FnOnce() -> Self::Value;
30}
31#[cfg(feature = "alloc")]
35macro_rules! entry {
36 ($($prefix:ident)::* -> $call:ident($($arg:tt),*)) => {
37 $($prefix)::*::Entry::$call($($arg),*)
38 };
39}
40#[cfg(feature = "alloc")]
41macro_rules! impl_entry {
42 (@raw $($prefix:ident)::* $(where $($preds:tt)*)?) => {
43 impl<'a, K, V> RawEntry<'a> for $($prefix)::*::Entry<'a, K, V> $(where $($preds)*)? {
44 type Key = K;
45 type Value = V;
46
47 seal!();
48
49 fn key(&self) -> &Self::Key {
50 entry!($($prefix)::* -> key(self))
51 }
52
53 fn value(&self) -> Option<&Self::Value> {
54 match self {
55 $($prefix)::*::Entry::Occupied(entry) => Some(entry.get()),
56 $($prefix)::*::Entry::Vacant(_) => None,
57 }
58 }
59
60 fn value_mut(&mut self) -> Option<&mut Self::Value> {
61 match self {
62 $($prefix)::*::Entry::Occupied(entry) => Some(entry.get_mut()),
63 $($prefix)::*::Entry::Vacant(_) => None,
64 }
65 }
66 }
67
68 };
69 (@entry $($prefix:ident)::* $(where $($preds:tt)*)?) => {
70 impl<'a, K, V> Entry<'a> for $($prefix)::*::Entry<'a, K, V> $(where $($preds)*)? {
71 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
72 entry!($($prefix)::* -> or_insert(self, default))
73 }
74
75 fn or_insert_with<F>(self, f: F) -> &'a mut Self::Value
76 where
77 F: FnOnce() -> Self::Value,
78 {
79 entry!($($prefix)::* -> or_insert_with(self, f))
80 }
81 }
82
83 };
84 (@impl $($rest:tt)*) => {
85 impl_entry!(@raw $($rest)*);
86 impl_entry!(@entry $($rest)*);
87
88 };
89 ($($rest:tt)*) => {
90 impl_entry!(@impl $($rest)*);
91 };
92}
93
94#[cfg(feature = "alloc")]
95impl_entry! {
96 alloc::collections::btree_map where K: Ord
97}
98
99#[cfg(feature = "std")]
100impl_entry! {
101 std::collections::hash_map where K: Eq + core::hash::Hash
102}