diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-10-26 17:11:08 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-10-26 17:14:11 +0200 |
| commit | 5c534fb42881b508175dd25129f62e604bdae403 (patch) | |
| tree | fab522708f55d3ee132cb7a0789de701b795d847 /src/layout | |
| parent | fb0cd3df6e1e1077c6f19c319726c9aa9678325b (diff) | |
Simplify node construction
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/grid.rs | 9 | ||||
| -rw-r--r-- | src/layout/image.rs | 9 | ||||
| -rw-r--r-- | src/layout/mod.rs | 113 | ||||
| -rw-r--r-- | src/layout/pad.rs | 9 | ||||
| -rw-r--r-- | src/layout/par.rs | 11 | ||||
| -rw-r--r-- | src/layout/shape.rs | 9 | ||||
| -rw-r--r-- | src/layout/stack.rs | 11 |
7 files changed, 60 insertions, 111 deletions
diff --git a/src/layout/grid.rs b/src/layout/grid.rs index 7220d7c2..57986b48 100644 --- a/src/layout/grid.rs +++ b/src/layout/grid.rs @@ -1,8 +1,7 @@ use super::*; /// A node that arranges its children in a grid. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct GridNode { /// Defines sizing for content rows and columns. pub tracks: Spec<Vec<TrackSizing>>, @@ -40,12 +39,6 @@ impl BlockLevel for GridNode { } } -impl From<GridNode> for BlockNode { - fn from(node: GridNode) -> Self { - Self::new(node) - } -} - /// Performs grid layout. struct GridLayouter<'a> { /// The original expand state of the target region. diff --git a/src/layout/image.rs b/src/layout/image.rs index 59b4c6ef..b410895b 100644 --- a/src/layout/image.rs +++ b/src/layout/image.rs @@ -2,8 +2,7 @@ use super::*; use crate::image::ImageId; /// An image node. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct ImageNode { /// The id of the image file. pub id: ImageId, @@ -43,9 +42,3 @@ impl InlineLevel for ImageNode { frame } } - -impl From<ImageNode> for InlineNode { - fn from(node: ImageNode) -> Self { - Self::new(node) - } -} diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 651daa21..ffbf2668 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -29,6 +29,7 @@ pub use stack::*; pub use text::*; use std::fmt::{self, Debug, Formatter}; +use std::hash::{Hash, Hasher}; use std::rc::Rc; use crate::font::FontStore; @@ -37,13 +38,6 @@ use crate::image::ImageStore; use crate::util::OptionExt; use crate::Context; -#[cfg(feature = "layout-cache")] -use { - fxhash::FxHasher64, - std::any::Any, - std::hash::{Hash, Hasher}, -}; - /// Layout a page-level node into a collection of frames. pub fn layout<T>(ctx: &mut Context, node: &T) -> Vec<Rc<Frame>> where @@ -129,9 +123,21 @@ pub trait BlockLevel: Debug { ctx: &mut LayoutContext, regions: &Regions, ) -> Vec<Constrained<Rc<Frame>>>; + + /// Convert to a packed block-level node. + fn pack(self) -> BlockNode + where + Self: Sized + Hash + 'static, + { + BlockNode { + #[cfg(feature = "layout-cache")] + hash: hash_node(&self), + node: Rc::new(self), + } + } } -/// A dynamic [block-level](BlockLevel) layouting node. +/// A packed [block-level](BlockLevel) layouting node with precomputed hash. #[derive(Clone)] pub struct BlockNode { node: Rc<dyn BlockLevel>, @@ -139,29 +145,6 @@ pub struct BlockNode { hash: u64, } -impl BlockNode { - /// Create a new dynamic node from any block-level node. - #[cfg(not(feature = "layout-cache"))] - pub fn new<T>(node: T) -> Self - where - T: BlockLevel + 'static, - { - Self { node: Rc::new(node) } - } - - /// Create a new dynamic node from any block-level node. - #[cfg(feature = "layout-cache")] - pub fn new<T>(node: T) -> Self - where - T: BlockLevel + Hash + 'static, - { - Self { - hash: hash_node(&node), - node: Rc::new(node), - } - } -} - impl BlockLevel for BlockNode { fn layout( &self, @@ -194,12 +177,21 @@ impl BlockLevel for BlockNode { frames }) } + + fn pack(self) -> BlockNode + where + Self: Sized + Hash + 'static, + { + self + } } -#[cfg(feature = "layout-cache")] impl Hash for BlockNode { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_u64(self.hash); + fn hash<H: Hasher>(&self, _state: &mut H) { + #[cfg(feature = "layout-cache")] + _state.write_u64(self.hash); + #[cfg(not(feature = "layout-cache"))] + unimplemented!() } } @@ -216,9 +208,21 @@ impl Debug for BlockNode { pub trait InlineLevel: Debug { /// Layout the node into a frame. fn layout(&self, ctx: &mut LayoutContext, space: Length, base: Size) -> Frame; + + /// Convert to a packed inline-level node. + fn pack(self) -> InlineNode + where + Self: Sized + Hash + 'static, + { + InlineNode { + #[cfg(feature = "layout-cache")] + hash: hash_node(&self), + node: Rc::new(self), + } + } } -/// A dynamic [inline-level](InlineLevel) layouting node. +/// A packed [inline-level](InlineLevel) layouting node with precomputed hash. #[derive(Clone)] pub struct InlineNode { node: Rc<dyn InlineLevel>, @@ -226,39 +230,25 @@ pub struct InlineNode { hash: u64, } -impl InlineNode { - /// Create a new dynamic node from any inline-level node. - #[cfg(not(feature = "layout-cache"))] - pub fn new<T>(node: T) -> Self - where - T: InlineLevel + 'static, - { - Self { node: Rc::new(node) } +impl InlineLevel for InlineNode { + fn layout(&self, ctx: &mut LayoutContext, space: Length, base: Size) -> Frame { + self.node.layout(ctx, space, base) } - /// Create a new dynamic node from any inline-level node. - #[cfg(feature = "layout-cache")] - pub fn new<T>(node: T) -> Self + fn pack(self) -> InlineNode where - T: InlineLevel + Hash + 'static, + Self: Sized + Hash + 'static, { - Self { - hash: hash_node(&node), - node: Rc::new(node), - } - } -} - -impl InlineLevel for InlineNode { - fn layout(&self, ctx: &mut LayoutContext, space: Length, base: Size) -> Frame { - self.node.layout(ctx, space, base) + self } } -#[cfg(feature = "layout-cache")] impl Hash for InlineNode { - fn hash<H: Hasher>(&self, state: &mut H) { - state.write_u64(self.hash); + fn hash<H: Hasher>(&self, _state: &mut H) { + #[cfg(feature = "layout-cache")] + _state.write_u64(self.hash); + #[cfg(not(feature = "layout-cache"))] + unimplemented!() } } @@ -271,7 +261,8 @@ impl Debug for InlineNode { /// Hash a node alongside its type id. #[cfg(feature = "layout-cache")] fn hash_node(node: &(impl Hash + 'static)) -> u64 { - let mut state = FxHasher64::default(); + use std::any::Any; + let mut state = fxhash::FxHasher64::default(); node.type_id().hash(&mut state); node.hash(&mut state); state.finish() diff --git a/src/layout/pad.rs b/src/layout/pad.rs index 20fcc161..55c11e45 100644 --- a/src/layout/pad.rs +++ b/src/layout/pad.rs @@ -1,8 +1,7 @@ use super::*; /// A node that adds padding to its child. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct PadNode { /// The amount of padding. pub padding: Sides<Linear>, @@ -74,9 +73,3 @@ impl BlockLevel for PadNode { frames } } - -impl From<PadNode> for BlockNode { - fn from(node: PadNode) -> Self { - Self::new(node) - } -} diff --git a/src/layout/par.rs b/src/layout/par.rs index 64265b63..a645eb07 100644 --- a/src/layout/par.rs +++ b/src/layout/par.rs @@ -12,8 +12,7 @@ use crate::util::{EcoString, RangeExt, SliceExt}; type Range = std::ops::Range<usize>; /// A node that arranges its children into a paragraph. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct ParNode { /// The inline direction of this paragraph. pub dir: Dir, @@ -24,7 +23,7 @@ pub struct ParNode { } /// A child of a paragraph node. -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Hash)] pub enum ParChild { /// Spacing between other nodes. Spacing(Spacing), @@ -93,12 +92,6 @@ impl ParNode { } } -impl From<ParNode> for BlockNode { - fn from(node: ParNode) -> Self { - Self::new(node) - } -} - impl Debug for ParChild { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { diff --git a/src/layout/shape.rs b/src/layout/shape.rs index 2e66a0dd..ed70dd95 100644 --- a/src/layout/shape.rs +++ b/src/layout/shape.rs @@ -4,8 +4,7 @@ use super::*; use crate::util::RcExt; /// Places its child into a sizable and fillable shape. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct ShapeNode { /// Which shape to place the child into. pub shape: ShapeKind, @@ -105,9 +104,3 @@ impl InlineLevel for ShapeNode { frame } } - -impl From<ShapeNode> for InlineNode { - fn from(node: ShapeNode) -> Self { - Self::new(node) - } -} diff --git a/src/layout/stack.rs b/src/layout/stack.rs index dbd9ddb0..0fb3e3eb 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -3,8 +3,7 @@ use std::fmt::{self, Debug, Formatter}; use super::*; /// A node that stacks its children. -#[derive(Debug)] -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Debug, Hash)] pub struct StackNode { /// The stacking direction. pub dir: Dir, @@ -13,7 +12,7 @@ pub struct StackNode { } /// A child of a stack node. -#[cfg_attr(feature = "layout-cache", derive(Hash))] +#[derive(Hash)] pub enum StackChild { /// Spacing between other nodes. Spacing(Spacing), @@ -31,12 +30,6 @@ impl BlockLevel for StackNode { } } -impl From<StackNode> for BlockNode { - fn from(node: StackNode) -> Self { - Self::new(node) - } -} - impl Debug for StackChild { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { |
