Skip to content

Commit 9962bba

Browse files
committed
kernel_cmdline: Add some more ergonomic/convenience methods
Implement Deref / AsRef / Clone in a few places where it makes sense. Signed-off-by: John Eckersberg <[email protected]>
1 parent 0232a41 commit 9962bba

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

crates/kernel_cmdline/src/bytes.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! arguments, supporting both key-only switches and key-value pairs with proper quote handling.
55
66
use std::borrow::Cow;
7+
use std::ops::Deref;
78

89
use crate::{utf8, Action};
910

@@ -279,14 +280,24 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
279280
#[derive(Clone, Debug, Eq)]
280281
pub struct ParameterKey<'a>(pub(crate) &'a [u8]);
281282

282-
impl<'a> std::ops::Deref for ParameterKey<'a> {
283+
impl<'a> Deref for ParameterKey<'a> {
283284
type Target = [u8];
284285

285286
fn deref(&self) -> &'a Self::Target {
286287
self.0
287288
}
288289
}
289290

291+
impl<'a, T> AsRef<T> for ParameterKey<'a>
292+
where
293+
T: ?Sized,
294+
<ParameterKey<'a> as Deref>::Target: AsRef<T>,
295+
{
296+
fn as_ref(&self) -> &T {
297+
self.deref().as_ref()
298+
}
299+
}
300+
290301
impl<'a, T: AsRef<[u8]> + ?Sized> From<&'a T> for ParameterKey<'a> {
291302
fn from(s: &'a T) -> Self {
292303
Self(s.as_ref())
@@ -318,7 +329,7 @@ impl PartialEq for ParameterKey<'_> {
318329
}
319330

320331
/// A single kernel command line parameter.
321-
#[derive(Debug, Eq)]
332+
#[derive(Clone, Debug, Eq)]
322333
pub struct Parameter<'a> {
323334
/// The full original value
324335
parameter: &'a [u8],
@@ -424,6 +435,14 @@ impl<'a> PartialEq for Parameter<'a> {
424435
}
425436
}
426437

438+
impl<'a> std::ops::Deref for Parameter<'a> {
439+
type Target = [u8];
440+
441+
fn deref(&self) -> &'a Self::Target {
442+
self.parameter
443+
}
444+
}
445+
427446
#[cfg(test)]
428447
mod tests {
429448
use super::*;
@@ -713,10 +732,7 @@ mod tests {
713732
let mut kargs = Cmdline::from(b"console=tty0 console=ttyS1");
714733

715734
// add new parameter with duplicate key but different value
716-
assert!(matches!(
717-
kargs.add(&param("console=ttyS2")),
718-
Action::Added
719-
));
735+
assert!(matches!(kargs.add(&param("console=ttyS2")), Action::Added));
720736
let mut iter = kargs.iter();
721737
assert_eq!(iter.next(), Some(param("console=tty0")));
722738
assert_eq!(iter.next(), Some(param("console=ttyS1")));

crates/kernel_cmdline/src/utf8.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ impl<'a> std::ops::Deref for ParameterKey<'a> {
215215
}
216216
}
217217

218+
impl<'a, T> AsRef<T> for ParameterKey<'a>
219+
where
220+
T: ?Sized,
221+
<ParameterKey<'a> as Deref>::Target: AsRef<T>,
222+
{
223+
fn as_ref(&self) -> &T {
224+
self.deref().as_ref()
225+
}
226+
}
227+
218228
impl<'a> ParameterKey<'a> {
219229
/// Construct a utf8::ParameterKey from a bytes::ParameterKey
220230
///
@@ -249,7 +259,7 @@ impl PartialEq for ParameterKey<'_> {
249259
}
250260

251261
/// A single kernel command line parameter.
252-
#[derive(Debug, Eq)]
262+
#[derive(Clone, Debug, Eq)]
253263
pub struct Parameter<'a>(bytes::Parameter<'a>);
254264

255265
impl<'a> Parameter<'a> {
@@ -312,6 +322,13 @@ impl<'a> Parameter<'a> {
312322
str::from_utf8(p).expect("We only construct the underlying bytes from valid UTF-8")
313323
})
314324
}
325+
326+
/// Returns the parameter as a &str
327+
pub fn as_str(&'a self) -> &'a str {
328+
// SAFETY: We know this is valid UTF-8 since we only
329+
// construct the underlying `bytes` from valid UTF-8
330+
str::from_utf8(&self.0).expect("We only construct the underlying bytes from valid UTF-8")
331+
}
315332
}
316333

317334
impl<'a> TryFrom<bytes::Parameter<'a>> for Parameter<'a> {
@@ -347,6 +364,16 @@ impl<'a> std::fmt::Display for Parameter<'a> {
347364
}
348365
}
349366

367+
impl<'a> std::ops::Deref for Parameter<'a> {
368+
type Target = str;
369+
370+
fn deref(&self) -> &Self::Target {
371+
// SAFETY: We know this is valid UTF-8 since we only
372+
// construct the underlying `bytes` from valid UTF-8
373+
str::from_utf8(&self.0).expect("We only construct the underlying bytes from valid UTF-8")
374+
}
375+
}
376+
350377
impl<'a> PartialEq for Parameter<'a> {
351378
fn eq(&self, other: &Self) -> bool {
352379
self.0 == other.0
@@ -657,10 +684,7 @@ mod tests {
657684
let mut kargs = Cmdline::from("console=tty0 console=ttyS1");
658685

659686
// add new parameter with duplicate key but different value
660-
assert!(matches!(
661-
kargs.add(&param("console=ttyS2")),
662-
Action::Added
663-
));
687+
assert!(matches!(kargs.add(&param("console=ttyS2")), Action::Added));
664688
let mut iter = kargs.iter();
665689
assert_eq!(iter.next(), Some(param("console=tty0")));
666690
assert_eq!(iter.next(), Some(param("console=ttyS1")));

0 commit comments

Comments
 (0)