Skip to content

Commit f6812bd

Browse files
committed
kernel_cmdline: Implement Deref for Cmdline
Signed-off-by: John Eckersberg <[email protected]>
1 parent 7f10088 commit f6812bd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/kernel_cmdline/src/bytes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ impl<'a, T: AsRef<[u8]> + ?Sized> From<&'a T> for Cmdline<'a> {
3030
}
3131
}
3232

33+
impl Deref for Cmdline<'_> {
34+
type Target = [u8];
35+
36+
fn deref(&self) -> &Self::Target {
37+
&self.0
38+
}
39+
}
40+
3341
impl From<Vec<u8>> for CmdlineOwned {
3442
/// Creates a new `Cmdline` from an owned `Vec<u8>`.
3543
fn from(input: Vec<u8>) -> Self {

crates/kernel_cmdline/src/utf8.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ impl<'a> Cmdline<'a> {
213213
}
214214
}
215215

216+
impl Deref for Cmdline<'_> {
217+
type Target = str;
218+
219+
fn deref(&self) -> &Self::Target {
220+
// SAFETY: We know this is valid UTF-8 since we only
221+
// construct the underlying `bytes` from valid UTF-8
222+
str::from_utf8(&self.0).expect("We only construct the underlying bytes from valid UTF-8")
223+
}
224+
}
225+
216226
impl<'a> AsRef<str> for Cmdline<'a> {
217227
fn as_ref(&self) -> &str {
218228
str::from_utf8(self.0.as_ref())

0 commit comments

Comments
 (0)