Add a `From` impl from `String` when using the "std" crate feature
Also `PartialEq` and `PartialOrd` impls
@@ -10,6 +10,9 @@ use core::ops::{
};
use core::str::{self, FromStr};
+#[cfg(feature = "std")]
+use alloc::string::String;
+
impl<const N: usize> Add<&str> for StaticString<N> {
type Output = Self;
@@ -132,6 +135,15 @@ impl<'a, const N: usize> From<&'a str> for StaticString<N> {
}
}
+impl<const N: usize> From<String> for StaticString<N> {
+ #[inline(always)]
+ fn from(string: String) -> Self {
+ Self {
+ vec: StaticVec::from_iter(string.into_bytes().iter()),
+ }
+ }
+}
+
impl<const N: usize> FromIterator<char> for StaticString<N> {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
@@ -293,6 +305,13 @@ impl<const N: usize> PartialEq<&str> for StaticString<N> {
}
}
+impl<const N: usize> PartialEq<String> for StaticString<N> {
+ #[inline(always)]
+ fn eq(&self, other: &String) -> bool {
+ self.as_str().eq(other.as_str())
+ }
+}
+
impl<const N: usize> PartialOrd for StaticString<N> {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
@@ -314,6 +333,13 @@ impl<const N: usize> PartialOrd<&str> for StaticString<N> {
}
}
+impl<const N: usize> PartialOrd<String> for StaticString<N> {
+ #[inline(always)]
+ fn partial_cmp(&self, other: &String) -> Option<Ordering> {
+ Some(self.as_str().cmp(other.as_str()))
+ }
+}
+
impl<const N: usize> Write for StaticString<N> {
#[inline(always)]
fn write_str(&mut self, s: &str) -> fmt::Result {
