Skip to content

Commit 809b18b

Browse files
jeckersbcgwalters
authored andcommitted
kernel_cmdline: Manually impl semantically-correct equivalence for Cmdline
Signed-off-by: John Eckersberg <[email protected]>
1 parent 35e64a4 commit 809b18b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

crates/kernel_cmdline/src/bytes.rs

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

2323
/// An owned Cmdline. Alias for `Cmdline<'static>`.
@@ -375,6 +375,19 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
375375
}
376376
}
377377

378+
impl PartialEq for Cmdline<'_> {
379+
fn eq(&self, other: &Self) -> bool {
380+
let mut our_params = self.iter().collect::<Vec<_>>();
381+
our_params.sort();
382+
let mut their_params = other.iter().collect::<Vec<_>>();
383+
their_params.sort();
384+
385+
our_params == their_params
386+
}
387+
}
388+
389+
impl Eq for Cmdline<'_> {}
390+
378391
/// A single kernel command line parameter key
379392
///
380393
/// Handles quoted values and treats dashes and underscores in keys as equivalent.
@@ -1091,4 +1104,24 @@ mod tests {
10911104

10921105
assert_eq!(params.len(), 0);
10931106
}
1107+
1108+
#[test]
1109+
fn test_cmdline_eq() {
1110+
// Ordering, quoting, and the whole dash-underscore
1111+
// equivalence thing shouldn't affect whether these are
1112+
// semantically equal
1113+
assert_eq!(
1114+
Cmdline::from("foo bar-with-delim=\"with spaces\""),
1115+
Cmdline::from("\"bar_with_delim=with spaces\" foo")
1116+
);
1117+
1118+
// Uneven lengths are not equal even if the parameters are. Or
1119+
// to put it another way, duplicate parameters break equality.
1120+
// Check with both orderings.
1121+
assert_ne!(Cmdline::from("foo"), Cmdline::from("foo foo"));
1122+
assert_ne!(Cmdline::from("foo foo"), Cmdline::from("foo"));
1123+
1124+
// Equal lengths but differing duplicates are also not equal
1125+
assert_ne!(Cmdline::from("a a b"), Cmdline::from("a b b"));
1126+
}
10941127
}

crates/kernel_cmdline/src/utf8.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,24 @@ 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+
948+
// Equal lengths but differing duplicates are also not equal
949+
assert_ne!(Cmdline::from("a a b"), Cmdline::from("a b b"));
950+
}
931951
}

0 commit comments

Comments
 (0)