@@ -15,37 +15,13 @@ use {
15
15
crate :: sys:: GridTrackVec ,
16
16
} ;
17
17
18
- /// `NodeIdRef` can be passed to a [`StyleBuilder`] so that caller can later
19
- /// retrieve the [`NodeId`] of a built tree node.
20
- #[ derive( Debug , Clone , Default ) ]
21
- pub struct NodeIdRef ( Option < NodeId > ) ;
22
-
23
- impl NodeIdRef {
24
- /// Create an empty [`NodeIdRef`].
25
- pub fn new ( ) -> Self {
26
- Self ( None )
27
- }
28
-
29
- /// Set the [`NodeId`].
30
- fn set ( & mut self , node_id : NodeId ) {
31
- self . 0 = Some ( node_id)
32
- }
33
-
34
- /// Get a copy of the inner [`NodeId`], if any is present.
35
- pub fn get ( & self ) -> Option < NodeId > {
36
- self . 0
37
- }
38
- }
39
-
40
18
/// Given a builder name and associated fields, generate the following :
41
19
/// * A struct of the given name, with the following fields
42
20
/// * `children`: a vec of child builder
43
- /// * `node_id_ref`: a field holding a [`Option<NodeIdRef >`], wich allow for retrieving the [`NodeId`] of the built node
21
+ /// * `node_id_ref`: a field holding an [`Option<&mut Option<NodeId> >`], wich allow for retrieving the [`NodeId`] of the built node
44
22
/// * `style`: the [`Style`] that will be modified when calling the setters in the `impl` block
45
- /// * A [`Option<_>`] field for each provided field
46
23
/// * An `impl` block containing the following :
47
24
/// * A method named after the provided field, used to set said field
48
- /// * A `build_style` method, used to generate a [`Style`](super::Style) based on data stored in the builder
49
25
macro_rules! gen_builder {
50
26
( $builder: ident, $( ( $field: ident: $type: ty $( , cfg: $( $cfg: tt) +) ?) ) ,* $( , ) ?) => {
51
27
/// Use [`StyleBuilder`] to construct a tree of nested style nodes.
@@ -54,21 +30,21 @@ macro_rules! gen_builder {
54
30
/// ```rust
55
31
/// # use taffy::prelude::*;
56
32
/// let mut builder_tree: TaffyTree<()> = TaffyTree::new();
57
- /// let mut header_node_handle = NodeIdRef::new() ;
58
- /// let mut body_node_handle = NodeIdRef::new() ;
33
+ /// let mut header_node_id = None ;
34
+ /// let mut body_node_id = None ;
59
35
///
60
36
/// let builder_root_node = StyleBuilder::new()
61
37
/// .flex_direction(FlexDirection::Column)
62
38
/// .size(Size { width: length(800.0), height: length(600.0) })
63
39
/// .child(
64
- /// StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(&mut header_node_handle ),
40
+ /// StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(&mut header_node_id ),
65
41
/// )
66
42
/// .child(
67
43
/// StyleBuilder::new()
68
44
/// .width(length(800.0))
69
45
/// .height(auto())
70
46
/// .flex_grow(1.0)
71
- /// .node_id_ref(&mut body_node_handle ),
47
+ /// .node_id_ref(&mut body_node_id ),
72
48
/// )
73
49
/// .build(&mut builder_tree)
74
50
/// .unwrap();
@@ -78,7 +54,7 @@ macro_rules! gen_builder {
78
54
#[ derive( Debug , Default ) ]
79
55
pub struct $builder<' a> {
80
56
children: Vec <& ' a mut StyleBuilder <' a>>,
81
- node_id_ref: Option <& ' a mut NodeIdRef >,
57
+ node_id_ref: Option <& ' a mut Option < NodeId > >,
82
58
style: Style ,
83
59
}
84
60
@@ -175,7 +151,7 @@ impl<'a> StyleBuilder<'a> {
175
151
let node_id = tree. new_leaf ( self . style . clone ( ) ) ?;
176
152
177
153
if let Some ( node_id_ref) = self . node_id_ref . as_mut ( ) {
178
- node_id_ref. set ( node_id) ;
154
+ * * node_id_ref = Some ( node_id)
179
155
}
180
156
181
157
let children_node_ids =
@@ -194,7 +170,7 @@ impl<'a> StyleBuilder<'a> {
194
170
/// # use taffy::prelude::*;
195
171
///
196
172
/// let mut tree: TaffyTree<()> = TaffyTree::new();
197
- /// let mut child_node_id_ref = NodeIdRef::new() ;
173
+ /// let mut child_node_id_ref = None ;
198
174
///
199
175
/// let root_node_id = StyleBuilder::new()
200
176
/// .display(Display::Block)
@@ -210,14 +186,14 @@ impl<'a> StyleBuilder<'a> {
210
186
///
211
187
/// assert!(
212
188
/// matches!(
213
- /// child_node_id_ref.get() ,
189
+ /// child_node_id_ref,
214
190
/// Some(_)
215
191
/// )
216
192
/// );
217
193
///
218
- /// tree.layout(child_node_id_ref.get(). unwrap()).unwrap();
194
+ /// tree.layout(child_node_id_ref.unwrap()).unwrap();
219
195
/// ```
220
- pub fn node_id_ref ( & ' a mut self , node_id_ref : & ' a mut NodeIdRef ) -> & ' a mut StyleBuilder < ' a > {
196
+ pub fn node_id_ref ( & ' a mut self , node_id_ref : & ' a mut Option < NodeId > ) -> & ' a mut StyleBuilder < ' a > {
221
197
self . node_id_ref = Some ( node_id_ref) ;
222
198
self
223
199
}
@@ -242,7 +218,6 @@ mod test {
242
218
243
219
use crate :: {
244
220
prelude:: { auto, length, TaffyMaxContent } ,
245
- style:: builder:: NodeIdRef ,
246
221
Size , TaffyTree ,
247
222
} ;
248
223
@@ -283,8 +258,8 @@ mod test {
283
258
tree. compute_layout ( root_node, Size :: MAX_CONTENT ) . unwrap ( ) ;
284
259
285
260
let mut builder_tree: TaffyTree < ( ) > = TaffyTree :: new ( ) ;
286
- let mut header_node_handle = NodeIdRef :: new ( ) ;
287
- let mut body_node_handle = NodeIdRef :: new ( ) ;
261
+ let mut header_node_handle = None ;
262
+ let mut body_node_handle = None ;
288
263
289
264
let builder_root_node = StyleBuilder :: new ( )
290
265
. flex_direction ( FlexDirection :: Column )
@@ -312,19 +287,19 @@ mod test {
312
287
) ;
313
288
assert_eq ! (
314
289
tree. layout( header_node) . unwrap( ) . size. width,
315
- builder_tree. layout( header_node_handle. get ( ) . unwrap( ) ) . unwrap( ) . size. width
290
+ builder_tree. layout( header_node_handle. unwrap( ) ) . unwrap( ) . size. width
316
291
) ;
317
292
assert_eq ! (
318
293
tree. layout( header_node) . unwrap( ) . size. height,
319
- builder_tree. layout( header_node_handle. get ( ) . unwrap( ) ) . unwrap( ) . size. height
294
+ builder_tree. layout( header_node_handle. unwrap( ) ) . unwrap( ) . size. height
320
295
) ;
321
296
assert_eq ! (
322
297
tree. layout( body_node) . unwrap( ) . size. width,
323
- builder_tree. layout( body_node_handle. get ( ) . unwrap( ) ) . unwrap( ) . size. width
298
+ builder_tree. layout( body_node_handle. unwrap( ) ) . unwrap( ) . size. width
324
299
) ;
325
300
assert_eq ! (
326
301
tree. layout( body_node) . unwrap( ) . size. height,
327
- builder_tree. layout( body_node_handle. get ( ) . unwrap( ) ) . unwrap( ) . size. height
302
+ builder_tree. layout( body_node_handle. unwrap( ) ) . unwrap( ) . size. height
328
303
) ;
329
304
}
330
305
0 commit comments