From 1ef6ba7f41b27e963e97e1774eb89d5911fee1d1 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 21 Nov 2023 14:54:47 +0100 Subject: [PATCH] Inline block into styles file --- crates/typst/src/model/block.rs | 82 --------------------------- crates/typst/src/model/mod.rs | 2 - crates/typst/src/model/styles.rs | 96 +++++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 93 deletions(-) delete mode 100644 crates/typst/src/model/block.rs diff --git a/crates/typst/src/model/block.rs b/crates/typst/src/model/block.rs deleted file mode 100644 index c1370c205..000000000 --- a/crates/typst/src/model/block.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::any::Any; -use std::fmt::{self, Debug, Formatter}; -use std::hash::{Hash, Hasher}; - -/// A block storage for storing stylechain values either on the stack (if they -/// fit) or on the heap. -/// -/// We're using a `Box` since values will either be contained in an `Arc` and -/// therefore already on the heap or they will be small enough that we can just -/// clone them. -pub struct Block(Box); - -impl Block { - /// Creates a new block. - pub fn new(value: T) -> Self { - Self(Box::new(value)) - } - - /// Downcasts the block to the specified type. - pub fn downcast(&self) -> Option<&T> { - self.0.as_any().downcast_ref() - } - - /// Downcasts mutably the block to the specified type. - pub fn downcast_mut(&mut self) -> Option<&mut T> { - self.0.as_any_mut().downcast_mut() - } -} - -impl Debug for Block { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - -impl Hash for Block { - fn hash(&self, state: &mut H) { - self.0.dyn_hash(state); - } -} - -impl Clone for Block { - fn clone(&self) -> Self { - self.0.dyn_clone() - } -} - -/// A value that can be stored in a block. -/// -/// Auto derived for all types that implement [`Any`], [`Clone`], [`Hash`], -/// [`Debug`], [`Send`] and [`Sync`]. -pub trait Blockable: Debug + Send + Sync + 'static { - /// Equivalent to `downcast_ref` for the block. - fn as_any(&self) -> &dyn Any; - - /// Equivalent to `downcast_mut` for the block. - fn as_any_mut(&mut self) -> &mut dyn Any; - - /// Equivalent to [`Hash`] for the block. - fn dyn_hash(&self, state: &mut dyn Hasher); - - /// Equivalent to [`Clone`] for the block. - fn dyn_clone(&self) -> Block; -} - -impl Blockable for T { - fn as_any(&self) -> &dyn Any { - self - } - - fn as_any_mut(&mut self) -> &mut dyn Any { - self - } - - fn dyn_hash(&self, mut state: &mut dyn Hasher) { - self.hash(&mut state); - } - - fn dyn_clone(&self) -> Block { - Block(Box::new(self.clone())) - } -} diff --git a/crates/typst/src/model/mod.rs b/crates/typst/src/model/mod.rs index af5c846f4..ec4c8bef0 100644 --- a/crates/typst/src/model/mod.rs +++ b/crates/typst/src/model/mod.rs @@ -1,6 +1,5 @@ //! The document model. -mod block; mod content; mod element; mod introspect; @@ -14,7 +13,6 @@ use ecow::EcoVec; #[doc(inline)] pub use typst_macros::elem; -pub use self::block::{Block, Blockable}; pub use self::content::{fat, Content, MetaElem, PlainText}; pub use self::element::{ Construct, Element, ElementFields, LocalName, NativeElement, NativeElementData, Set, diff --git a/crates/typst/src/model/styles.rs b/crates/typst/src/model/styles.rs index 5566b8394..a943472ec 100644 --- a/crates/typst/src/model/styles.rs +++ b/crates/typst/src/model/styles.rs @@ -1,5 +1,7 @@ +use std::any::Any; use std::borrow::Cow; use std::fmt::{self, Debug, Formatter}; +use std::hash::{Hash, Hasher}; use std::iter; use std::mem; use std::ptr; @@ -11,7 +13,7 @@ use smallvec::SmallVec; use crate::diag::{SourceResult, Trace, Tracepoint}; use crate::eval::{cast, ty, Args, Func, Repr, Value, Vm}; -use crate::model::{Block, Blockable, Content, Element, NativeElement, Selector, Vt}; +use crate::model::{Content, Element, NativeElement, Selector, Vt}; use crate::syntax::Span; /// A list of style properties. @@ -165,7 +167,10 @@ pub struct Property { impl Property { /// Create a new property from a key-value pair. - pub fn new(elem: Element, id: u8, value: T) -> Self { + pub fn new(elem: Element, id: u8, value: T) -> Self + where + T: Debug + Clone + Hash + Send + Sync + 'static, + { Self { elem, id, value: Block::new(value), span: None } } @@ -193,6 +198,79 @@ impl Debug for Property { } } +/// A block storage for storing style values. +/// +/// We're using a `Box` since values will either be contained in an `Arc` and +/// therefore already on the heap or they will be small enough that we can just +/// clone them. +struct Block(Box); + +impl Block { + /// Creates a new block. + fn new(value: T) -> Self { + Self(Box::new(value)) + } + + /// Downcasts the block to the specified type. + fn downcast(&self) -> Option<&T> { + self.0.as_any().downcast_ref() + } +} + +impl Debug for Block { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +impl Hash for Block { + fn hash(&self, state: &mut H) { + self.0.dyn_hash(state); + } +} + +impl Clone for Block { + fn clone(&self) -> Self { + self.0.dyn_clone() + } +} + +/// A value that can be stored in a block. +/// +/// Auto derived for all types that implement [`Any`], [`Clone`], [`Hash`], +/// [`Debug`], [`Send`] and [`Sync`]. +trait Blockable: Debug + Send + Sync + 'static { + /// Equivalent to `downcast_ref` for the block. + fn as_any(&self) -> &dyn Any; + + /// Equivalent to `downcast_mut` for the block. + fn as_any_mut(&mut self) -> &mut dyn Any; + + /// Equivalent to [`Hash`] for the block. + fn dyn_hash(&self, state: &mut dyn Hasher); + + /// Equivalent to [`Clone`] for the block. + fn dyn_clone(&self) -> Block; +} + +impl Blockable for T { + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } + + fn dyn_hash(&self, mut state: &mut dyn Hasher) { + self.hash(&mut state); + } + + fn dyn_clone(&self) -> Block { + Block(Box::new(self.clone())) + } +} + /// A show rule recipe. #[derive(Clone, PartialEq, Hash)] pub struct Recipe { @@ -329,7 +407,7 @@ impl<'a> StyleChain<'a> { /// Cast the first value for the given property in the chain, /// returning a borrowed value if possible. - pub fn get_borrowed( + pub fn get_borrowed( self, func: Element, id: u8, @@ -342,7 +420,7 @@ impl<'a> StyleChain<'a> { } /// Cast the first value for the given property in the chain. - pub fn get( + pub fn get( self, func: Element, id: u8, @@ -353,7 +431,7 @@ impl<'a> StyleChain<'a> { } /// Cast the first value for the given property in the chain. - pub fn get_resolve( + pub fn get_resolve( self, func: Element, id: u8, @@ -364,7 +442,7 @@ impl<'a> StyleChain<'a> { } /// Cast the first value for the given property in the chain. - pub fn get_fold( + pub fn get_fold( self, func: Element, id: u8, @@ -392,7 +470,7 @@ impl<'a> StyleChain<'a> { default: impl Fn() -> ::Output, ) -> ::Output where - T: Blockable + Clone + Resolve, + T: Resolve + Clone + 'static, T::Output: Fold, { fn next( @@ -401,7 +479,7 @@ impl<'a> StyleChain<'a> { default: &impl Fn() -> ::Output, ) -> ::Output where - T: Blockable + Resolve, + T: Resolve + 'static, T::Output: Fold, { values @@ -419,7 +497,7 @@ impl<'a> StyleChain<'a> { } /// Iterate over all values for the given property in the chain. - pub fn properties( + pub fn properties( self, func: Element, id: u8,