Skip to content

Commit

Permalink
Make Measurable::measure take &mut self
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Aug 23, 2023
1 parent 9ba6432 commit 831698d
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/tree/measure_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use crate::style::AvailableSpace;
#[cfg(any(feature = "std", feature = "alloc"))]
use crate::util::sys::Box;

/// A function type that can be used in a [`MeasureFunc`]
/// Represents a node that can be sized A function type that can be used in a [`MeasureFunc`]
///
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
/// This trait is automatically implemented for `FnMut` closures that define a function with the appropriate type signature.
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
type Context;

/// Measure node
fn measure(
&self,
&mut self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
context: &mut Self::Context,
Expand All @@ -26,20 +26,31 @@ pub trait Measurable {
pub enum MeasureFunc<Context = ()> {
/// Stores an unboxed function
#[allow(clippy::type_complexity)]
Raw(fn(Size<Option<f32>>, Size<AvailableSpace>, context: &mut Context) -> Size<f32>),
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>>),
}

/// A function that can be used to compute the intrinsic size of a node
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>),
}

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

/// Call the measure function to measure the node
#[inline(always)]
fn measure(
&self,
&mut self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
context: &mut Context,
Expand All @@ -52,24 +63,13 @@ impl<Context> Measurable for MeasureFunc<Context> {
}
}

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

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

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

/// Call the measure function to measure the node
#[inline(always)]
fn measure(
&self,
&mut self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
context: &mut Context,
Expand Down

0 comments on commit 831698d

Please sign in to comment.