From 0821d8013d45a9e86b7815b357f85120d8455265 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 3 Jun 2023 12:28:58 +0100 Subject: [PATCH] Update docs + add constructor to MeasureFunc --- src/tree/measure_func.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/tree/measure_func.rs b/src/tree/measure_func.rs index 1c32fb710..6e0e4dd1d 100644 --- a/src/tree/measure_func.rs +++ b/src/tree/measure_func.rs @@ -5,9 +5,11 @@ use crate::style::AvailableSpace; #[cfg(any(feature = "std", feature = "alloc"))] use crate::util::sys::Box; -/// Represents a node that can be sized A function type that can be used in a [`MeasureFunc`] +/// Represents a node that can be sized / laid out, where the sizing/layout logic exists outside of Taffy. For example, +/// a text node where the text layout logic is implemented by the user. /// -/// This trait is automatically implemented for `FnMut` closures that define a function with the appropriate type signature. +/// This trait is implemented by the `MeasureFunc` and `SyncMeasureFunc` types which provide a bridge between this trait +/// and raw functions / `FnMut` closures. pub trait Measurable { /// A user-defined context which is passed to taffy when the `compute_layout` function is called, and which Taffy then passes /// into measure functions when it calls them @@ -23,25 +25,35 @@ pub trait Measurable { } /// A function that can be used to compute the intrinsic size of a node +#[allow(clippy::type_complexity)] pub enum MeasureFunc { /// Stores an unboxed function - #[allow(clippy::type_complexity)] Raw(fn(Size>, Size, &mut Context) -> Size), /// Stores a boxed function #[cfg(any(feature = "std", feature = "alloc"))] - Boxed(Box>), + Boxed(Box>, Size, &mut Context) -> Size>), } /// A function that can be used to compute the intrinsic size of a node +#[allow(clippy::type_complexity)] pub enum SyncMeasureFunc { /// Stores an unboxed function - #[allow(clippy::type_complexity)] Raw(fn(Size>, Size, &mut Context) -> Size), /// Stores a boxed function #[cfg(any(feature = "std", feature = "alloc"))] - Boxed(Box + Send + Sync>), + Boxed(Box>, Size, &mut Context) -> Size + Send + Sync>), +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl MeasureFunc { + /// Constructor for the Boxed variant that takes a plain closure and handles the actual boxing + pub fn boxed( + measure: impl FnMut(Size>, Size, &mut Context) -> Size + 'static, + ) -> Self { + Self::Boxed(Box::new(measure)) + } } impl Measurable for MeasureFunc { @@ -58,11 +70,21 @@ impl Measurable for MeasureFunc { match self { Self::Raw(measure) => measure(known_dimensions, available_space, context), #[cfg(any(feature = "std", feature = "alloc"))] - Self::Boxed(measurable) => measurable.measure(known_dimensions, available_space, context), + Self::Boxed(measure) => measure(known_dimensions, available_space, context), } } } +#[cfg(any(feature = "std", feature = "alloc"))] +impl SyncMeasureFunc { + /// Constructor for the Boxed variant that takes a plain closure and handles the actual boxing + pub fn boxed( + measure: impl FnMut(Size>, Size, &mut Context) -> Size + Send + Sync + 'static, + ) -> Self { + Self::Boxed(Box::new(measure)) + } +} + impl Measurable for SyncMeasureFunc { type Context = Context; @@ -77,7 +99,7 @@ impl Measurable for SyncMeasureFunc { match self { Self::Raw(measure) => measure(known_dimensions, available_space, context), #[cfg(any(feature = "std", feature = "alloc"))] - Self::Boxed(measurable) => measurable.measure(known_dimensions, available_space, context), + Self::Boxed(measure) => measure(known_dimensions, available_space, context), } } }