From c3cc92e352a2a05051a175307df4d67b8a40067d Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 6 Sep 2025 12:51:32 -0600 Subject: [PATCH] mcf: add `(Try)From` impls for `McfHashRef` and `&str` Adds impls for converting both `McfHash` and `McfHashRef` to/from `&str` --- mcf/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mcf/src/lib.rs b/mcf/src/lib.rs index c2ac6f204..c07d793ea 100644 --- a/mcf/src/lib.rs +++ b/mcf/src/lib.rs @@ -69,6 +69,27 @@ impl<'a> McfHashRef<'a> { } } +impl<'a> From> for &'a str { + fn from(hash: McfHashRef<'a>) -> &'a str { + hash.0 + } +} + +#[cfg(feature = "alloc")] +impl From> for alloc::string::String { + fn from(hash: McfHashRef<'_>) -> Self { + hash.0.into() + } +} + +impl<'a> TryFrom<&'a str> for McfHashRef<'a> { + type Error = Error; + + fn try_from(s: &'a str) -> Result { + Self::new(s) + } +} + #[cfg(feature = "alloc")] mod allocating { use crate::{Base64, Error, Field, Fields, McfHashRef, Result, fields, validate, validate_id}; @@ -177,6 +198,14 @@ mod allocating { } } + impl TryFrom<&str> for McfHash { + type Error = Error; + + fn try_from(s: &str) -> Result { + Self::new(s) + } + } + impl fmt::Display for McfHash { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.as_str())