Debian Codegraph @

third_party/rust/mls-rs/src/map.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5 use alloc::vec::Vec;
6 use core::{
7 hash::Hash,
8 ops::{Deref, DerefMut},
9 };
10
11 use map_impl::SmallMapInner;
12 pub use map_impl::{LargeMap, LargeMapEntry, SmallMap};
13 use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
14
15 #[cfg(feature = "std")]
16 mod map_impl {
17 use core::hash::Hash;
18 use std::collections::{hash_map::Entry, HashMap};
19
20 #[derive(Clone, Debug, PartialEq, Eq)]
21 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22 pub struct SmallMap<K: Hash + Eq, V>(pub(super) HashMap<K, V>);
23
24 pub type LargeMap<K, V> = SmallMap<K, V>;
25 pub(super) type SmallMapInner<K, V> = HashMap<K, V>;
26 pub type LargeMapEntry<'a, K, V> = Entry<'a, K, V>;
27 }
28
29 #[cfg(not(feature = "std"))]
30 mod map_impl {
31 use core::hash::Hash;
32
33 use alloc::{
34 collections::{btree_map::Entry, BTreeMap},
35 vec::Vec,
36 };
37 #[cfg(feature = "by_ref_proposal")]
38 use itertools::Itertools;
39
40 #[derive(Clone, Debug, PartialEq, Eq)]
41 pub struct SmallMap<K: Hash + Eq, V>(pub(super) Vec<(K, V)>);
42
43 pub type LargeMap<K, V> = BTreeMap<K, V>;
44 pub(super) type SmallMapInner<K, V> = Vec<(K, V)>;
45 pub type LargeMapEntry<'a, K, V> = Entry<'a, K, V>;
46
47 #[cfg(feature = "by_ref_proposal")]
48 impl<K: Hash + Eq, V> SmallMap<K, V> {
49 pub fn get(&self, key: &K) -> Option<&V> {
50 self.find(key).map(|i| &self.0[i].1)
51 }
52
53 pub fn insert(&mut self, key: K, value: V) {
54 match self.0.iter_mut().find(|(k, _)| (k == &key)) {
55 Some((_, v)) => *v = value,
56 None => self.0.push((key, value)),
57 }
58 }
59
60 pub fn remove(&mut self, key: &K) -> Option<V> {
61 self.find(key).map(|i| self.0.remove(i).1)
62 }
63
64 fn find(&self, key: &K) -> Option<usize> {
65 self.0.iter().position(|(k, _)| k == key)
66 }
67 }
68 }
69
70 impl<K: Hash + Eq, V> Default for SmallMap<K, V> {
71 fn default() -> Self {
72 Self(SmallMapInner::new())
73 }
74 }
75
76 impl<K: Hash + Eq, V> Deref for SmallMap<K, V> {
77 type Target = SmallMapInner<K, V>;
78
79 fn deref(&self) -> &Self::Target {
80 &self.0
81 }
82 }
83
84 impl<K: Hash + Eq, V> DerefMut for SmallMap<K, V> {
85 fn deref_mut(&mut self) -> &mut Self::Target {
86 &mut self.0
87 }
88 }
89
90 impl<K, V> MlsDecode for SmallMap<K, V>
91 where
92 K: Hash + Eq + MlsEncode + MlsDecode + MlsSize,
93 V: MlsEncode + MlsDecode + MlsSize,
94 {
95 fn mls_decode(reader: &mut &[u8]) -> Result<Self, mls_rs_codec::Error> {
96 SmallMapInner::mls_decode(reader).map(Self)
97 }
98 }
99
100 impl<K, V> MlsSize for SmallMap<K, V>
101 where
102 K: Hash + Eq + MlsEncode + MlsDecode + MlsSize,
103 V: MlsEncode + MlsDecode + MlsSize,
104 {
105 fn mls_encoded_len(&self) -> usize {
106 self.0.mls_encoded_len()
107 }
108 }
109
110 impl<K, V> MlsEncode for SmallMap<K, V>
111 where
112 K: Hash + Eq + MlsEncode + MlsDecode + MlsSize,
113 V: MlsEncode + MlsDecode + MlsSize,
114 {
115 fn mls_encode(&self, writer: &mut Vec<u8>) -> Result<(), mls_rs_codec::Error> {
116 self.0.mls_encode(writer)
117 }
118 }