From e9f885350ca219e3df23f5485f9ae4399579e6f1 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Thu, 9 Jan 2025 13:35:26 +0100 Subject: [PATCH] fix: Add custom SendAnyMap for torin to not depend on freya-native-core always --- crates/core/src/render/skia_measurer.rs | 2 +- crates/torin/benches/bench.rs | 1 - crates/torin/examples/demo.rs | 24 ++--------------- crates/torin/src/custom_measurer.rs | 25 ++++++++++++++--- crates/torin/src/dom_adapter.rs | 2 +- crates/torin/src/lib.rs | 2 ++ crates/torin/src/sendanymap.rs | 36 +++++++++++++++++++++++++ crates/torin/src/test_utils.rs | 28 +++---------------- 8 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 crates/torin/src/sendanymap.rs diff --git a/crates/core/src/render/skia_measurer.rs b/crates/core/src/render/skia_measurer.rs index 55eec4ce1..7dba7df4f 100644 --- a/crates/core/src/render/skia_measurer.rs +++ b/crates/core/src/render/skia_measurer.rs @@ -9,7 +9,6 @@ use freya_native_core::{ prelude::{ ElementNode, NodeType, - SendAnyMap, }, real_dom::NodeImmutable, tags::TagName, @@ -20,6 +19,7 @@ use torin::prelude::{ Area, LayoutMeasurer, Node, + SendAnyMap, Size2D, }; diff --git a/crates/torin/benches/bench.rs b/crates/torin/benches/bench.rs index 09385c8c2..399af25f7 100644 --- a/crates/torin/benches/bench.rs +++ b/crates/torin/benches/bench.rs @@ -9,7 +9,6 @@ use criterion::{ criterion_main, Criterion, }; -use freya_native_core::SendAnyMap; use torin::prelude::*; struct TestingMeasurer; diff --git a/crates/torin/examples/demo.rs b/crates/torin/examples/demo.rs index f900eea67..f8b334bf6 100644 --- a/crates/torin/examples/demo.rs +++ b/crates/torin/examples/demo.rs @@ -3,27 +3,8 @@ use std::{ sync::Arc, }; -use freya_native_core::prelude::SendAnyMap; use torin::prelude::*; -// Custom measurer, useful to measure certain elements such as text with other libraries -pub struct CustomMeasurer; - -impl LayoutMeasurer for CustomMeasurer { - fn measure( - &mut self, - _node_id: usize, - _node: &Node, - _size: &Size2D, - ) -> Option<(Size2D, Arc)> { - None - } - - fn should_measure_inner_children(&mut self, _node_id: usize) -> bool { - true - } -} - #[derive(Clone)] struct DemoNode { parent: Option, @@ -112,7 +93,6 @@ impl DOMAdapter for DemoDOM { fn main() { let mut layout = Torin::::new(); - let mut measurer = Some(CustomMeasurer); let mut demo_dom = DemoDOM::default(); @@ -158,7 +138,7 @@ fn main() { layout.measure( 0, // Root ID Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)), // Available Area - &mut measurer, + &mut None::, &mut demo_dom, ); @@ -185,7 +165,7 @@ fn main() { layout.measure( 0, // Fallback Root ID Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)), // Available Area - &mut measurer, + &mut None::, &mut demo_dom, ); diff --git a/crates/torin/src/custom_measurer.rs b/crates/torin/src/custom_measurer.rs index 3dcd1ecf5..445078f12 100644 --- a/crates/torin/src/custom_measurer.rs +++ b/crates/torin/src/custom_measurer.rs @@ -1,12 +1,13 @@ use std::sync::Arc; -use freya_native_core::prelude::SendAnyMap; - use crate::{ dom_adapter::NodeKey, geometry::Size2D, node::Node, - prelude::Area, + prelude::{ + Area, + SendAnyMap, + }, }; pub trait LayoutMeasurer { @@ -21,3 +22,21 @@ pub trait LayoutMeasurer { fn notify_layout_references(&self, _node_id: Key, _area: Area, _inner_sizes: Size2D) {} } + +// No-op measurer, use it when you don't need one. +pub struct NoopMeasurer; + +impl LayoutMeasurer for NoopMeasurer { + fn measure( + &mut self, + _node_id: usize, + _node: &Node, + _size: &Size2D, + ) -> Option<(Size2D, Arc)> { + None + } + + fn should_measure_inner_children(&mut self, _node_id: usize) -> bool { + false + } +} diff --git a/crates/torin/src/dom_adapter.rs b/crates/torin/src/dom_adapter.rs index 4cdff767b..9810f5312 100644 --- a/crates/torin/src/dom_adapter.rs +++ b/crates/torin/src/dom_adapter.rs @@ -1,7 +1,6 @@ use std::sync::Arc; pub use euclid::Rect; -use freya_native_core::SendAnyMap; use crate::{ geometry::Area, @@ -9,6 +8,7 @@ use crate::{ prelude::{ AreaModel, Gaps, + SendAnyMap, }, }; diff --git a/crates/torin/src/lib.rs b/crates/torin/src/lib.rs index 0b9f3f996..63633a6f3 100644 --- a/crates/torin/src/lib.rs +++ b/crates/torin/src/lib.rs @@ -4,6 +4,7 @@ pub mod geometry; mod measure; pub mod node; pub mod scaled; +pub mod sendanymap; pub mod torin; pub mod values; @@ -17,6 +18,7 @@ pub mod prelude { geometry::*, node::*, scaled::*, + sendanymap::*, torin::*, values::prelude::*, }; diff --git a/crates/torin/src/sendanymap.rs b/crates/torin/src/sendanymap.rs new file mode 100644 index 000000000..875805940 --- /dev/null +++ b/crates/torin/src/sendanymap.rs @@ -0,0 +1,36 @@ +use std::any::{ + Any, + TypeId, +}; + +use rustc_hash::FxHashMap; + +/// A map of types that can be sent between threads +#[derive(Debug)] +pub struct SendAnyMap { + map: FxHashMap>, +} + +impl Default for SendAnyMap { + fn default() -> Self { + Self::new() + } +} + +impl SendAnyMap { + pub fn new() -> Self { + Self { + map: FxHashMap::default(), + } + } + + pub fn get(&self) -> Option<&T> { + self.map + .get(&TypeId::of::()) + .and_then(|any| any.downcast_ref::()) + } + + pub fn insert(&mut self, value: T) { + self.map.insert(TypeId::of::(), Box::new(value)); + } +} diff --git a/crates/torin/src/test_utils.rs b/crates/torin/src/test_utils.rs index 33391f6e0..b2db3c58f 100644 --- a/crates/torin/src/test_utils.rs +++ b/crates/torin/src/test_utils.rs @@ -1,29 +1,7 @@ -use std::{ - collections::HashMap, - sync::Arc, -}; - -use freya_native_core::SendAnyMap; +use std::collections::HashMap; use crate::prelude::*; -pub struct TestingMeasurer; - -impl LayoutMeasurer for TestingMeasurer { - fn measure( - &mut self, - _node_id: usize, - _node: &Node, - _area_size: &Size2D, - ) -> Option<(Size2D, Arc)> { - None - } - - fn should_measure_inner_children(&mut self, _node_id: usize) -> bool { - true - } -} - #[derive(Default)] pub struct TestingDOM { mapper: HashMap, Vec, u16, Node)>, @@ -83,9 +61,9 @@ impl DOMAdapter for TestingDOM { } } -pub fn test_utils() -> (Torin, Option) { +pub fn test_utils() -> (Torin, Option) { let layout = Torin::::new(); - let measurer = Some(TestingMeasurer); + let measurer = None::; (layout, measurer) }