bpf_api/collections/hashmap.rs
1use crate::error::Error;
2use crate::platform::{Map, MapType};
3
4/// A hashmap that exposes an idiomatic Rust interface to an underlying BPF hashmap.
5pub struct HashMap<K: Copy + Default, V: Copy + Default> {
6 map: Map<K, V>,
7}
8
9impl<K: Copy + Default, V: Copy + Default> HashMap<K, V> {
10 /// Creates a new BPF hashmap with `entries` elements.
11 ///
12 /// # Arguments
13 ///
14 /// * `entries` - The number of elements in the hashmap.
15 ///
16 /// # Example
17 /// ```
18 /// use bpf_api::collections::HashMap;
19 ///
20 /// let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
21 /// ```
22 pub fn with_capacity(entries: u32) -> Result<Self, Error> {
23 Ok(Self {
24 map: Map::with_capacity(MapType::Hash, entries)?,
25 })
26 }
27
28 /// Retrieves the value for a given key.
29 ///
30 /// # Arguments
31 ///
32 /// * `key` - The key associated with the value to be retrieved.
33 ///
34 /// # Example
35 /// ```
36 /// use bpf_api::collections::HashMap;
37 ///
38 /// let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
39 /// assert!(matches!(hashmap.get(1000), Err(_)));
40 /// ```
41 pub fn get(&self, key: K) -> Result<V, Error> {
42 self.map.get(&key)
43 }
44
45 /// Sets the value for a given key.
46 ///
47 /// # Arguments
48 ///
49 /// * `key` - The key associated with the value to be set.
50 /// * `value` - The new value.
51 ///
52 /// # Example
53 /// ```
54 /// use bpf_api::collections::HashMap;
55 ///
56 /// let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
57 /// assert!(matches!(hashmap.get(1000), Err(_)));
58 /// assert!(matches!(hashmap.insert(1000, 0xdeadbeef), Ok(_)));
59 /// assert!(matches!(hashmap.get(1000), Ok(0xdeadbeef)));
60 /// ```
61 pub fn insert(&self, key: K, val: V) -> Result<(), Error> {
62 self.map.set(&key, &val)
63 }
64
65 /// Deletes an entry from the hash map given a key.
66 ///
67 /// # Arguments
68 ///
69 /// * `key` - The key of the entry to be deleted.
70 ///
71 /// # Example
72 /// ```
73 /// use bpf_api::collections::HashMap;
74 ///
75 /// let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
76 /// assert!(matches!(hashmap.get(1000), Err(_)));
77 /// assert!(matches!(hashmap.insert(1000, 0xdeadbeef), Ok(_)));
78 /// assert!(matches!(hashmap.get(1000), Ok(0xdeadbeef)));
79 /// assert!(matches!(hashmap.remove(1000), Ok(_)));
80 /// assert!(matches!(hashmap.remove(1000), Err(_)));
81 /// ```
82 pub fn remove(&self, key: K) -> Result<(), Error> {
83 self.map.del(&key)
84 }
85
86 /// Retrieve the BPF identifier for this map. This is the underlying file
87 /// descriptor that's used in eBPF programs.
88 ///
89 /// # Example
90 /// ```
91 /// use bpf_api::collections::HashMap;
92 ///
93 /// let hashmap = HashMap::<u32, u32>::with_capacity(10).expect("Failed to create hashmap");
94 /// hashmap.get_identifier();
95 /// ```
96 pub fn get_identifier(&self) -> u32 {
97 self.map.get_identifier()
98 }
99}