summaryrefslogtreecommitdiff
path: root/src/eval/str.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-16 18:52:26 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-16 19:15:03 +0200
commit9462fb17b390c57846b9215217ca7c32b649f0a5 (patch)
treebd6f96fea83c5e757c8f0eefefe5c0347784f00b /src/eval/str.rs
parentcb0aab3cfab2122a87d1d221290f7178b4291758 (diff)
Convert single-field structs to tuple structs
Diffstat (limited to 'src/eval/str.rs')
-rw-r--r--src/eval/str.rs46
1 files changed, 22 insertions, 24 deletions
diff --git a/src/eval/str.rs b/src/eval/str.rs
index 7f84f80f..1a0e3e1f 100644
--- a/src/eval/str.rs
+++ b/src/eval/str.rs
@@ -7,9 +7,7 @@ use crate::util::EcoString;
/// A string value with inline storage and clone-on-write semantics.
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
-pub struct Str {
- string: EcoString,
-}
+pub struct Str(EcoString);
impl Str {
/// Create a new, empty string.
@@ -19,17 +17,17 @@ impl Str {
/// Whether the string is empty.
pub fn is_empty(&self) -> bool {
- self.string.is_empty()
+ self.0.is_empty()
}
/// The length of the string in bytes.
pub fn len(&self) -> i64 {
- self.string.len() as i64
+ self.0.len() as i64
}
/// Borrow this as a string slice.
pub fn as_str(&self) -> &str {
- self.string.as_str()
+ self.0.as_str()
}
/// Return an iterator over the chars as strings.
@@ -41,10 +39,10 @@ impl Str {
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let n = usize::try_from(n)
.ok()
- .and_then(|n| self.string.len().checked_mul(n).map(|_| n))
+ .and_then(|n| self.0.len().checked_mul(n).map(|_| n))
.ok_or_else(|| format!("cannot repeat this string {} times", n))?;
- Ok(self.string.repeat(n).into())
+ Ok(self.0.repeat(n).into())
}
}
@@ -67,7 +65,7 @@ impl Display for Str {
impl Debug for Str {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- Debug::fmt(&self.string, f)
+ Debug::fmt(&self.0, f)
}
}
@@ -75,7 +73,7 @@ impl Deref for Str {
type Target = str;
fn deref(&self) -> &str {
- self.string.deref()
+ self.0.deref()
}
}
@@ -90,48 +88,48 @@ impl Add for Str {
impl AddAssign for Str {
fn add_assign(&mut self, rhs: Self) {
- self.string.push_str(rhs.as_str());
+ self.0.push_str(rhs.as_str());
}
}
impl From<char> for Str {
fn from(c: char) -> Self {
- Self { string: c.into() }
+ Self(c.into())
}
}
impl From<&str> for Str {
- fn from(string: &str) -> Self {
- Self { string: string.into() }
+ fn from(s: &str) -> Self {
+ Self(s.into())
}
}
impl From<String> for Str {
- fn from(string: String) -> Self {
- Self { string: string.into() }
+ fn from(s: String) -> Self {
+ Self(s.into())
}
}
impl From<EcoString> for Str {
- fn from(string: EcoString) -> Self {
- Self { string }
+ fn from(s: EcoString) -> Self {
+ Self(s)
}
}
impl From<&EcoString> for Str {
- fn from(string: &EcoString) -> Self {
- Self { string: string.clone() }
+ fn from(s: &EcoString) -> Self {
+ Self(s.clone())
}
}
impl From<Str> for EcoString {
- fn from(string: Str) -> Self {
- string.string
+ fn from(s: Str) -> Self {
+ s.0
}
}
impl From<&Str> for EcoString {
- fn from(string: &Str) -> Self {
- string.string.clone()
+ fn from(s: &Str) -> Self {
+ s.0.clone()
}
}