@@ -8,7 +8,7 @@ use crate::util::sys::Box;
8
8
/// A function type that can be used in a [`MeasureFunc`]
9
9
///
10
10
/// 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 {
12
12
/// A user-defined context which is passed to taffy when the `compute_layout` function is called, and which Taffy then passes
13
13
/// into measure functions when it calls them
14
14
type Context ;
@@ -55,13 +55,53 @@ impl<Context> Measurable for MeasureFunc<Context> {
55
55
}
56
56
}
57
57
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
+
58
91
#[ cfg( test) ]
59
92
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
+ }
61
101
62
102
#[ test]
63
- fn measure_func_is_send_and_sync ( ) {
103
+ fn taffy_with_sync_measure_func_is_send_and_sync ( ) {
64
104
fn is_send_and_sync < T : Send + Sync > ( ) { }
65
- is_send_and_sync :: < MeasureFunc > ( ) ;
105
+ is_send_and_sync :: < Taffy < SyncMeasureFunc > > ( ) ;
66
106
}
67
107
}
0 commit comments