Skip to content

Commit

Permalink
Add Context to Measurable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed May 21, 2023
1 parent 5443996 commit 862559b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/tree/measure_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,39 @@ use crate::util::sys::Box;
///
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
pub trait Measurable: Send + Sync {
/// 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
type Context;

/// Measure node
fn measure(&self, known_dimensions: Size<Option<f32>>, available_space: Size<AvailableSpace>) -> Size<f32>;
fn measure(
&self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
context: Self::Context,
) -> Size<f32>;
}

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

/// Stores a boxed function
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(Box<dyn Measurable>),
Boxed(Box<dyn Measurable<Context = Context>>),
}

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

/// Call the measure function to measure to the node
#[inline(always)]
fn measure(&self, known_dimensions: Size<Option<f32>>, available_space: Size<AvailableSpace>) -> Size<f32> {
fn measure(&self, known_dimensions: Size<Option<f32>>, available_space: Size<AvailableSpace>, context: Context) -> Size<f32> {
match self {
Self::Raw(measure) => measure(known_dimensions, available_space),
Self::Raw(measure) => measure(known_dimensions, available_space, context),
#[cfg(any(feature = "std", feature = "alloc"))]
Self::Boxed(measurable) => measurable.measure(known_dimensions, available_space),
Self::Boxed(measurable) => measurable.measure(known_dimensions, available_space, context),
}
}
}
Expand Down

0 comments on commit 862559b

Please sign in to comment.