Skip to content

Commit ea0e5f8

Browse files
committed
kernel_cmdline: Manually impl semantically-correct equivalence for Cmdline
Signed-off-by: John Eckersberg <[email protected]>
1 parent 1a613ca commit ea0e5f8

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

crates/kernel_cmdline/src/bytes.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};
1616
/// Wraps the raw command line bytes and provides methods for parsing and iterating
1717
/// over individual parameters. Uses copy-on-write semantics to avoid unnecessary
1818
/// allocations when working with borrowed data.
19-
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
19+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
2020
pub struct Cmdline<'a>(Cow<'a, [u8]>);
2121

2222
/// An owned Cmdline. Alias for `Cmdline<'static>`.
@@ -374,6 +374,21 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
374374
}
375375
}
376376

377+
impl PartialEq for Cmdline<'_> {
378+
fn eq(&self, other: &Self) -> bool {
379+
let our_params = self.iter().collect::<Vec<_>>();
380+
let their_params = other.iter().collect::<Vec<_>>();
381+
382+
if our_params.len() != their_params.len() {
383+
return false;
384+
}
385+
386+
our_params.iter().all(|p| their_params.contains(p))
387+
}
388+
}
389+
390+
impl Eq for Cmdline<'_> {}
391+
377392
/// A single kernel command line parameter key
378393
///
379394
/// Handles quoted values and treats dashes and underscores in keys as equivalent.
@@ -1066,4 +1081,21 @@ mod tests {
10661081

10671082
assert_eq!(params.len(), 0);
10681083
}
1084+
1085+
#[test]
1086+
fn test_cmdline_eq() {
1087+
// Ordering, quoting, and the whole dash-underscore
1088+
// equivalence thing shouldn't affect whether these are
1089+
// semantically equal
1090+
assert_eq!(
1091+
Cmdline::from("foo bar-with-delim=\"with spaces\""),
1092+
Cmdline::from("\"bar_with_delim=with spaces\" foo")
1093+
);
1094+
1095+
// Uneven lengths are not equal even if the parameters are. Or
1096+
// to put it another way, duplicate parameters break equality.
1097+
// Check with both orderings.
1098+
assert_ne!(Cmdline::from("foo"), Cmdline::from("foo foo"));
1099+
assert_ne!(Cmdline::from("foo foo"), Cmdline::from("foo"));
1100+
}
10691101
}

crates/kernel_cmdline/src/utf8.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,21 @@ mod tests {
928928
assert_eq!(params[1], param("baz=qux"));
929929
assert_eq!(params[2], param("wiz"));
930930
}
931+
932+
#[test]
933+
fn test_cmdline_eq() {
934+
// Ordering, quoting, and the whole dash-underscore
935+
// equivalence thing shouldn't affect whether these are
936+
// semantically equal
937+
assert_eq!(
938+
Cmdline::from("foo bar-with-delim=\"with spaces\""),
939+
Cmdline::from("\"bar_with_delim=with spaces\" foo")
940+
);
941+
942+
// Uneven lengths are not equal even if the parameters are. Or
943+
// to put it another way, duplicate parameters break equality.
944+
// Check with both orderings.
945+
assert_ne!(Cmdline::from("foo"), Cmdline::from("foo foo"));
946+
assert_ne!(Cmdline::from("foo foo"), Cmdline::from("foo"));
947+
}
931948
}

0 commit comments

Comments
 (0)