Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MeasureFunc Send and Sync #157

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- fixed rounding of fractional values to follow latest Chrome - values are now rounded the same regardless of their position
- fixed computing free space when using both `flex-grow` and a minimum size
- padding is now only subtracted when determining the available space if the node size is unspecified, following [section 9.2.2 of the flexbox spec](https://www.w3.org/TR/css-flexbox-1/#line-sizing)
- `MeasureFunc` (and hence `NodeData` and hence `Forest` and hence the public `Taffy` type) are now `Send` and `Sync`, enabling their use in async and parallel applications

### 0.2.0 Removed

Expand Down
22 changes: 20 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ use crate::sys::Box;
use crate::sys::{new_map_with_capacity, ChildrenVec, Map, Vec};
use core::sync::atomic::{AtomicUsize, Ordering};

/// A function that can be applied to a `Size<Option<f32>>` to obtain a `Size<f32>`
/// 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.
pub trait Measurable: Send + Sync + Fn(Size<Option<f32>>) -> Size<f32> {}

impl<F: Send + Sync + Fn(Size<Option<f32>>) -> Size<f32>> Measurable for F {}

/// A function that can be used to compute the intrinsic size of a node
pub enum MeasureFunc {
/// Stores an unboxed function
Raw(fn(Size<Option<f32>>) -> Size<f32>),
/// Stores a boxed function
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(Box<dyn Fn(Size<Option<f32>>) -> Size<f32>>),
Boxed(Box<dyn Measurable>),
}

/// Global taffy instance id allocator.
Expand Down Expand Up @@ -320,3 +327,14 @@ impl Allocator {
Id(self.last_id.fetch_add(1, Ordering::Relaxed))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn measure_func_is_send_and_sync() {
fn is_send_and_sync<T: Sync>() {}
is_send_and_sync::<MeasureFunc>();
Comment on lines +336 to +338

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is missing the T: Send bound.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye. I'll fix this ASAP.

}
}