sp_runtime: TryFrom<RuntimeString> for &str (#3942)

Added `TryFrom<&'a RuntimeString> for &'a str`
This commit is contained in:
Michal Kucharczyk
2024-04-02 18:06:01 +02:00
committed by GitHub
parent 5d9826c262
commit 0becc45bd8
@@ -61,6 +61,19 @@ impl From<&'static str> for RuntimeString {
}
}
impl<'a> TryFrom<&'a RuntimeString> for &'a str {
type Error = core::str::Utf8Error;
fn try_from(from: &'a RuntimeString) -> core::result::Result<&'a str, Self::Error> {
match from {
#[cfg(feature = "std")]
RuntimeString::Owned(string) => Ok(string.as_str()),
#[cfg(not(feature = "std"))]
RuntimeString::Owned(vec) => core::str::from_utf8(&vec),
RuntimeString::Borrowed(str) => Ok(str),
}
}
}
#[cfg(feature = "std")]
impl From<RuntimeString> for String {
fn from(string: RuntimeString) -> Self {