Skip to content

Commit be363a4

Browse files
atenartd-e-s-o
authored andcommitted
map: move keys() helper from Map to MapHandle
Before the introduction of MapHandle it was possible to create a Map using libbpf_rs and to iterate over its keys (with care). But now the helper is only available for Map as it was not moved alongside the other public functions. Fix this by moving the keys() helper to MapHandle, still allowing Map objects to use it thanks to the Deref impl. For the above to work, make MapKeyIter reference a MapHandle. Signed-off-by: Antoine Tenart <[email protected]>
1 parent 3211b1e commit be363a4

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

libbpf-rs/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Unreleased
2+
----------
3+
- Enabled key iteration on `MapHandle` objects (formerly possible only on `Map`
4+
objects)
5+
6+
17
0.21.1
28
------
39
- Fixed build failures on 32 bit x86 and aarch32

libbpf-rs/src/map.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,6 @@ impl Map {
285285
util::parse_ret(ret)
286286
}
287287

288-
/// Returns an iterator over keys in this map
289-
///
290-
/// Note that if the map is not stable (stable meaning no updates or deletes) during iteration,
291-
/// iteration can skip keys, restart from the beginning, or duplicate keys. In other words,
292-
/// iteration becomes unpredictable.
293-
pub fn keys(&self) -> MapKeyIter<'_> {
294-
MapKeyIter::new(self, self.key_size())
295-
}
296-
297288
/// Attach a struct ops map
298289
pub fn attach_struct_ops(&self) -> Result<Link> {
299290
if self.map_type() != MapType::StructOps {
@@ -842,6 +833,15 @@ impl MapHandle {
842833
Err(e) => Err(Error::Internal(format!("remove pin map failed: {e}"))),
843834
}
844835
}
836+
837+
/// Returns an iterator over keys in this map
838+
///
839+
/// Note that if the map is not stable (stable meaning no updates or deletes) during iteration,
840+
/// iteration can skip keys, restart from the beginning, or duplicate keys. In other words,
841+
/// iteration becomes unpredictable.
842+
pub fn keys(&self) -> MapKeyIter<'_> {
843+
MapKeyIter::new(self, self.key_size())
844+
}
845845
}
846846

847847
impl AsFd for MapHandle {
@@ -938,13 +938,13 @@ impl MapType {
938938

939939
#[derive(Debug)]
940940
pub struct MapKeyIter<'a> {
941-
map: &'a Map,
941+
map: &'a MapHandle,
942942
prev: Option<Vec<u8>>,
943943
next: Vec<u8>,
944944
}
945945

946946
impl<'a> MapKeyIter<'a> {
947-
fn new(map: &'a Map, key_size: u32) -> Self {
947+
fn new(map: &'a MapHandle, key_size: u32) -> Self {
948948
Self {
949949
map,
950950
prev: None,

0 commit comments

Comments
 (0)