Skip to content

Commit 02b06c0

Browse files
committed
kernel_cmdline: Consistently take AsRef + ?Sized references where possible
Signed-off-by: John Eckersberg <[email protected]>
1 parent f4dfd81 commit 02b06c0

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

crates/kernel_cmdline/src/bytes.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a> Cmdline<'a> {
7777
///
7878
/// Returns the first parameter matching the given key, or `None` if not found.
7979
/// Key comparison treats dashes and underscores as equivalent.
80-
pub fn find(&'a self, key: impl AsRef<[u8]>) -> Option<Parameter<'a>> {
80+
pub fn find<T: AsRef<[u8]> + ?Sized>(&'a self, key: &T) -> Option<Parameter<'a>> {
8181
let key = ParameterKey(key.as_ref());
8282
self.iter().find(|p| p.key == key)
8383
}
@@ -90,7 +90,10 @@ impl<'a> Cmdline<'a> {
9090
/// Otherwise, returns the first parameter matching the given key,
9191
/// or `None` if not found. Key comparison treats dashes and
9292
/// underscores as equivalent.
93-
pub fn find_utf8(&'a self, key: impl AsRef<str>) -> Result<Option<utf8::Parameter<'a>>> {
93+
pub fn find_utf8<T: AsRef<[u8]> + ?Sized>(
94+
&'a self,
95+
key: &T,
96+
) -> Result<Option<utf8::Parameter<'a>>> {
9497
let bytes = match self.find(key.as_ref()) {
9598
Some(p) => p,
9699
None => return Ok(None),
@@ -114,14 +117,14 @@ impl<'a> Cmdline<'a> {
114117
///
115118
/// Returns the first value matching the given key, or `None` if not found.
116119
/// Key comparison treats dashes and underscores as equivalent.
117-
pub fn value_of(&'a self, key: impl AsRef<[u8]>) -> Option<&'a [u8]> {
118-
self.find(key).and_then(|p| p.value)
120+
pub fn value_of<T: AsRef<[u8]> + ?Sized>(&'a self, key: &T) -> Option<&'a [u8]> {
121+
self.find(&key).and_then(|p| p.value)
119122
}
120123

121124
/// Find the value of the kernel argument with the provided name, which must be present.
122125
///
123126
/// Otherwise the same as [`Self::value_of`].
124-
pub fn require_value_of(&'a self, key: impl AsRef<[u8]>) -> Result<&'a [u8]> {
127+
pub fn require_value_of<T: AsRef<[u8]> + ?Sized>(&'a self, key: &T) -> Result<&'a [u8]> {
125128
let key = key.as_ref();
126129
self.value_of(key).ok_or_else(|| {
127130
let key = String::from_utf8_lossy(key);
@@ -273,8 +276,8 @@ impl<'a> Parameter<'a> {
273276
///
274277
/// Any remaining bytes not consumed from the input are returned
275278
/// as the second tuple item.
276-
pub fn parse(input: &'a [u8]) -> (Option<Self>, &'a [u8]) {
277-
let input = input.trim_ascii_start();
279+
pub fn parse<T: AsRef<[u8]> + ?Sized>(input: &'a T) -> (Option<Self>, &'a [u8]) {
280+
let input = input.as_ref().trim_ascii_start();
278281

279282
if input.is_empty() {
280283
return (None, input);

crates/kernel_cmdline/src/utf8.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,27 @@ impl<'a> Cmdline<'a> {
7272
///
7373
/// Returns the first parameter matching the given key, or `None` if not found.
7474
/// Key comparison treats dashes and underscores as equivalent.
75-
pub fn find(&'a self, key: impl AsRef<str>) -> Option<Parameter<'a>> {
75+
pub fn find<T: AsRef<str> + ?Sized>(&'a self, key: &T) -> Option<Parameter<'a>> {
7676
let key = ParameterKey::from(key.as_ref());
7777
self.iter().find(|p| p.key() == key)
7878
}
7979

8080
/// Find all kernel arguments starting with the given UTF-8 prefix.
8181
///
8282
/// This is a variant of [`Self::find`].
83-
pub fn find_all_starting_with(
83+
pub fn find_all_starting_with<T: AsRef<str> + ?Sized>(
8484
&'a self,
85-
prefix: &'a str,
85+
prefix: &'a T,
8686
) -> impl Iterator<Item = Parameter<'a>> + 'a {
87-
self.iter().filter(move |p| p.key().starts_with(prefix))
87+
self.iter()
88+
.filter(move |p| p.key().starts_with(prefix.as_ref()))
8889
}
8990

9091
/// Locate the value of the kernel argument with the given key name.
9192
///
9293
/// Returns the first value matching the given key, or `None` if not found.
9394
/// Key comparison treats dashes and underscores as equivalent.
94-
pub fn value_of(&'a self, key: impl AsRef<str>) -> Option<&'a str> {
95+
pub fn value_of<T: AsRef<str> + ?Sized>(&'a self, key: &T) -> Option<&'a str> {
9596
self.0.value_of(key.as_ref().as_bytes()).map(|v| {
9697
// SAFETY: We know this is valid UTF-8 since we only
9798
// construct the underlying `bytes` from valid UTF-8
@@ -102,7 +103,7 @@ impl<'a> Cmdline<'a> {
102103
/// Find the value of the kernel argument with the provided name, which must be present.
103104
///
104105
/// Otherwise the same as [`Self::value_of`].
105-
pub fn require_value_of(&'a self, key: impl AsRef<str>) -> Result<&'a str> {
106+
pub fn require_value_of<T: AsRef<str> + ?Sized>(&'a self, key: &T) -> Result<&'a str> {
106107
let key = key.as_ref();
107108
self.value_of(key)
108109
.ok_or_else(|| anyhow::anyhow!("Failed to find kernel argument '{key}'"))
@@ -159,9 +160,9 @@ impl<'a> ParameterKey<'a> {
159160
}
160161
}
161162

162-
impl<'a> From<&'a str> for ParameterKey<'a> {
163-
fn from(input: &'a str) -> Self {
164-
Self(bytes::ParameterKey(input.as_bytes()))
163+
impl<'a, T: AsRef<str> + ?Sized> From<&'a T> for ParameterKey<'a> {
164+
fn from(input: &'a T) -> Self {
165+
Self(bytes::ParameterKey(input.as_ref().as_bytes()))
165166
}
166167
}
167168

@@ -197,8 +198,8 @@ impl<'a> Parameter<'a> {
197198
///
198199
/// Any remaining characters not consumed from the input are
199200
/// returned as the second tuple item.
200-
pub fn parse(input: &'a str) -> (Option<Self>, &'a str) {
201-
let (bytes, rest) = bytes::Parameter::parse(input.as_bytes());
201+
pub fn parse<T: AsRef<str> + ?Sized>(input: &'a T) -> (Option<Self>, &'a str) {
202+
let (bytes, rest) = bytes::Parameter::parse(input.as_ref().as_bytes());
202203

203204
// SAFETY: we know this is valid UTF-8 since input is &str,
204205
// and `rest` is a subslice of that &str which was split on

0 commit comments

Comments
 (0)