From 13b3df68c765e2a52ce09d1a5d3e830fa4cd124e Mon Sep 17 00:00:00 2001 From: Alex Hayton Date: Mon, 8 Jul 2024 19:16:34 +0100 Subject: [PATCH 1/3] Implement PartialOrd and Ord traits --- src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1779204..179451f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,6 +100,7 @@ #![cfg_attr(feature = "nightly", feature(const_trait_impl))] #![cfg_attr(not(feature = "std"), no_std)] +use core::cmp::Ordering; use core::fmt; use core::fmt::Write; use core::result::Result; @@ -139,6 +140,19 @@ impl From for FourCC { ]) } } +impl PartialOrd for FourCC { + fn partial_cmp(&self, other: &Self) -> Option { + // Implement comparison logic here, possibly using the inner FourCC value + // For example, if FourCC can be converted to something comparable: + self.to_string().partial_cmp(&other.to_string()) + } +} +impl Ord for FourCC { + fn cmp(&self, other: &Self) -> Ordering { + self.to_string().cmp(&other.to_string()) + } +} + // The macro is needed, because the `impl const` syntax doesn't exists on `stable`. #[cfg(not(feature = "nightly"))] macro_rules! from_fourcc_for_u32 { From 4b2a6e9eff425f9f55db38f005dae295cfe2227d Mon Sep 17 00:00:00 2001 From: Alex Hayton Date: Wed, 24 Jul 2024 13:57:15 +0100 Subject: [PATCH 2/3] Add FromStr --- src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 179451f..afd99a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,6 +104,7 @@ use core::cmp::Ordering; use core::fmt; use core::fmt::Write; use core::result::Result; +use core::str::FromStr; /// A _four-character-code_ value. /// @@ -152,6 +153,17 @@ impl Ord for FourCC { self.to_string().cmp(&other.to_string()) } } +impl FromStr for FourCC { + type Err = u32; + fn from_str(s: &str) -> Result { + if s.len() != 4 { + return Err(s.len() as u32); + } + let mut buf = [0u8; 4]; + buf.copy_from_slice(s.as_bytes()); + Ok(FourCC(buf)) + } +} // The macro is needed, because the `impl const` syntax doesn't exists on `stable`. #[cfg(not(feature = "nightly"))] From 836a62d192f6990b3050cbd2f3ffce6160d198ee Mon Sep 17 00:00:00 2001 From: Alex Hayton Date: Tue, 30 Jul 2024 12:25:38 +0100 Subject: [PATCH 3/3] Fix duplicate definition when serde feature is enabled --- src/lib.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index afd99a9..ec44bab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,14 +246,6 @@ impl FromStrVisitor { } } -#[cfg(feature = "serde")] -impl core::str::FromStr for FourCC { - type Err = u32; - fn from_str(s: &str) -> Result { - Ok(s.as_bytes().into()) - } -} - #[cfg(feature = "serde")] impl<'de, T> serde::de::Visitor<'de> for FromStrVisitor where