Skip to content

Commit c1e4a7a

Browse files
jeckersbcgwalters
authored andcommitted
kernel_cmdline: Standardize Deref/AsRef impls across types
Implement generic AsRef in terms of Deref as suggested in: https://doc.rust-lang.org/std/convert/trait.AsRef.html#generic-implementations I'm not 100% sold on this since in some case (like the change to the tests here) you have to end up doing `&*` coercion to get the right type, but this at least makes everything consistent. Would be a bit nicer when/if str_as_str[1] stabilizes, at least for the most common UTF-8 case. [1] rust-lang/rust#130366 Signed-off-by: John Eckersberg <[email protected]>
1 parent de988f0 commit c1e4a7a

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

crates/kernel_cmdline/src/bytes.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ impl Deref for Cmdline<'_> {
3939
}
4040
}
4141

42+
impl<'a, T> AsRef<T> for Cmdline<'a>
43+
where
44+
T: ?Sized,
45+
<Cmdline<'a> as Deref>::Target: AsRef<T>,
46+
{
47+
fn as_ref(&self) -> &T {
48+
self.deref().as_ref()
49+
}
50+
}
51+
4252
impl From<Vec<u8>> for CmdlineOwned {
4353
/// Creates a new `Cmdline` from an owned `Vec<u8>`.
4454
fn from(input: Vec<u8>) -> Self {
@@ -340,12 +350,6 @@ impl<'a> Cmdline<'a> {
340350
}
341351
}
342352

343-
impl<'a> AsRef<[u8]> for Cmdline<'a> {
344-
fn as_ref(&self) -> &[u8] {
345-
&self.0
346-
}
347-
}
348-
349353
impl<'a> IntoIterator for &'a Cmdline<'a> {
350354
type Item = Parameter<'a>;
351355
type IntoIter = CmdlineIter<'a>;
@@ -376,10 +380,10 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
376380
#[derive(Clone, Debug, Eq)]
377381
pub struct ParameterKey<'a>(pub(crate) &'a [u8]);
378382

379-
impl<'a> Deref for ParameterKey<'a> {
383+
impl Deref for ParameterKey<'_> {
380384
type Target = [u8];
381385

382-
fn deref(&self) -> &'a Self::Target {
386+
fn deref(&self) -> &Self::Target {
383387
self.0
384388
}
385389
}
@@ -509,14 +513,24 @@ impl<'a> PartialEq for Parameter<'a> {
509513
}
510514
}
511515

512-
impl<'a> std::ops::Deref for Parameter<'a> {
516+
impl Deref for Parameter<'_> {
513517
type Target = [u8];
514518

515-
fn deref(&self) -> &'a Self::Target {
519+
fn deref(&self) -> &Self::Target {
516520
self.parameter
517521
}
518522
}
519523

524+
impl<'a, T> AsRef<T> for Parameter<'a>
525+
where
526+
T: ?Sized,
527+
<Parameter<'a> as Deref>::Target: AsRef<T>,
528+
{
529+
fn as_ref(&self) -> &T {
530+
self.deref().as_ref()
531+
}
532+
}
533+
520534
#[cfg(test)]
521535
mod tests {
522536
use super::*;

crates/kernel_cmdline/src/utf8.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,13 @@ impl Deref for Cmdline<'_> {
224224
}
225225
}
226226

227-
impl<'a> AsRef<str> for Cmdline<'a> {
228-
fn as_ref(&self) -> &str {
229-
str::from_utf8(self.0.as_ref())
230-
.expect("We only construct the underlying bytes from valid UTF-8")
227+
impl<'a, T> AsRef<T> for Cmdline<'a>
228+
where
229+
T: ?Sized,
230+
<Cmdline<'a> as Deref>::Target: AsRef<T>,
231+
{
232+
fn as_ref(&self) -> &T {
233+
self.deref().as_ref()
231234
}
232235
}
233236

@@ -267,7 +270,7 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
267270
#[derive(Clone, Debug, Eq)]
268271
pub struct ParameterKey<'a>(bytes::ParameterKey<'a>);
269272

270-
impl<'a> Deref for ParameterKey<'a> {
273+
impl Deref for ParameterKey<'_> {
271274
type Target = str;
272275

273276
fn deref(&self) -> &Self::Target {
@@ -381,7 +384,7 @@ impl<'a> std::fmt::Display for Parameter<'a> {
381384
}
382385
}
383386

384-
impl<'a> Deref for Parameter<'a> {
387+
impl Deref for Parameter<'_> {
385388
type Target = str;
386389

387390
fn deref(&self) -> &Self::Target {
@@ -391,6 +394,16 @@ impl<'a> Deref for Parameter<'a> {
391394
}
392395
}
393396

397+
impl<'a, T> AsRef<T> for Parameter<'a>
398+
where
399+
T: ?Sized,
400+
<Parameter<'a> as Deref>::Target: AsRef<T>,
401+
{
402+
fn as_ref(&self) -> &T {
403+
self.deref().as_ref()
404+
}
405+
}
406+
394407
impl<'a> PartialEq for Parameter<'a> {
395408
fn eq(&self, other: &Self) -> bool {
396409
self.0 == other.0
@@ -769,7 +782,7 @@ mod tests {
769782
fn test_add_empty_cmdline() {
770783
let mut kargs = Cmdline::from("");
771784
assert!(matches!(kargs.add(&param("foo")), Action::Added));
772-
assert_eq!(kargs.as_ref(), "foo");
785+
assert_eq!(&*kargs, "foo");
773786
}
774787

775788
#[test]
@@ -809,7 +822,7 @@ mod tests {
809822
fn test_add_or_modify_empty_cmdline() {
810823
let mut kargs = Cmdline::from("");
811824
assert!(matches!(kargs.add_or_modify(&param("foo")), Action::Added));
812-
assert_eq!(kargs.as_ref(), "foo");
825+
assert_eq!(&*kargs, "foo");
813826
}
814827

815828
#[test]

0 commit comments

Comments
 (0)