diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-07-26 23:24:50 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-07-26 23:27:44 +0200 |
| commit | fc574b39454aec77cf2c33270566225917c7c823 (patch) | |
| tree | ccebc217ce9f869bb0078753a7749789d77db551 /src/util | |
| parent | 1e9a5eda48c65096b482b396d550d139a4c2e61d (diff) | |
New `Str` type with methods
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/eco.rs | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/src/util/eco.rs b/src/util/eco.rs index 63abe9e7..10a1f2ed 100644 --- a/src/util/eco.rs +++ b/src/util/eco.rs @@ -1,4 +1,4 @@ -use std::borrow::Borrow; +use std::borrow::{Borrow, Cow}; use std::cmp::Ordering; use std::fmt::{self, Debug, Display, Formatter, Write}; use std::hash::{Hash, Hasher}; @@ -227,18 +227,7 @@ impl Default for EcoString { impl Debug for EcoString { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.write_char('"')?; - for c in self.chars() { - match c { - '\\' => f.write_str(r"\\")?, - '"' => f.write_str(r#"\""#)?, - '\n' => f.write_str(r"\n")?, - '\r' => f.write_str(r"\r")?, - '\t' => f.write_str(r"\t")?, - _ => f.write_char(c)?, - } - } - f.write_char('"') + Debug::fmt(self.as_str(), f) } } @@ -325,12 +314,6 @@ impl Borrow<str> for EcoString { } } -impl From<&Self> for EcoString { - fn from(s: &Self) -> Self { - s.clone() - } -} - impl From<char> for EcoString { fn from(c: char) -> Self { let mut buf = [0; LIMIT]; @@ -351,9 +334,22 @@ impl From<String> for EcoString { } } -impl From<&EcoString> for String { - fn from(s: &EcoString) -> Self { - s.as_str().to_owned() +impl From<Cow<'_, str>> for EcoString { + fn from(s: Cow<str>) -> Self { + match s { + Cow::Borrowed(s) => s.into(), + Cow::Owned(s) => s.into(), + } + } +} + +impl FromIterator<char> for EcoString { + fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self { + let mut s = Self::new(); + for c in iter { + s.push(c); + } + s } } @@ -366,13 +362,9 @@ impl From<EcoString> for String { } } -impl FromIterator<char> for EcoString { - fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self { - let mut s = Self::new(); - for c in iter { - s.push(c); - } - s +impl From<&EcoString> for String { + fn from(s: &EcoString) -> Self { + s.as_str().to_owned() } } |
