Skip to content

Commit 70d4011

Browse files
phip1611nicholasbishop
authored andcommitted
strings: minor improvements for the EqStrUntilNul trait
1 parent 4c4555c commit 70d4011

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

uefi/src/data_types/owned_strs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl PartialEq<&CStr16> for CString16 {
128128
}
129129
}
130130

131-
impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CString16 {
131+
impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CString16 {
132132
fn eq_str_until_nul(&self, other: &StrType) -> bool {
133133
let this = self.as_ref();
134134
this.eq_str_until_nul(other)
@@ -196,6 +196,7 @@ mod tests {
196196
let input = CString16::try_from("test").unwrap();
197197

198198
// test various comparisons with different order (left, right)
199+
assert!(input.eq_str_until_nul("test")); // requires ?Sized constraint
199200
assert!(input.eq_str_until_nul(&"test"));
200201
assert!(input.eq_str_until_nul(&String::from("test")));
201202

uefi/src/data_types/strs.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl fmt::Display for CStr8 {
136136
}
137137
}
138138

139-
impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr8 {
139+
impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CStr8 {
140140
fn eq_str_until_nul(&self, other: &StrType) -> bool {
141141
let other = other.as_ref();
142142

@@ -147,7 +147,8 @@ impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr8 {
147147
.copied()
148148
.map(char::from)
149149
.zip(other.chars())
150-
// this only works as CStr8 is guaranteed to have a fixed character length
150+
// This only works as CStr8 is guaranteed to have a fixed character length
151+
// (unlike UTF-8).
151152
.take_while(|(l, r)| *l != '\0' && *r != '\0')
152153
.any(|(l, r)| l != r);
153154

@@ -345,7 +346,7 @@ impl CStr16 {
345346
}
346347
}
347348

348-
impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr16 {
349+
impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CStr16 {
349350
fn eq_str_until_nul(&self, other: &StrType) -> bool {
350351
let other = other.as_ref();
351352

@@ -354,7 +355,8 @@ impl<StrType: AsRef<str>> EqStrUntilNul<StrType> for CStr16 {
354355
.copied()
355356
.map(char::from)
356357
.zip(other.chars())
357-
// this only works as CStr16 is guaranteed to have a fixed character length
358+
// This only works as CStr16 is guaranteed to have a fixed character length
359+
// (unlike UTF-8 or UTF-16).
358360
.take_while(|(l, r)| *l != '\0' && *r != '\0')
359361
.any(|(l, r)| l != r);
360362

@@ -416,10 +418,11 @@ impl<'a> UnalignedSlice<'a, u16> {
416418
}
417419
}
418420

419-
/// Trait that helps to compare Rust strings against CStr16 types.
420-
/// A generic implementation of this trait enables us that we only have to
421-
/// implement one direction (`left.eq_str_until_nul(&right)`) and we get
422-
/// the other direction (`right.eq_str_until_nul(&left)`) for free.
421+
/// The EqStrUntilNul trait helps to compare Rust strings against UEFI string types (UCS-2 strings).
422+
/// The given generic implementation of this trait enables us that we only have to
423+
/// implement one direction (`left.eq_str_until_nul(&right)`) for each UEFI string type and we
424+
/// get the other direction (`right.eq_str_until_nul(&left)`) for free. Hence, the relation is
425+
/// reflexive.
423426
pub trait EqStrUntilNul<StrType: ?Sized> {
424427
/// Checks if the provided Rust string `StrType` is equal to [Self] until the first null-byte
425428
/// is found. An exception is the terminating null-byte of [Self] which is ignored.

0 commit comments

Comments
 (0)