From 20b1a38414101f842a6d9201133a5aaaa45a7cec Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 31 Jan 2022 16:06:44 +0100 Subject: Switch from `Rc` to `Arc` --- src/eval/array.rs | 26 +++++++++++++------------- src/eval/capture.rs | 4 ++-- src/eval/dict.rs | 24 ++++++++++++------------ src/eval/function.rs | 12 ++++++------ src/eval/node.rs | 4 ++-- src/eval/scope.rs | 8 ++++---- src/eval/styles.rs | 12 ++++++------ src/eval/value.rs | 16 ++++++++-------- 8 files changed, 53 insertions(+), 53 deletions(-) (limited to 'src/eval') diff --git a/src/eval/array.rs b/src/eval/array.rs index 912aa3c0..45f6fcc7 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -3,11 +3,11 @@ use std::convert::TryFrom; use std::fmt::{self, Debug, Formatter, Write}; use std::iter::FromIterator; use std::ops::{Add, AddAssign}; -use std::rc::Rc; +use std::sync::Arc; use super::Value; use crate::diag::StrResult; -use crate::util::RcExt; +use crate::util::ArcExt; /// Create a new [`Array`] from values. #[allow(unused_macros)] @@ -23,7 +23,7 @@ macro_rules! array { /// An array of values with clone-on-write value semantics. #[derive(Default, Clone, PartialEq)] -pub struct Array(Rc>); +pub struct Array(Arc>); impl Array { /// Create a new, empty array. @@ -33,7 +33,7 @@ impl Array { /// Create a new array from a vector of values. pub fn from_vec(vec: Vec) -> Self { - Self(Rc::new(vec)) + Self(Arc::new(vec)) } /// Whether the array is empty. @@ -59,19 +59,19 @@ impl Array { let len = self.len(); usize::try_from(index) .ok() - .and_then(move |i| Rc::make_mut(&mut self.0).get_mut(i)) + .and_then(move |i| Arc::make_mut(&mut self.0).get_mut(i)) .ok_or_else(|| out_of_bounds(index, len)) } /// Push a value to the end of the array. pub fn push(&mut self, value: Value) { - Rc::make_mut(&mut self.0).push(value); + Arc::make_mut(&mut self.0).push(value); } /// Clear the array. pub fn clear(&mut self) { - if Rc::strong_count(&self.0) == 1 { - Rc::make_mut(&mut self.0).clear(); + if Arc::strong_count(&self.0) == 1 { + Arc::make_mut(&mut self.0).clear(); } else { *self = Self::new(); } @@ -87,7 +87,7 @@ impl Array { /// Returns an error if two values could not be compared. pub fn sorted(mut self) -> StrResult { let mut result = Ok(()); - Rc::make_mut(&mut self.0).sort_by(|a, b| { + Arc::make_mut(&mut self.0).sort_by(|a, b| { a.partial_cmp(b).unwrap_or_else(|| { if result.is_ok() { result = Err(format!( @@ -146,7 +146,7 @@ impl Add for Array { impl AddAssign for Array { fn add_assign(&mut self, rhs: Array) { - match Rc::try_unwrap(rhs.0) { + match Arc::try_unwrap(rhs.0) { Ok(vec) => self.extend(vec), Err(rc) => self.extend(rc.iter().cloned()), } @@ -155,13 +155,13 @@ impl AddAssign for Array { impl Extend for Array { fn extend>(&mut self, iter: T) { - Rc::make_mut(&mut self.0).extend(iter); + Arc::make_mut(&mut self.0).extend(iter); } } impl FromIterator for Array { fn from_iter>(iter: T) -> Self { - Self(Rc::new(iter.into_iter().collect())) + Self(Arc::new(iter.into_iter().collect())) } } @@ -170,7 +170,7 @@ impl IntoIterator for Array { type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { - Rc::take(self.0).into_iter() + Arc::take(self.0).into_iter() } } diff --git a/src/eval/capture.rs b/src/eval/capture.rs index e47831df..8585776a 100644 --- a/src/eval/capture.rs +++ b/src/eval/capture.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use super::{Scope, Scopes, Value}; use crate::syntax::ast::{ClosureParam, Expr, Ident, Imports, TypedNode}; @@ -35,7 +35,7 @@ impl<'a> CapturesVisitor<'a> { pub fn capture(&mut self, ident: Ident) { if self.internal.get(&ident).is_none() { if let Some(slot) = self.external.get(&ident) { - self.captures.def_slot(ident.take(), Rc::clone(slot)); + self.captures.def_slot(ident.take(), Arc::clone(slot)); } } } diff --git a/src/eval/dict.rs b/src/eval/dict.rs index 0d7198e1..2ce3c3f9 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -2,11 +2,11 @@ use std::collections::BTreeMap; use std::fmt::{self, Debug, Formatter, Write}; use std::iter::FromIterator; use std::ops::{Add, AddAssign}; -use std::rc::Rc; +use std::sync::Arc; use super::Value; use crate::diag::StrResult; -use crate::util::{EcoString, RcExt}; +use crate::util::{ArcExt, EcoString}; /// Create a new [`Dict`] from key-value pairs. #[allow(unused_macros)] @@ -21,7 +21,7 @@ macro_rules! dict { /// A dictionary from strings to values with clone-on-write value semantics. #[derive(Default, Clone, PartialEq)] -pub struct Dict(Rc>); +pub struct Dict(Arc>); impl Dict { /// Create a new, empty dictionary. @@ -31,7 +31,7 @@ impl Dict { /// Create a new dictionary from a mapping of strings to values. pub fn from_map(map: BTreeMap) -> Self { - Self(Rc::new(map)) + Self(Arc::new(map)) } /// Whether the dictionary is empty. @@ -54,18 +54,18 @@ impl Dict { /// This inserts the key with [`None`](Value::None) as the value if not /// present so far. pub fn get_mut(&mut self, key: EcoString) -> &mut Value { - Rc::make_mut(&mut self.0).entry(key).or_default() + Arc::make_mut(&mut self.0).entry(key).or_default() } /// Insert a mapping from the given `key` to the given `value`. pub fn insert(&mut self, key: EcoString, value: Value) { - Rc::make_mut(&mut self.0).insert(key, value); + Arc::make_mut(&mut self.0).insert(key, value); } /// Clear the dictionary. pub fn clear(&mut self) { - if Rc::strong_count(&self.0) == 1 { - Rc::make_mut(&mut self.0).clear(); + if Arc::strong_count(&self.0) == 1 { + Arc::make_mut(&mut self.0).clear(); } else { *self = Self::new(); } @@ -112,7 +112,7 @@ impl Add for Dict { impl AddAssign for Dict { fn add_assign(&mut self, rhs: Dict) { - match Rc::try_unwrap(rhs.0) { + match Arc::try_unwrap(rhs.0) { Ok(map) => self.extend(map), Err(rc) => self.extend(rc.iter().map(|(k, v)| (k.clone(), v.clone()))), } @@ -121,13 +121,13 @@ impl AddAssign for Dict { impl Extend<(EcoString, Value)> for Dict { fn extend>(&mut self, iter: T) { - Rc::make_mut(&mut self.0).extend(iter); + Arc::make_mut(&mut self.0).extend(iter); } } impl FromIterator<(EcoString, Value)> for Dict { fn from_iter>(iter: T) -> Self { - Self(Rc::new(iter.into_iter().collect())) + Self(Arc::new(iter.into_iter().collect())) } } @@ -136,7 +136,7 @@ impl IntoIterator for Dict { type IntoIter = std::collections::btree_map::IntoIter; fn into_iter(self) -> Self::IntoIter { - Rc::take(self.0).into_iter() + Arc::take(self.0).into_iter() } } diff --git a/src/eval/function.rs b/src/eval/function.rs index 931a90a0..0edc1e78 100644 --- a/src/eval/function.rs +++ b/src/eval/function.rs @@ -1,5 +1,5 @@ use std::fmt::{self, Debug, Formatter, Write}; -use std::rc::Rc; +use std::sync::Arc; use super::{Cast, EvalContext, Value}; use crate::diag::{At, TypResult}; @@ -8,9 +8,9 @@ use crate::util::EcoString; /// An evaluatable function. #[derive(Clone)] -pub struct Function(Rc>); +pub struct Function(Arc>); -/// The unsized structure behind the [`Rc`]. +/// The unsized structure behind the [`Arc`]. struct Inner { name: Option, func: T, @@ -24,7 +24,7 @@ impl Function { where F: Fn(&mut EvalContext, &mut Args) -> TypResult + 'static, { - Self(Rc::new(Inner { name, func })) + Self(Arc::new(Inner { name, func })) } /// The name of the function. @@ -53,8 +53,8 @@ impl PartialEq for Function { fn eq(&self, other: &Self) -> bool { // We cast to thin pointers for comparison. std::ptr::eq( - Rc::as_ptr(&self.0) as *const (), - Rc::as_ptr(&other.0) as *const (), + Arc::as_ptr(&self.0) as *const (), + Arc::as_ptr(&other.0) as *const (), ) } } diff --git a/src/eval/node.rs b/src/eval/node.rs index d909fc7d..665550b0 100644 --- a/src/eval/node.rs +++ b/src/eval/node.rs @@ -71,7 +71,7 @@ impl Node { /// Create an inline-level node. pub fn inline(node: T) -> Self where - T: Layout + Debug + Hash + 'static, + T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Inline(node.pack()) } @@ -79,7 +79,7 @@ impl Node { /// Create a block-level node. pub fn block(node: T) -> Self where - T: Layout + Debug + Hash + 'static, + T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Block(node.pack()) } diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 5178c819..34da68d4 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -2,14 +2,14 @@ use std::cell::RefCell; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; use std::iter; -use std::rc::Rc; +use std::sync::Arc; use super::{Args, Class, Construct, EvalContext, Function, Set, Value}; use crate::diag::TypResult; use crate::util::EcoString; /// A slot where a variable is stored. -pub type Slot = Rc>; +pub type Slot = Arc>; /// A stack of scopes. #[derive(Debug, Default, Clone)] @@ -85,12 +85,12 @@ impl Scope { // FIXME: Use Ref::leak once stable. std::mem::forget(cell.borrow()); - self.values.insert(var.into(), Rc::new(cell)); + self.values.insert(var.into(), Arc::new(cell)); } /// Define a mutable variable with a value. pub fn def_mut(&mut self, var: impl Into, value: impl Into) { - self.values.insert(var.into(), Rc::new(RefCell::new(value.into()))); + self.values.insert(var.into(), Arc::new(RefCell::new(value.into()))); } /// Define a variable with a slot. diff --git a/src/eval/styles.rs b/src/eval/styles.rs index 508996a1..5276353a 100644 --- a/src/eval/styles.rs +++ b/src/eval/styles.rs @@ -1,7 +1,7 @@ use std::any::{Any, TypeId}; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; -use std::rc::Rc; +use std::sync::Arc; // TODO(style): Possible optimizations: // - Ref-count map for cheaper cloning and smaller footprint @@ -334,13 +334,13 @@ impl Debug for Link<'_> { /// An entry for a single style property. #[derive(Clone)] struct Entry { - p: Rc, + p: Arc, scoped: bool, } impl Entry { fn new(key: P, value: P::Value) -> Self { - Self { p: Rc::new((key, value)), scoped: false } + Self { p: Arc::new((key, value)), scoped: false } } fn is(&self) -> bool { @@ -390,11 +390,11 @@ impl Hash for Entry { /// /// This trait is not intended to be implemented manually, but rather through /// the `#[properties]` proc-macro. -pub trait Property: Copy + 'static { +pub trait Property: Copy + Sync + Send + 'static { /// The type of value that is returned when getting this property from a /// style map. For example, this could be [`Length`](crate::geom::Length) /// for a `WIDTH` property. - type Value: Debug + Clone + PartialEq + Hash + 'static; + type Value: Debug + Clone + PartialEq + Hash + Sync + Send + 'static; /// The name of the property, used for debug printing. const NAME: &'static str; @@ -432,7 +432,7 @@ pub trait Nonfolding {} /// value types below. Although it is zero-sized, the property `P` must be part /// of the implementing type so that we can use it in the methods (it must be a /// constrained type parameter). -trait Bounds: 'static { +trait Bounds: Sync + Send + 'static { fn as_any(&self) -> &dyn Any; fn dyn_fmt(&self, f: &mut Formatter) -> fmt::Result; fn dyn_eq(&self, other: &Entry) -> bool; diff --git a/src/eval/value.rs b/src/eval/value.rs index e64f6cc6..7d65d5af 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -2,7 +2,7 @@ use std::any::Any; use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; -use std::rc::Rc; +use std::sync::Arc; use super::{ops, Args, Array, Class, Dict, Function, Node}; use crate::diag::StrResult; @@ -58,7 +58,7 @@ impl Value { /// Create an inline-level node value. pub fn inline(node: T) -> Self where - T: Layout + Debug + Hash + 'static, + T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Node(Node::inline(node)) } @@ -66,7 +66,7 @@ impl Value { /// Create a block-level node value. pub fn block(node: T) -> Self where - T: Layout + Debug + Hash + 'static, + T: Layout + Debug + Hash + Sync + Send + 'static, { Self::Node(Node::block(node)) } @@ -211,15 +211,15 @@ impl From for Value { /// A dynamic value. #[derive(Clone)] -pub struct Dynamic(Rc); +pub struct Dynamic(Arc); impl Dynamic { /// Create a new instance from any value that satisifies the required bounds. pub fn new(any: T) -> Self where - T: Type + Debug + PartialEq + 'static, + T: Type + Debug + PartialEq + Sync + Send + 'static, { - Self(Rc::new(any)) + Self(Arc::new(any)) } /// Whether the wrapped type is `T`. @@ -250,7 +250,7 @@ impl PartialEq for Dynamic { } } -trait Bounds: Debug + 'static { +trait Bounds: Debug + Sync + Send + 'static { fn as_any(&self) -> &dyn Any; fn dyn_eq(&self, other: &Dynamic) -> bool; fn dyn_type_name(&self) -> &'static str; @@ -258,7 +258,7 @@ trait Bounds: Debug + 'static { impl Bounds for T where - T: Type + Debug + PartialEq + 'static, + T: Type + Debug + PartialEq + Sync + Send + 'static, { fn as_any(&self) -> &dyn Any { self -- cgit v1.2.3