diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-01-31 16:06:44 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-01-31 16:47:00 +0100 |
| commit | 20b1a38414101f842a6d9201133a5aaaa45a7cec (patch) | |
| tree | 2365453d4dfdebfa11d618baad1a36c65b62d7c7 /src/util | |
| parent | fa57d86ed981373b66804972147bf59cab920e6b (diff) | |
Switch from `Rc` to `Arc`
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/eco_string.rs | 32 | ||||
| -rw-r--r-- | src/util/mod.rs | 8 |
2 files changed, 23 insertions, 17 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), } } } diff --git a/src/util/mod.rs b/src/util/mod.rs index 61e7131d..de37354e 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -11,7 +11,7 @@ use std::cell::RefMut; use std::cmp::Ordering; use std::ops::Range; use std::path::{Component, Path, PathBuf}; -use std::rc::Rc; +use std::sync::Arc; /// Additional methods for strings. pub trait StrExt { @@ -62,18 +62,18 @@ impl<T> OptionExt<T> for Option<T> { } /// Additional methods for reference-counted pointers. -pub trait RcExt<T> { +pub trait ArcExt<T> { /// Takes the inner value if there is exactly one strong reference and /// clones it otherwise. fn take(self) -> T; } -impl<T> RcExt<T> for Rc<T> +impl<T> ArcExt<T> for Arc<T> where T: Clone, { fn take(self) -> T { - match Rc::try_unwrap(self) { + match Arc::try_unwrap(self) { Ok(v) => v, Err(rc) => (*rc).clone(), } |
