scsys_traits/cont/
entry.rs1pub trait Entry<'a> {
9 type Key;
10 type Value;
11 fn key(&self) -> &Self::Key;
13 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value;
16 fn or_insert_with<F>(self, f: F) -> &'a mut Self::Value
19 where
20 F: FnOnce() -> Self::Value;
21}
22pub trait OrInsert<K, V> {
25 fn or_insert(&mut self, key: K, value: V) -> V;
26}
27
28#[allow(unused_macros)]
33macro_rules! entry {
34 ($($prefix:ident)::* -> $call:ident($($arg:tt),*)) => {
35 $($prefix)::*::Entry::$call($($arg),*)
36 };
37}
38
39#[allow(unused_macros)]
40macro_rules! impl_entry {
41 ($($prefix:ident)::* $(where $($preds:tt)*)?) => {
42
43 impl<'a, K, V> Entry<'a> for $($prefix)::*::Entry<'a, K, V> $(where $($preds)*)? {
44 type Key = K;
45 type Value = V;
46
47 fn key(&self) -> &Self::Key {
48 entry!($($prefix)::* -> key(self))
49 }
50
51 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
52 entry!($($prefix)::* -> or_insert(self, default))
53 }
54
55 fn or_insert_with<F>(self, f: F) -> &'a mut Self::Value
56 where
57 F: FnOnce() -> Self::Value,
58 {
59 entry!($($prefix)::* -> or_insert_with(self, f))
60 }
61 }
62
63 };
64}
65
66#[cfg(feature = "alloc")]
67impl_entry!(alloc::collections::btree_map where K: Ord);
68#[cfg(feature = "std")]
69impl_entry!(std::collections::hash_map where K: Eq + core::hash::Hash);