Skip to content

Commit d9204b3

Browse files
committed
Implement Ord and PartialOrd for ShortFileName.
1 parent c3aef4a commit d9204b3

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/filesystem/filename.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl ToShortFileName for ShortFileName {
3333

3434
impl ToShortFileName for &ShortFileName {
3535
fn to_short_filename(self) -> Result<ShortFileName, FilenameError> {
36-
Ok(self.clone())
36+
Ok(*self)
3737
}
3838
}
3939

@@ -48,7 +48,7 @@ impl ToShortFileName for &str {
4848
/// ISO-8859-1 encoding is assumed. All lower-case is converted to upper-case by
4949
/// default.
5050
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
51-
#[derive(PartialEq, Eq, Clone)]
51+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
5252
pub struct ShortFileName {
5353
pub(crate) contents: [u8; Self::TOTAL_LEN],
5454
}
@@ -431,6 +431,40 @@ mod test {
431431
assert_eq!(sfn, ShortFileName::create_from_str("1.C").unwrap());
432432
}
433433

434+
#[test]
435+
fn filename_ordering() {
436+
assert!(
437+
ShortFileName::create_from_str("1.C").unwrap()
438+
< ShortFileName::create_from_str("2.C").unwrap()
439+
);
440+
assert!(
441+
ShortFileName::create_from_str("1.C").unwrap()
442+
< ShortFileName::create_from_str("1.D").unwrap()
443+
);
444+
assert!(
445+
ShortFileName::create_from_str("12.C").unwrap()
446+
< ShortFileName::create_from_str("3.C").unwrap()
447+
);
448+
assert!(
449+
ShortFileName::create_from_str("1.D").unwrap()
450+
< ShortFileName::create_from_str("12.C").unwrap()
451+
);
452+
assert_eq!(
453+
ShortFileName::create_from_str("1.D")
454+
.unwrap()
455+
.cmp(&ShortFileName::create_from_str("1.D").unwrap()),
456+
core::cmp::Ordering::Equal
457+
);
458+
assert!(
459+
ShortFileName::create_from_str("1").unwrap()
460+
< ShortFileName::create_from_str("1.C").unwrap()
461+
);
462+
assert!(
463+
ShortFileName::create_from_str("1.C").unwrap()
464+
< ShortFileName::create_from_str("2").unwrap()
465+
);
466+
}
467+
434468
#[test]
435469
fn filename_empty() {
436470
assert_eq!(

0 commit comments

Comments
 (0)