scsys_core/cont/traits/
entry.rs

1/*
2    appellation: key_value <module>
3    authors: @FL03
4*/
5
6/// A [`RawEntry`] represents a single record within a key-value store, providing access to the
7/// key alongisde methods for accessing and manipulating the value of the entry.
8pub trait RawEntry<'a> {
9    type Key;
10    type Value;
11
12    private!();
13    /// Returns a reference to the key of the entry.
14    fn key(&self) -> &Self::Key;
15    /// Returns a reference to the value of the entry.
16    fn value(&self) -> Option<&Self::Value>;
17    /// Returns a mutable reference to the value of the entry.
18    fn value_mut(&mut self) -> Option<&mut Self::Value>;
19}
20/// The [`Entry`] trait extends the [`RawEntry`] trait to provide additional methods for
21///
22pub trait Entry<'a>: RawEntry<'a> {
23    /// if the entry is vacant, insert the given value
24    fn or_insert(self, value: Self::Value) -> &'a mut Self::Value;
25    /// if the entry does not exist, insert the value returned by the provided function and
26    /// return a mutable reference to it.
27    fn or_insert_with<F>(self, f: F) -> &'a mut Self::Value
28    where
29        F: FnOnce() -> Self::Value;
30}
31/*
32 ************* Implementations *************
33*/
34#[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}