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/layout/constraints.rs | 8 ++++---- src/layout/incremental.rs | 14 +++++++------- src/layout/mod.rs | 41 +++++++++++++++++++---------------------- 3 files changed, 30 insertions(+), 33 deletions(-) (limited to 'src/layout') diff --git a/src/layout/constraints.rs b/src/layout/constraints.rs index 0d772cea..3bdbc4bc 100644 --- a/src/layout/constraints.rs +++ b/src/layout/constraints.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use super::Regions; use crate::frame::Frame; @@ -7,12 +7,12 @@ use crate::geom::{Length, Size, Spec}; /// Constrain a frame with constraints. pub trait Constrain { /// Reference-count the frame and wrap it with constraints. - fn constrain(self, cts: Constraints) -> Constrained>; + fn constrain(self, cts: Constraints) -> Constrained>; } impl Constrain for Frame { - fn constrain(self, cts: Constraints) -> Constrained> { - Constrained::new(Rc::new(self), cts) + fn constrain(self, cts: Constraints) -> Constrained> { + Constrained::new(Arc::new(self), cts) } } diff --git a/src/layout/incremental.rs b/src/layout/incremental.rs index 63915b53..2112fef1 100644 --- a/src/layout/incremental.rs +++ b/src/layout/incremental.rs @@ -1,6 +1,6 @@ use std::cmp::Reverse; use std::collections::HashMap; -use std::rc::Rc; +use std::sync::Arc; use itertools::Itertools; @@ -65,7 +65,7 @@ impl LayoutCache { &mut self, hash: u64, regions: &Regions, - ) -> Option>>> { + ) -> Option>>> { self.frames .get_mut(&hash)? .iter_mut() @@ -193,7 +193,7 @@ impl LayoutCache { #[derive(Debug, Clone)] pub struct FramesEntry { /// The cached frames for a node. - frames: Vec>>, + frames: Vec>>, /// How nested the frame was in the context is was originally appearing in. level: usize, /// For how long the element already exists. @@ -209,7 +209,7 @@ pub struct FramesEntry { impl FramesEntry { /// Construct a new instance. - pub fn new(frames: Vec>>, level: usize) -> Self { + pub fn new(frames: Vec>>, level: usize) -> Self { Self { frames, level, @@ -222,7 +222,7 @@ impl FramesEntry { /// Checks if the cached frames are valid in the given regions and returns /// them if so. - pub fn lookup(&mut self, regions: &Regions) -> Option>>> { + pub fn lookup(&mut self, regions: &Regions) -> Option>>> { self.check(regions).then(|| { self.temperature[0] = self.temperature[0].saturating_add(1); self.frames.clone() @@ -396,9 +396,9 @@ mod tests { use crate::geom::{Size, Spec}; use crate::layout::Constraints; - fn empty_frames() -> Vec>> { + fn empty_frames() -> Vec>> { vec![Constrained { - item: Rc::new(Frame::default()), + item: Arc::new(Frame::default()), cts: Constraints::new(Spec::splat(false)), }] } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index e4c29f9b..147e8c9d 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -13,7 +13,7 @@ pub use regions::*; use std::any::Any; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; -use std::rc::Rc; +use std::sync::Arc; use crate::eval::{StyleChain, Styled}; use crate::font::FontStore; @@ -29,7 +29,7 @@ pub struct RootNode(pub Vec>); impl RootNode { /// Layout the document into a sequence of frames, one per page. - pub fn layout(&self, ctx: &mut Context) -> Vec> { + pub fn layout(&self, ctx: &mut Context) -> Vec> { let (mut ctx, styles) = LayoutContext::new(ctx); self.0 .iter() @@ -56,17 +56,17 @@ pub trait Layout { ctx: &mut LayoutContext, regions: &Regions, styles: StyleChain, - ) -> Vec>>; + ) -> Vec>>; /// Convert to a packed node. fn pack(self) -> PackedNode where - Self: Debug + Hash + Sized + 'static, + Self: Debug + Hash + Sized + Sync + Send + 'static, { PackedNode { #[cfg(feature = "layout-cache")] hash: self.hash64(), - node: Rc::new(self), + node: Arc::new(self), } } } @@ -112,7 +112,7 @@ impl Layout for EmptyNode { _: &mut LayoutContext, regions: &Regions, _: StyleChain, - ) -> Vec>> { + ) -> Vec>> { let size = regions.expand.select(regions.current, Size::zero()); let mut cts = Constraints::new(regions.expand); cts.exact = regions.current.filter(regions.expand); @@ -124,7 +124,7 @@ impl Layout for EmptyNode { #[derive(Clone)] pub struct PackedNode { /// The type-erased node. - node: Rc, + node: Arc, /// A precomputed hash for the node. #[cfg(feature = "layout-cache")] hash: u64, @@ -205,7 +205,7 @@ impl Layout for PackedNode { ctx: &mut LayoutContext, regions: &Regions, styles: StyleChain, - ) -> Vec>> { + ) -> Vec>> { let styles = styles.barred(self.node.as_any().type_id()); #[cfg(not(feature = "layout-cache"))] @@ -243,10 +243,7 @@ impl Layout for PackedNode { }) } - fn pack(self) -> PackedNode - where - Self: Sized + Hash + 'static, - { + fn pack(self) -> PackedNode { self } } @@ -266,8 +263,8 @@ impl Debug for PackedNode { impl PartialEq for PackedNode { fn eq(&self, other: &Self) -> bool { std::ptr::eq( - Rc::as_ptr(&self.node) as *const (), - Rc::as_ptr(&other.node) as *const (), + Arc::as_ptr(&self.node) as *const (), + Arc::as_ptr(&other.node) as *const (), ) } } @@ -282,14 +279,14 @@ impl Hash for PackedNode { } } -trait Bounds: Layout + Debug + 'static { +trait Bounds: Layout + Debug + Sync + Send + 'static { fn as_any(&self) -> &dyn Any; fn hash64(&self) -> u64; } impl Bounds for T where - T: Layout + Hash + Debug + 'static, + T: Layout + Hash + Debug + Sync + Send + 'static, { fn as_any(&self) -> &dyn Any { self @@ -320,7 +317,7 @@ impl Layout for SizedNode { ctx: &mut LayoutContext, regions: &Regions, styles: StyleChain, - ) -> Vec>> { + ) -> Vec>> { let is_auto = self.sizing.map_is_none(); let is_rel = self.sizing.map(|s| s.map_or(false, Linear::is_relative)); @@ -346,7 +343,7 @@ impl Layout for SizedNode { // Ensure frame size matches regions size if expansion is on. let target = regions.expand.select(regions.current, frame.size); - Rc::make_mut(frame).resize(target, Align::LEFT_TOP); + Arc::make_mut(frame).resize(target, Align::LEFT_TOP); // Set base & exact constraints if the child is automatically sized // since we don't know what the child might have done. Also set base if @@ -374,11 +371,11 @@ impl Layout for FillNode { ctx: &mut LayoutContext, regions: &Regions, styles: StyleChain, - ) -> Vec>> { + ) -> Vec>> { let mut frames = self.child.layout(ctx, regions, styles); for Constrained { item: frame, .. } in &mut frames { let shape = Shape::filled(Geometry::Rect(frame.size), self.fill); - Rc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape)); + Arc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape)); } frames } @@ -399,11 +396,11 @@ impl Layout for StrokeNode { ctx: &mut LayoutContext, regions: &Regions, styles: StyleChain, - ) -> Vec>> { + ) -> Vec>> { let mut frames = self.child.layout(ctx, regions, styles); for Constrained { item: frame, .. } in &mut frames { let shape = Shape::stroked(Geometry::Rect(frame.size), self.stroke); - Rc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape)); + Arc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape)); } frames } -- cgit v1.2.3