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

Added `TryFrom<&'a RuntimeString> for &'a str`
This commit is contained in:
Michal Kucharczyk authored and GitHub committed 2024-04-02 16:06:01 +00:00
1 parent 5d9826c262
commit 0becc45bd8
1 file changed
+13
@@ -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 {