@@ -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+
218228impl < ' 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 ) ]
253263pub struct Parameter < ' a > ( bytes:: Parameter < ' a > ) ;
254264
255265impl < ' 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
317334impl < ' 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+
350377impl < ' 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