Skip to content

Commit

Permalink
Update docs + add constructor to MeasureFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jun 3, 2023
1 parent 5227bc7 commit f1e12b5
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/tree/measure_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,25 +25,34 @@ pub trait Measurable {
}

/// A function that can be used to compute the intrinsic size of a node
#[allow(clippy::type_complexity)]
pub enum MeasureFunc<Context = ()> {
/// Stores an unboxed function
#[allow(clippy::type_complexity)]
Raw(fn(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32>),

/// Stores a boxed function
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(Box<dyn Measurable<Context = Context>>),
Boxed(Box<dyn FnMut(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32>>),
}

/// A function that can be used to compute the intrinsic size of a node
#[allow(clippy::type_complexity)]
pub enum SyncMeasureFunc<Context = ()> {
/// Stores an unboxed function
#[allow(clippy::type_complexity)]
Raw(fn(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32>),

/// Stores a boxed function
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(Box<dyn Measurable<Context = Context> + Send + Sync>),
Boxed(Box<dyn FnMut(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32> + Send + Sync>),
}

impl<Context> MeasureFunc<Context> {
/// Constructor for the Boxed variant that takes a plain closure and handles the actual boxing
pub fn boxed(
measure: impl FnMut(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32> + 'static,
) -> Self {
Self::Boxed(Box::new(measure))
}
}

impl<Context> Measurable for MeasureFunc<Context> {
Expand All @@ -58,11 +69,20 @@ impl<Context> Measurable for MeasureFunc<Context> {
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),
}
}
}

impl<Context> SyncMeasureFunc<Context> {
/// Constructor for the Boxed variant that takes a plain closure and handles the actual boxing
pub fn boxed(
measure: impl FnMut(Size<Option<f32>>, Size<AvailableSpace>, &mut Context) -> Size<f32> + Send + Sync + 'static,
) -> Self {
Self::Boxed(Box::new(measure))
}
}

impl<Context> Measurable for SyncMeasureFunc<Context> {
type Context = Context;

Expand All @@ -77,7 +97,7 @@ impl<Context> Measurable for SyncMeasureFunc<Context> {
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),
}
}
}
Expand Down

0 comments on commit f1e12b5

Please sign in to comment.