diff --git a/src/algo.rs b/src/algo.rs
index 16b74a56d..7d7240314 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -5,7 +5,7 @@ use libm::F32Ext;
use core::f32;
-use crate::node::{NodeId, Storage, Stretch};
+use crate::node::{Node, Storage, Stretch};
use crate::result;
use crate::result::Result;
use crate::style::*;
@@ -21,7 +21,7 @@ pub struct ComputeResult {
}
struct FlexItem {
- node: NodeId,
+ node: Node,
size: Size<Number>,
min_size: Size<Number>,
@@ -58,7 +58,7 @@ struct FlexLine {
}
impl Stretch {
- pub(crate) fn compute(&mut self, root: NodeId, size: Size<Number>) -> Result<()> {
+ pub(crate) fn compute(&mut self, root: Node, size: Size<Number>) -> Result<()> {
let style = self.style[&root];
let has_root_min_max = style.min_size.width.is_defined()
|| style.min_size.height.is_defined()
@@ -113,8 +113,8 @@ impl Stretch {
fn round_layout(
layouts: &mut Storage<result::Layout>,
- children: &Storage<Vec<NodeId>>,
- root: NodeId,
+ children: &Storage<Vec<Node>>,
+ root: Node,
abs_x: f32,
abs_y: f32,
) {
@@ -133,7 +133,7 @@ impl Stretch {
fn compute_internal(
&mut self,
- node: NodeId,
+ node: Node,
node_size: Size<Number>,
parent_size: Size<Number>,
perform_layout: bool,
@@ -730,7 +730,7 @@ impl Stretch {
// TODO - probably should move this somewhere else as it doesn't make a ton of sense here but we need it below
// TODO - This is expensive and should only be done if we really require a baseline. aka, make it lazy
- fn calc_baseline(db: &Stretch, node: NodeId, layout: &result::Layout) -> f32 {
+ fn calc_baseline(db: &Stretch, node: Node, layout: &result::Layout) -> f32 {
if db.children[&node].is_empty() {
layout.size.height
} else {
@@ -1345,12 +1345,7 @@ impl Stretch {
}
}
- fn hidden_layout(
- layout: &mut Storage<result::Layout>,
- children: &Storage<Vec<NodeId>>,
- node: NodeId,
- order: u32,
- ) {
+ fn hidden_layout(layout: &mut Storage<result::Layout>, children: &Storage<Vec<Node>>, node: Node, order: u32) {
*layout.get_mut(&node).unwrap() =
result::Layout { order, size: Size { width: 0.0, height: 0.0 }, location: Point { x: 0.0, y: 0.0 } };
diff --git a/src/node.rs b/src/node.rs
index d5684b581..3e85f761f 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -10,12 +10,14 @@ use crate::style::*;
type MeasureFunc = Box<Fn(Size<Number>) -> Result<Size<f32>>>;
-pub type NodeId = usize;
-pub(crate) type Storage<T> = HashMap<NodeId, T>;
+#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
+pub struct Node(usize);
+
+pub(crate) type Storage<T> = HashMap<Node, T>;
struct Allocator {
- new_id: NodeId,
- free_ids: Vec<NodeId>,
+ new_id: usize,
+ free_ids: Vec<usize>,
}
impl Allocator {
@@ -23,13 +25,13 @@ impl Allocator {
Allocator { new_id: 0, free_ids: Vec::new() }
}
- pub fn allocate(&mut self) -> NodeId {
+ pub fn allocate(&mut self) -> Node {
match self.free_ids.pop() {
- Some(id) => id,
+ Some(id) => Node(id),
None => {
let id = self.new_id;
self.new_id += 1;
- id
+ Node(id)
}
}
}
@@ -38,8 +40,8 @@ impl Allocator {
pub struct Stretch {
nodes: Allocator,
pub(crate) style: Storage<Style>,
- pub(crate) parents: Storage<Vec<NodeId>>,
- pub(crate) children: Storage<Vec<NodeId>>,
+ pub(crate) parents: Storage<Vec<Node>>,
+ pub(crate) children: Storage<Vec<Node>>,
pub(crate) measure: Storage<Option<MeasureFunc>>,
pub(crate) layout: Storage<Layout>,
pub(crate) layout_cache: Storage<Option<Cache>>,
@@ -60,7 +62,7 @@ impl Stretch {
}
}
- pub fn new_leaf(&mut self, style: Style, measure: MeasureFunc) -> NodeId {
+ pub fn new_leaf(&mut self, style: Style, measure: MeasureFunc) -> Node {
// Node(Rc::new(RefCell::new(InternalNode {
// style,
// parents: Vec::with_capacity(1),
@@ -72,7 +74,7 @@ impl Stretch {
unimplemented!()
}
- pub fn new_node(&mut self, style: Style, children: Vec<NodeId>) -> NodeId {
+ pub fn new_node(&mut self, style: Style, children: Vec<Node>) -> Node {
let node = self.nodes.allocate();
for child in &children {
@@ -90,14 +92,14 @@ impl Stretch {
node
}
- pub fn set_measure(&mut self, node: NodeId, measure: Option<MeasureFunc>) {
+ pub fn set_measure(&mut self, node: Node, measure: Option<MeasureFunc>) {
// self.0.borrow_mut().measure = measure;
// self.mark_dirty();
unimplemented!()
}
- pub fn add_child(&mut self, node: NodeId, child: NodeId) {
+ pub fn add_child(&mut self, node: Node, child: Node) {
// child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
// self.0.borrow_mut().children.push(Rc::clone(&child.0));
// self.mark_dirty();
@@ -105,7 +107,7 @@ impl Stretch {
unimplemented!()
}
- pub fn set_children(&mut self, node: NodeId, children: Vec<NodeId>) {
+ pub fn set_children(&mut self, node: Node, children: Vec<Node>) {
// for child in &self.0.borrow().children {
// let position =
// child.borrow().parents.iter().position(|x| Rc::ptr_eq(&x.upgrade().unwrap(), &self.0)).unwrap();
@@ -124,7 +126,7 @@ impl Stretch {
unimplemented!()
}
- pub fn remove_child(&mut self, node: NodeId, child: NodeId) -> NodeId {
+ pub fn remove_child(&mut self, node: Node, child: Node) -> Node {
// self.remove_child_at_index({
// let parent = self.0.borrow();
// parent.children.iter().position(|x| Rc::ptr_eq(&x, &child.0)).unwrap()
@@ -133,7 +135,7 @@ impl Stretch {
unimplemented!()
}
- pub fn remove_child_at_index(&mut self, node: NodeId, index: usize) -> NodeId {
+ pub fn remove_child_at_index(&mut self, node: Node, index: usize) -> Node {
// let child = {
// let mut parent = self.0.borrow_mut();
// let child = parent.children.remove(index);
@@ -149,7 +151,7 @@ impl Stretch {
unimplemented!()
}
- pub fn replace_child_at_index(&mut self, node: NodeId, index: usize, child: NodeId) -> NodeId {
+ pub fn replace_child_at_index(&mut self, node: Node, index: usize, child: Node) -> Node {
// child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
// let old_child = std::mem::replace(&mut self.0.borrow_mut().children[index], Rc::clone(&child.0));
@@ -164,38 +166,38 @@ impl Stretch {
unimplemented!()
}
- pub fn children(&self, node: NodeId) -> Vec<NodeId> {
+ pub fn children(&self, node: Node) -> Vec<Node> {
// let node = self.0.borrow_mut();
// node.children.iter().map(|child| Node(Rc::clone(child))).collect()
unimplemented!()
}
- pub fn child_count(&self, node: NodeId) -> usize {
+ pub fn child_count(&self, node: Node) -> usize {
// let node = self.0.borrow_mut();
// node.children.len()
unimplemented!()
}
- pub fn set_style(&mut self, node: NodeId, style: Style) {
+ pub fn set_style(&mut self, node: Node, style: Style) {
// self.0.borrow_mut().style = style;
// self.mark_dirty();
unimplemented!()
}
- pub fn style(&self, node: NodeId) -> Style {
+ pub fn style(&self, node: Node) -> Style {
// self.0.borrow().style
unimplemented!()
}
- pub fn layout(&self, node: NodeId) -> &Layout {
+ pub fn layout(&self, node: Node) -> &Layout {
&self.layout[&node]
}
- pub fn mark_dirty(&mut self, node: NodeId) {
+ pub fn mark_dirty(&mut self, node: Node) {
// let mut node = self.0.borrow_mut();
// node.layout_cache = None;
// node.is_dirty = true;
@@ -209,13 +211,13 @@ impl Stretch {
unimplemented!()
}
- pub fn dirty(&self, node: NodeId) -> bool {
+ pub fn dirty(&self, node: Node) -> bool {
// self.0.borrow().is_dirty
unimplemented!()
}
- pub fn compute_layout(&mut self, node: NodeId, size: Size<Number>) -> Result<()> {
+ pub fn compute_layout(&mut self, node: Node, size: Size<Number>) -> Result<()> {
self.compute(node, size)
}
}