diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index e8d2c463..1e48ebe3 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -101,6 +101,15 @@ impl, K: Pod, V: Pod> IterableMap for HashMap } } +impl HashMap { + /// Creates a `HashMap` from `MapData`, typically loaded from a pinned path. + /// + /// This is useful when accessing a map from user-space without reloading the entire BPF object. + pub fn from_map_data(map_data: MapData) -> Result { + Self::new(map_data) + } +} + #[cfg(test)] mod tests { use std::io; @@ -514,4 +523,12 @@ mod tests { assert_matches!(iter.next(), Some(Ok((30, 300)))); assert_matches!(iter.next(), None); } + + #[test] + fn test_from_map_data() { + let map = new_map(new_obj_map()); + let map_data = map; // already a MapData + let map = HashMap::<_, u32, u32>::from_map_data(map_data); + assert!(map.is_ok()); + } }