Skip to content

Commit 9439948

Browse files
committed
NodeIdRef no longer relies on interior mutability (#275)
1 parent d50c6ba commit 9439948

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ detailed_layout_info = []
6464
## Use strict provenance APIs for pointer manipulation. Using this feature requires Rust 1.84 or higher.
6565
strict_provenance = []
6666
## Expose a builder pattern interface to construct layout tree
67-
builder = ["alloc"]
67+
builder = []
6868

6969
#! ### Taffy Tree
7070

src/style/builder.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use super::{
33
AlignContent, AlignItems, AlignSelf, BoxSizing, Dimension, Display, JustifyContent, LengthPercentage,
44
LengthPercentageAuto, Overflow, Position, Style,
55
};
6-
use crate::sys::Rc;
76
use crate::{util::sys::Vec, Line, NodeId, Point, Rect, Size};
8-
use core::cell::Cell;
97

108
#[cfg(feature = "flexbox")]
119
use super::{FlexDirection, FlexWrap};
@@ -20,22 +18,22 @@ use {
2018
/// `NodeIdRef` can be passed to a [`StyleBuilder`] so that caller can later
2119
/// retrieve the [`NodeId`] of a built tree node.
2220
#[derive(Debug, Clone, Default)]
23-
pub struct NodeIdRef(Rc<Cell<Option<NodeId>>>);
21+
pub struct NodeIdRef(Option<NodeId>);
2422

2523
impl NodeIdRef {
2624
/// Create an empty [`NodeIdRef`].
2725
pub fn new() -> Self {
28-
Self(Rc::new(Cell::new(None)))
26+
Self(None)
2927
}
3028

3129
/// Set the [`NodeId`].
32-
fn set(&self, node_id: NodeId) {
33-
self.0.set(Some(node_id));
30+
fn set(&mut self, node_id: NodeId) {
31+
self.0 = Some(node_id)
3432
}
3533

3634
/// Get a copy of the inner [`NodeId`], if any is present.
3735
pub fn get(&self) -> Option<NodeId> {
38-
self.0.get()
36+
self.0
3937
}
4038
}
4139

@@ -56,21 +54,21 @@ macro_rules! gen_builder {
5654
/// ```rust
5755
/// # use taffy::prelude::*;
5856
/// let mut builder_tree: TaffyTree<()> = TaffyTree::new();
59-
/// let header_node_handle = NodeIdRef::new();
60-
/// let body_node_handle = NodeIdRef::new();
57+
/// let mut header_node_handle = NodeIdRef::new();
58+
/// let mut body_node_handle = NodeIdRef::new();
6159
///
6260
/// let builder_root_node = StyleBuilder::new()
6361
/// .flex_direction(FlexDirection::Column)
6462
/// .size(Size { width: length(800.0), height: length(600.0) })
6563
/// .child(
66-
/// StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(header_node_handle.clone()),
64+
/// StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(&mut header_node_handle),
6765
/// )
6866
/// .child(
6967
/// StyleBuilder::new()
7068
/// .width(length(800.0))
7169
/// .height(auto())
7270
/// .flex_grow(1.0)
73-
/// .node_id_ref(body_node_handle.clone()),
71+
/// .node_id_ref(&mut body_node_handle),
7472
/// )
7573
/// .build(&mut builder_tree)
7674
/// .unwrap();
@@ -79,8 +77,8 @@ macro_rules! gen_builder {
7977
/// ```
8078
#[derive(Debug, Default)]
8179
pub struct $builder<'a> {
82-
children: Vec<&'a StyleBuilder<'a>>,
83-
node_id_ref: Option<NodeIdRef>,
80+
children: Vec<&'a mut StyleBuilder<'a>>,
81+
node_id_ref: Option<&'a mut NodeIdRef>,
8482
style: Style,
8583
}
8684

@@ -162,7 +160,7 @@ impl<'a> StyleBuilder<'a> {
162160
/// Add a child [`StyleBuilder`] to this builder. Calling this method does not result
163161
/// in the child [`StyleBuilder`] being built until the [`StyleBuilder::build`] method
164162
/// is invoke on this builder.
165-
pub fn child(&'a mut self, style_builder: &'a StyleBuilder) -> &'a mut StyleBuilder<'a> {
163+
pub fn child(&'a mut self, style_builder: &'a mut StyleBuilder<'a>) -> &'a mut StyleBuilder<'a> {
166164
self.children.push(style_builder);
167165
self
168166
}
@@ -173,14 +171,15 @@ impl<'a> StyleBuilder<'a> {
173171
/// Return a [`TaffyResult<NodeId>`] for the root node. Child [`NodeId`] can be
174172
/// retrieved once [`build`](StyleBuilder::build) is invoked via setting a [`NodeIdRef`]
175173
/// in each of the desired child [`StyleBuilder`]
176-
pub fn build(&self, tree: &mut TaffyTree) -> TaffyResult<NodeId> {
174+
pub fn build(&mut self, tree: &mut TaffyTree) -> TaffyResult<NodeId> {
177175
let node_id = tree.new_leaf(self.style.clone())?;
178176

179-
if let Some(node_id_ref) = self.node_id_ref.as_ref() {
177+
if let Some(node_id_ref) = self.node_id_ref.as_mut() {
180178
node_id_ref.set(node_id);
181179
}
182180

183-
let children_node_ids = self.children.iter().map(|child| child.build(tree)).collect::<Result<Vec<_>, _>>()?;
181+
let children_node_ids =
182+
self.children.iter_mut().map(|child| child.build(tree)).collect::<Result<Vec<_>, _>>()?;
184183

185184
tree.set_children(node_id, &children_node_ids)?;
186185

@@ -195,14 +194,14 @@ impl<'a> StyleBuilder<'a> {
195194
/// # use taffy::prelude::*;
196195
///
197196
/// let mut tree: TaffyTree<()> = TaffyTree::new();
198-
/// let child_node_id_ref = NodeIdRef::new();
197+
/// let mut child_node_id_ref = NodeIdRef::new();
199198
///
200199
/// let root_node_id = StyleBuilder::new()
201200
/// .display(Display::Block)
202201
/// .child(
203202
/// StyleBuilder::new()
204203
/// .display(Display::Block)
205-
/// .node_id_ref(child_node_id_ref.clone())
204+
/// .node_id_ref(&mut child_node_id_ref)
206205
/// )
207206
/// .build(&mut tree)
208207
/// .unwrap();
@@ -218,7 +217,7 @@ impl<'a> StyleBuilder<'a> {
218217
///
219218
/// tree.layout(child_node_id_ref.get().unwrap()).unwrap();
220219
/// ```
221-
pub fn node_id_ref(&'a mut self, node_id_ref: NodeIdRef) -> &'a mut StyleBuilder<'a> {
220+
pub fn node_id_ref(&'a mut self, node_id_ref: &'a mut NodeIdRef) -> &'a mut StyleBuilder<'a> {
222221
self.node_id_ref = Some(node_id_ref);
223222
self
224223
}
@@ -284,21 +283,19 @@ mod test {
284283
tree.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
285284

286285
let mut builder_tree: TaffyTree<()> = TaffyTree::new();
287-
let header_node_handle = NodeIdRef::new();
288-
let body_node_handle = NodeIdRef::new();
286+
let mut header_node_handle = NodeIdRef::new();
287+
let mut body_node_handle = NodeIdRef::new();
289288

290289
let builder_root_node = StyleBuilder::new()
291290
.flex_direction(FlexDirection::Column)
292291
.size(Size { width: length(800.0), height: length(600.0) })
293-
.child(
294-
StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(header_node_handle.clone()),
295-
)
292+
.child(StyleBuilder::new().width(length(800.0)).height(length(100.0)).node_id_ref(&mut header_node_handle))
296293
.child(
297294
StyleBuilder::new()
298295
.width(length(800.0))
299296
.height(auto())
300297
.flex_grow(1.0)
301-
.node_id_ref(body_node_handle.clone()),
298+
.node_id_ref(&mut body_node_handle),
302299
)
303300
.build(&mut builder_tree)
304301
.unwrap();

src/util/sys.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ mod std {
2222
#[cfg(feature = "grid")]
2323
/// A vector of grid tracks
2424
pub(crate) type GridTrackVec<A> = std::vec::Vec<A>;
25-
/// Rc
26-
pub(crate) type Rc<A> = std::rc::Rc<A>;
2725

2826
/// Creates a new vector with the capacity for the specified number of items before it must be resized
2927
#[must_use]
@@ -85,8 +83,6 @@ mod alloc {
8583
#[cfg(feature = "grid")]
8684
/// A vector of grid tracks
8785
pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
88-
/// Rc
89-
pub(crate) type Rc<A> = alloc::rc::Rc<A>;
9086

9187
/// Creates a new vector with the capacity for the specified number of items before it must be resized
9288
#[must_use]

0 commit comments

Comments
 (0)