Skip to content

Commit 2cb95be

Browse files
committed
node_id_ref now accepts &mut Option<NodeId> (#275)
1 parent fec1447 commit 2cb95be

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ pub use crate::style_helpers::{
3030
pub use crate::TaffyTree;
3131

3232
#[cfg(feature = "builder")]
33-
pub use crate::style::{NodeIdRef, StyleBuilder};
33+
pub use crate::style::StyleBuilder;

src/style/builder.rs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,13 @@ use {
1515
crate::sys::GridTrackVec,
1616
};
1717

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-
4018
/// Given a builder name and associated fields, generate the following :
4119
/// * A struct of the given name, with the following fields
4220
/// * `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
4422
/// * `style`: the [`Style`] that will be modified when calling the setters in the `impl` block
45-
/// * A [`Option<_>`] field for each provided field
4623
/// * An `impl` block containing the following :
4724
/// * 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
4925
macro_rules! gen_builder {
5026
($builder:ident, $(($field:ident: $type:ty $(, cfg: $($cfg:tt)+)?)),* $(,)?) => {
5127
/// Use [`StyleBuilder`] to construct a tree of nested style nodes.
@@ -54,21 +30,21 @@ macro_rules! gen_builder {
5430
/// ```rust
5531
/// # use taffy::prelude::*;
5632
/// 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;
5935
///
6036
/// let builder_root_node = StyleBuilder::new()
6137
/// .flex_direction(FlexDirection::Column)
6238
/// .size(Size { width: length(800.0), height: length(600.0) })
6339
/// .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),
6541
/// )
6642
/// .child(
6743
/// StyleBuilder::new()
6844
/// .width(length(800.0))
6945
/// .height(auto())
7046
/// .flex_grow(1.0)
71-
/// .node_id_ref(&mut body_node_handle),
47+
/// .node_id_ref(&mut body_node_id),
7248
/// )
7349
/// .build(&mut builder_tree)
7450
/// .unwrap();
@@ -78,7 +54,7 @@ macro_rules! gen_builder {
7854
#[derive(Debug, Default)]
7955
pub struct $builder<'a> {
8056
children: Vec<&'a mut StyleBuilder<'a>>,
81-
node_id_ref: Option<&'a mut NodeIdRef>,
57+
node_id_ref: Option<&'a mut Option<NodeId>>,
8258
style: Style,
8359
}
8460

@@ -175,7 +151,7 @@ impl<'a> StyleBuilder<'a> {
175151
let node_id = tree.new_leaf(self.style.clone())?;
176152

177153
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)
179155
}
180156

181157
let children_node_ids =
@@ -194,7 +170,7 @@ impl<'a> StyleBuilder<'a> {
194170
/// # use taffy::prelude::*;
195171
///
196172
/// let mut tree: TaffyTree<()> = TaffyTree::new();
197-
/// let mut child_node_id_ref = NodeIdRef::new();
173+
/// let mut child_node_id_ref = None;
198174
///
199175
/// let root_node_id = StyleBuilder::new()
200176
/// .display(Display::Block)
@@ -210,14 +186,14 @@ impl<'a> StyleBuilder<'a> {
210186
///
211187
/// assert!(
212188
/// matches!(
213-
/// child_node_id_ref.get(),
189+
/// child_node_id_ref,
214190
/// Some(_)
215191
/// )
216192
/// );
217193
///
218-
/// tree.layout(child_node_id_ref.get().unwrap()).unwrap();
194+
/// tree.layout(child_node_id_ref.unwrap()).unwrap();
219195
/// ```
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> {
221197
self.node_id_ref = Some(node_id_ref);
222198
self
223199
}
@@ -242,7 +218,6 @@ mod test {
242218

243219
use crate::{
244220
prelude::{auto, length, TaffyMaxContent},
245-
style::builder::NodeIdRef,
246221
Size, TaffyTree,
247222
};
248223

@@ -283,8 +258,8 @@ mod test {
283258
tree.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
284259

285260
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;
288263

289264
let builder_root_node = StyleBuilder::new()
290265
.flex_direction(FlexDirection::Column)
@@ -312,19 +287,19 @@ mod test {
312287
);
313288
assert_eq!(
314289
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
316291
);
317292
assert_eq!(
318293
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
320295
);
321296
assert_eq!(
322297
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
324299
);
325300
assert_eq!(
326301
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
328303
);
329304
}
330305

src/style/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use self::dimension::{Dimension, LengthPercentage, LengthPercentageAuto};
2121
#[cfg(feature = "block_layout")]
2222
pub use self::block::{BlockContainerStyle, BlockItemStyle, TextAlign};
2323
#[cfg(feature = "builder")]
24-
pub use self::builder::{NodeIdRef, StyleBuilder};
24+
pub use self::builder::StyleBuilder;
2525
#[cfg(feature = "flexbox")]
2626
pub use self::flex::{FlexDirection, FlexWrap, FlexboxContainerStyle, FlexboxItemStyle};
2727
#[cfg(feature = "grid")]

0 commit comments

Comments
 (0)