Skip to content

Commit 87b7d9d

Browse files
committed
kernel: Add a higher level find api
This is just a bit nicer for callers as especially by using `AsRef<[u8]>` they can pass a string and not a byte array as a lookup key. Signed-off-by: Colin Walters <[email protected]>
1 parent 92409e9 commit 87b7d9d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

crates/lib/src/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ struct RootMountInfo {
16631663
/// Discover how to mount the root filesystem, using existing kernel arguments and information
16641664
/// about the root mount.
16651665
fn find_root_args_to_inherit(cmdline: &Cmdline, root_info: &Filesystem) -> Result<RootMountInfo> {
1666-
let root = cmdline.iter().find(|p| p.key == b"root");
1666+
let root = cmdline.find("root");
16671667
// If we have a root= karg, then use that
16681668
let (mount_spec, kargs) = if let Some(root) = root {
16691669
let rootflags = cmdline

crates/lib/src/kernel.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ impl<'a> Cmdline<'a> {
3232
})
3333
.map(Parameter::from)
3434
}
35+
36+
/// Locate a kernel argument with the given key name.
37+
pub fn find(&'a self, key: impl AsRef<[u8]>) -> Option<Parameter<'a>> {
38+
let key = key.as_ref();
39+
self.iter().find(|p| p.key == key)
40+
}
3541
}
3642

3743
#[derive(Debug, Eq)]
@@ -237,6 +243,10 @@ mod tests {
237243
);
238244

239245
assert_eq!(iter.next(), None);
246+
247+
// Test the find API
248+
assert_eq!(kargs.find("foo").unwrap().value.unwrap(), b"bar");
249+
assert!(kargs.find("nothing").is_none());
240250
}
241251

242252
#[test]

0 commit comments

Comments
 (0)