summaryrefslogtreecommitdiff
path: root/src/util/eco_string.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-01-31 16:06:44 +0100
committerLaurenz <laurmaedje@gmail.com>2022-01-31 16:47:00 +0100
commit20b1a38414101f842a6d9201133a5aaaa45a7cec (patch)
tree2365453d4dfdebfa11d618baad1a36c65b62d7c7 /src/util/eco_string.rs
parentfa57d86ed981373b66804972147bf59cab920e6b (diff)
Switch from `Rc` to `Arc`
Diffstat (limited to 'src/util/eco_string.rs')
-rw-r--r--src/util/eco_string.rs32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/util/eco_string.rs b/src/util/eco_string.rs
index 8cb83377..953508ce 100644
--- a/src/util/eco_string.rs
+++ b/src/util/eco_string.rs
@@ -3,9 +3,9 @@ use std::cmp::Ordering;
use std::fmt::{self, Debug, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::ops::{Add, AddAssign, Deref};
-use std::rc::Rc;
+use std::sync::Arc;
-use super::RcExt;
+use super::ArcExt;
/// Create a new [`EcoString`] from a format string.
macro_rules! format_eco {
@@ -27,13 +27,13 @@ pub struct EcoString(Repr);
#[derive(Clone)]
enum Repr {
Small { buf: [u8; LIMIT], len: u8 },
- Large(Rc<String>),
+ Large(Arc<String>),
}
/// The maximum number of bytes that can be stored inline.
///
/// The value is chosen such that an `EcoString` fits exactly into 16 bytes
-/// (which are needed anyway due to the `Rc`s alignment, at least on 64-bit
+/// (which are needed anyway due to the `Arc`s alignment, at least on 64-bit
/// platforms).
///
/// Must be at least 4 to hold any char.
@@ -50,7 +50,7 @@ impl EcoString {
if capacity <= LIMIT {
Self::new()
} else {
- Self(Repr::Large(Rc::new(String::with_capacity(capacity))))
+ Self(Repr::Large(Arc::new(String::with_capacity(capacity))))
}
}
@@ -66,7 +66,7 @@ impl EcoString {
buf[.. len].copy_from_slice(slice.as_bytes());
Repr::Small { buf, len: len as u8 }
} else {
- Repr::Large(Rc::new(s.into()))
+ Repr::Large(Arc::new(s.into()))
})
}
@@ -100,7 +100,7 @@ impl EcoString {
self.push_str(c.encode_utf8(&mut [0; 4]));
}
}
- Repr::Large(rc) => Rc::make_mut(rc).push(c),
+ Repr::Large(rc) => Arc::make_mut(rc).push(c),
}
}
@@ -117,10 +117,10 @@ impl EcoString {
let mut spilled = String::with_capacity(new);
spilled.push_str(self);
spilled.push_str(string);
- self.0 = Repr::Large(Rc::new(spilled));
+ self.0 = Repr::Large(Arc::new(spilled));
}
}
- Repr::Large(rc) => Rc::make_mut(rc).push_str(string),
+ Repr::Large(rc) => Arc::make_mut(rc).push_str(string),
}
}
@@ -132,7 +132,7 @@ impl EcoString {
*len -= c.len_utf8() as u8;
}
Repr::Large(rc) => {
- Rc::make_mut(rc).pop();
+ Arc::make_mut(rc).pop();
}
}
Some(c)
@@ -143,8 +143,8 @@ impl EcoString {
match &mut self.0 {
Repr::Small { len, .. } => *len = 0,
Repr::Large(rc) => {
- if Rc::strong_count(rc) == 1 {
- Rc::make_mut(rc).clear();
+ if Arc::strong_count(rc) == 1 {
+ Arc::make_mut(rc).clear();
} else {
*self = Self::new();
}
@@ -351,11 +351,17 @@ impl From<String> for EcoString {
}
}
+impl From<&EcoString> for String {
+ fn from(s: &EcoString) -> Self {
+ s.as_str().to_owned()
+ }
+}
+
impl From<EcoString> for String {
fn from(s: EcoString) -> Self {
match s.0 {
Repr::Small { .. } => s.as_str().to_owned(),
- Repr::Large(rc) => Rc::take(rc),
+ Repr::Large(rc) => Arc::take(rc),
}
}
}