Skip to content

Commit 2e037d9

Browse files
committed
Remove Send+Sync bound from Measurable and create separate SyncMeasureFunc type
1 parent 9865cd1 commit 2e037d9

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![deny(unsafe_code)]
44
#![warn(missing_docs)]
55
#![warn(clippy::missing_docs_in_private_items)]
6-
#![forbid(unsafe_code)]
76

87
// We always need std for the tests
98
// See <https://github.com/la10736/rstest/issues/149#issuecomment-1156402989>

src/tree/measure_func.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::util::sys::Box;
88
/// A function type that can be used in a [`MeasureFunc`]
99
///
1010
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
11-
pub trait Measurable: Send + Sync {
11+
pub trait Measurable {
1212
/// A user-defined context which is passed to taffy when the `compute_layout` function is called, and which Taffy then passes
1313
/// into measure functions when it calls them
1414
type Context;
@@ -55,13 +55,53 @@ impl<Context> Measurable for MeasureFunc<Context> {
5555
}
5656
}
5757

58+
/// A function that can be used to compute the intrinsic size of a node
59+
pub enum SyncMeasureFunc<Context = ()> {
60+
/// Stores an unboxed function with no context parameter
61+
Raw(fn(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32>),
62+
63+
/// Stores an unboxed function with a context parameter
64+
RawWithContext(fn(Size<Option<f32>>, Size<AvailableSpace>, context: &mut Context) -> Size<f32>),
65+
66+
/// Stores a boxed function
67+
#[cfg(any(feature = "std", feature = "alloc"))]
68+
Boxed(Box<dyn Measurable<Context = Context> + Send + Sync>),
69+
}
70+
71+
impl<Context> Measurable for SyncMeasureFunc<Context> {
72+
type Context = Context;
73+
74+
/// Call the measure function to measure to the node
75+
#[inline(always)]
76+
fn measure(
77+
&self,
78+
known_dimensions: Size<Option<f32>>,
79+
available_space: Size<AvailableSpace>,
80+
context: &mut Context,
81+
) -> Size<f32> {
82+
match self {
83+
Self::Raw(measure) => measure(known_dimensions, available_space),
84+
Self::RawWithContext(measure) => measure(known_dimensions, available_space, context),
85+
#[cfg(any(feature = "std", feature = "alloc"))]
86+
Self::Boxed(measurable) => measurable.measure(known_dimensions, available_space, context),
87+
}
88+
}
89+
}
90+
5891
#[cfg(test)]
5992
mod test {
60-
use super::MeasureFunc;
93+
use super::SyncMeasureFunc;
94+
use crate::tree::Taffy;
95+
96+
#[test]
97+
fn sync_measure_func_is_send_and_sync() {
98+
fn is_send_and_sync<T: Send + Sync>() {}
99+
is_send_and_sync::<SyncMeasureFunc>();
100+
}
61101

62102
#[test]
63-
fn measure_func_is_send_and_sync() {
103+
fn taffy_with_sync_measure_func_is_send_and_sync() {
64104
fn is_send_and_sync<T: Send + Sync>() {}
65-
is_send_and_sync::<MeasureFunc>();
105+
is_send_and_sync::<Taffy<SyncMeasureFunc>>();
66106
}
67107
}

src/tree/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::style::{AvailableSpace, Style};
77
mod cache;
88
pub use cache::{Cache, CacheEntry};
99
mod measure_func;
10-
pub use measure_func::{Measurable, MeasureFunc};
10+
pub use measure_func::{Measurable, MeasureFunc, SyncMeasureFunc};
1111
mod node;
1212
#[cfg(feature = "taffy_tree")]
1313
pub(self) use node::NodeData;

src/tree/taffy_tree/tree.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
pub(crate) nodes: SlotMap<DefaultKey, NodeData>,
3434

3535
/// Functions/closures that compute the intrinsic size of leaf nodes
36-
pub(crate) measure_funcs: SparseSecondaryMap<DefaultKey, MeasureFunc<Measure::Context>>,
36+
pub(crate) measure_funcs: SparseSecondaryMap<DefaultKey, Measure>,
3737

3838
/// The children of each node
3939
///
@@ -168,11 +168,7 @@ impl<Measure: Measurable> Taffy<Measure> {
168168
/// Creates and adds a new unattached leaf node to the tree, and returns the node of the new node
169169
///
170170
/// Creates and adds a new leaf node with a supplied [`MeasureFunc`]
171-
pub fn new_leaf_with_measure(
172-
&mut self,
173-
layout: Style,
174-
measure: MeasureFunc<Measure::Context>,
175-
) -> TaffyResult<NodeId> {
171+
pub fn new_leaf_with_measure(&mut self, layout: Style, measure: Measure) -> TaffyResult<NodeId> {
176172
let mut data = NodeData::new(layout);
177173
data.needs_measure = true;
178174

@@ -225,7 +221,7 @@ impl<Measure: Measurable> Taffy<Measure> {
225221
}
226222

227223
/// Sets the [`MeasureFunc`] of the associated node
228-
pub fn set_measure(&mut self, node: NodeId, measure: Option<MeasureFunc<Measure::Context>>) -> TaffyResult<()> {
224+
pub fn set_measure(&mut self, node: NodeId, measure: Option<Measure>) -> TaffyResult<()> {
229225
let key = node.into();
230226
if let Some(measure) = measure {
231227
self.nodes[key].needs_measure = true;

0 commit comments

Comments
 (0)