Cleanup imports (#75)
* Cleanup imports
* Fix another glob import
* feature gating import
* Testing gating box to behind sys
* Testing any feature?
a86dcfac473db4dd966f122198251847eaeab985 · by Andreas Weibye · (4 years ago) · parent 7b4a10160 · browse the tree at this commit
diff --git a/src/algo.rs b/src/algo.rs
index 2dab93beb..3ae8aa712 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -1,17 +1,16 @@
use core::f32;
+use crate::algo::Number::{Defined, Undefined};
use crate::forest::{Forest, NodeData};
+use crate::geometry::{Point, Rect, Size};
use crate::id::NodeId;
use crate::node::MeasureFunc;
+use crate::number::{MinMax, OrElse};
+use crate::prelude::Number;
use crate::result;
-use crate::style::*;
+use crate::style::{AlignContent, AlignSelf, Dimension, Display, FlexWrap, JustifyContent, PositionType};
use crate::sys;
-use crate::number::Number::*;
-use crate::number::*;
-
-use crate::geometry::{Point, Rect, Size};
-
#[derive(Debug, Clone)]
pub struct ComputeResult {
pub size: Size<f32>,
diff --git a/src/forest.rs b/src/forest.rs
index 40ad35f77..3164b95c1 100644
--- a/src/forest.rs
+++ b/src/forest.rs
@@ -7,7 +7,7 @@ use crate::node::MeasureFunc;
use crate::number::Number;
use crate::result::{Cache, Layout};
use crate::style::Style;
-use crate::sys;
+use crate::sys::{new_vec_with_capacity, ChildrenVec, ParentsVec, Vec};
pub(crate) struct NodeData {
pub(crate) style: Style,
@@ -43,36 +43,36 @@ impl NodeData {
}
pub(crate) struct Forest {
- pub(crate) nodes: sys::Vec<NodeData>,
- pub(crate) children: sys::Vec<sys::ChildrenVec<NodeId>>,
- pub(crate) parents: sys::Vec<sys::ParentsVec<NodeId>>,
+ pub(crate) nodes: Vec<NodeData>,
+ pub(crate) children: Vec<ChildrenVec<NodeId>>,
+ pub(crate) parents: Vec<ParentsVec<NodeId>>,
}
impl Forest {
pub fn with_capacity(capacity: usize) -> Self {
Self {
- nodes: sys::new_vec_with_capacity(capacity),
- children: sys::new_vec_with_capacity(capacity),
- parents: sys::new_vec_with_capacity(capacity),
+ nodes: new_vec_with_capacity(capacity),
+ children: new_vec_with_capacity(capacity),
+ parents: new_vec_with_capacity(capacity),
}
}
pub fn new_leaf(&mut self, style: Style, measure: MeasureFunc) -> NodeId {
let id = self.nodes.len();
self.nodes.push(NodeData::new_leaf(style, measure));
- self.children.push(sys::new_vec_with_capacity(0));
- self.parents.push(sys::new_vec_with_capacity(1));
+ self.children.push(new_vec_with_capacity(0));
+ self.parents.push(new_vec_with_capacity(1));
id
}
- pub fn new_node(&mut self, style: Style, children: sys::ChildrenVec<NodeId>) -> NodeId {
+ pub fn new_node(&mut self, style: Style, children: ChildrenVec<NodeId>) -> NodeId {
let id = self.nodes.len();
for child in &children {
self.parents[*child].push(id);
}
self.nodes.push(NodeData::new(style));
self.children.push(children);
- self.parents.push(sys::new_vec_with_capacity(1));
+ self.parents.push(new_vec_with_capacity(1));
id
}
@@ -170,7 +170,7 @@ impl Forest {
}
pub fn mark_dirty(&mut self, node: NodeId) {
- fn mark_dirty_impl(nodes: &mut sys::Vec<NodeData>, parents: &[sys::ParentsVec<NodeId>], node_id: NodeId) {
+ fn mark_dirty_impl(nodes: &mut Vec<NodeData>, parents: &[ParentsVec<NodeId>], node_id: NodeId) {
let node = &mut nodes[node_id];
node.main_size_layout_cache = None;
node.other_layout_cache = None;
diff --git a/src/geometry.rs b/src/geometry.rs
index 21c1206dc..a52d32e6d 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -1,7 +1,7 @@
use core::ops::Add;
use crate::number::Number;
-use crate::style;
+use crate::style::{Dimension, FlexDirection};
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -48,7 +48,7 @@ where
self.top + self.bottom
}
- pub(crate) fn main(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn main(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.horizontal()
} else {
@@ -56,7 +56,7 @@ where
}
}
- pub(crate) fn cross(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn cross(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.vertical()
} else {
@@ -69,7 +69,7 @@ impl<T> Rect<T>
where
T: Copy + Clone,
{
- pub(crate) fn main_start(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn main_start(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.start
} else {
@@ -77,7 +77,7 @@ where
}
}
- pub(crate) fn main_end(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn main_end(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.end
} else {
@@ -85,7 +85,7 @@ where
}
}
- pub(crate) fn cross_start(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn cross_start(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.top
} else {
@@ -93,7 +93,7 @@ where
}
}
- pub(crate) fn cross_end(&self, direction: style::FlexDirection) -> T {
+ pub(crate) fn cross_end(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.bottom
} else {
@@ -126,7 +126,7 @@ impl<T> Size<T> {
Size { width: f(self.width), height: f(self.height) }
}
- pub(crate) fn set_main(&mut self, direction: style::FlexDirection, value: T) {
+ pub(crate) fn set_main(&mut self, direction: FlexDirection, value: T) {
if direction.is_row() {
self.width = value
} else {
@@ -134,7 +134,7 @@ impl<T> Size<T> {
}
}
- pub(crate) fn set_cross(&mut self, direction: style::FlexDirection, value: T) {
+ pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: T) {
if direction.is_row() {
self.height = value
} else {
@@ -142,7 +142,7 @@ impl<T> Size<T> {
}
}
- pub(crate) fn main(self, direction: style::FlexDirection) -> T {
+ pub(crate) fn main(self, direction: FlexDirection) -> T {
if direction.is_row() {
self.width
} else {
@@ -150,7 +150,7 @@ impl<T> Size<T> {
}
}
- pub(crate) fn cross(self, direction: style::FlexDirection) -> T {
+ pub(crate) fn cross(self, direction: FlexDirection) -> T {
if direction.is_row() {
self.height
} else {
@@ -165,7 +165,7 @@ impl Size<f32> {
}
}
-impl Size<style::Dimension> {
+impl Size<Dimension> {
pub(crate) fn resolve(&self, parent: Size<Number>) -> Size<Number> {
Size { width: self.width.resolve(parent.width), height: self.height.resolve(parent.height) }
}
diff --git a/src/id.rs b/src/id.rs
index 154d1f87d..a29f12e98 100644
--- a/src/id.rs
+++ b/src/id.rs
@@ -1,7 +1,7 @@
//! Identifier for a Node
//!
//!
-use core::sync::atomic;
+use core::sync::atomic::{AtomicUsize, Ordering};
/// Internal node id.
pub(crate) type NodeId = usize;
@@ -11,16 +11,16 @@ pub(crate) type NodeId = usize;
pub(crate) struct Id(usize);
pub(crate) struct Allocator {
- new_id: atomic::AtomicUsize,
+ new_id: AtomicUsize,
}
impl Allocator {
pub const fn new() -> Self {
- Self { new_id: atomic::AtomicUsize::new(0) }
+ Self { new_id: AtomicUsize::new(0) }
}
pub fn allocate(&self) -> Id {
- Id(self.new_id.fetch_add(1, atomic::Ordering::Relaxed))
+ Id(self.new_id.fetch_add(1, Ordering::Relaxed))
}
pub fn free(&self, _ids: &[Id]) {}
diff --git a/src/lib.rs b/src/lib.rs
index 4dde31de5..b82b2ff1d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,9 +18,11 @@ pub mod style;
mod algo;
mod forest;
mod id;
-
mod sys;
+#[cfg(feature = "std")]
+use core::fmt::{Display, Formatter, Result};
+
pub use crate::node::Stretch;
#[derive(Debug)]
@@ -29,8 +31,8 @@ pub enum Error {
}
#[cfg(feature = "std")]
-impl std::fmt::Display for Error {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+impl Display for Error {
+ fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
Error::InvalidNode(ref node) => write!(f, "Invalid node {:?}", node),
}
diff --git a/src/node.rs b/src/node.rs
index 2a73c05c8..d5a2aadda 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -2,34 +2,36 @@ use core::ops::Drop;
use crate::forest::Forest;
use crate::geometry::Size;
-use crate::id::{self, NodeId};
+use crate::id::{Allocator, Id, NodeId};
use crate::number::Number;
use crate::result::Layout;
-use crate::style::*;
-use crate::sys;
+use crate::style::Style;
+#[cfg(any(feature = "std", feature = "alloc"))]
+use crate::sys::Box;
+use crate::sys::{new_map_with_capacity, ChildrenVec, Map, Vec};
use crate::Error;
pub enum MeasureFunc {
Raw(fn(Size<Number>) -> Size<f32>),
#[cfg(any(feature = "std", feature = "alloc"))]
- Boxed(sys::Box<dyn Fn(Size<Number>) -> Size<f32>>),
+ Boxed(Box<dyn Fn(Size<Number>) -> Size<f32>>),
}
/// Global stretch instance id allocator.
-static INSTANCE_ALLOCATOR: id::Allocator = id::Allocator::new();
+static INSTANCE_ALLOCATOR: Allocator = Allocator::new();
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(not(any(feature = "std", feature = "alloc")), derive(hash32_derive::Hash32))]
pub struct Node {
- instance: id::Id,
- local: id::Id,
+ instance: Id,
+ local: Id,
}
pub struct Stretch {
- id: id::Id,
- nodes: id::Allocator,
- nodes_to_ids: crate::sys::Map<Node, NodeId>,
- ids_to_nodes: crate::sys::Map<NodeId, Node>,
+ id: Id,
+ nodes: Allocator,
+ nodes_to_ids: Map<Node, NodeId>,
+ ids_to_nodes: Map<NodeId, Node>,
forest: Forest,
}
@@ -47,9 +49,9 @@ impl Stretch {
pub fn with_capacity(capacity: usize) -> Self {
Self {
id: INSTANCE_ALLOCATOR.allocate(),
- nodes: id::Allocator::new(),
- nodes_to_ids: crate::sys::new_map_with_capacity(capacity),
- ids_to_nodes: crate::sys::new_map_with_capacity(capacity),
+ nodes: Allocator::new(),
+ nodes_to_ids: new_map_with_capacity(capacity),
+ ids_to_nodes: new_map_with_capacity(capacity),
forest: Forest::with_capacity(capacity),
}
}
@@ -82,7 +84,7 @@ impl Stretch {
pub fn new_node(&mut self, style: Style, children: &[Node]) -> Result<Node, Error> {
let node = self.allocate_node();
let children =
- children.iter().map(|child| self.find_node(*child)).collect::<Result<sys::ChildrenVec<_>, Error>>()?;
+ children.iter().map(|child| self.find_node(*child)).collect::<Result<ChildrenVec<_>, Error>>()?;
let id = self.forest.new_node(style, children);
self.add_node(node, id);
Ok(node)
@@ -131,8 +133,7 @@ impl Stretch {
pub fn set_children(&mut self, node: Node, children: &[Node]) -> Result<(), Error> {
let node_id = self.find_node(node)?;
- let children_id =
- children.iter().map(|child| self.find_node(*child)).collect::<Result<sys::ChildrenVec<_>, _>>()?;
+ let children_id = children.iter().map(|child| self.find_node(*child)).collect::<Result<ChildrenVec<_>, _>>()?;
// Remove node as parent from all its current children.
for child in &self.forest.children[node_id] {
@@ -179,7 +180,7 @@ impl Stretch {
Ok(self.ids_to_nodes[&old_child])
}
- pub fn children(&self, node: Node) -> Result<sys::Vec<Node>, Error> {
+ pub fn children(&self, node: Node) -> Result<Vec<Node>, Error> {
let id = self.find_node(node)?;
Ok(self.forest.children[id].iter().map(|child| self.ids_to_nodes[child]).collect())
}
diff --git a/src/number.rs b/src/number.rs
index ab6712f92..d54358be9 100644
--- a/src/number.rs
+++ b/src/number.rs
@@ -1,4 +1,4 @@
-use core::ops;
+use core::ops::{Add, Div, Mul, Sub};
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -114,7 +114,7 @@ impl From<f32> for Number {
}
}
-impl ops::Add<f32> for Number {
+impl Add<f32> for Number {
type Output = Number;
fn add(self, rhs: f32) -> Self {
@@ -125,7 +125,7 @@ impl ops::Add<f32> for Number {
}
}
-impl ops::Add<Number> for Number {
+impl Add<Number> for Number {
type Output = Number;
fn add(self, rhs: Self) -> Self {
@@ -137,7 +137,7 @@ impl ops::Add<Number> for Number {
}
}
-impl ops::Sub<f32> for Number {
+impl Sub<f32> for Number {
type Output = Number;
fn sub(self, rhs: f32) -> Self {
@@ -148,7 +148,7 @@ impl ops::Sub<f32> for Number {
}
}
-impl ops::Sub<Number> for Number {
+impl Sub<Number> for Number {
type Output = Number;
fn sub(self, rhs: Self) -> Self {
@@ -160,7 +160,7 @@ impl ops::Sub<Number> for Number {
}
}
-impl ops::Mul<f32> for Number {
+impl Mul<f32> for Number {
type Output = Number;
fn mul(self, rhs: f32) -> Self {
@@ -171,7 +171,7 @@ impl ops::Mul<f32> for Number {
}
}
-impl ops::Mul<Number> for Number {
+impl Mul<Number> for Number {
type Output = Number;
fn mul(self, rhs: Self) -> Self {
@@ -183,7 +183,7 @@ impl ops::Mul<Number> for Number {
}
}
-impl ops::Div<f32> for Number {
+impl Div<f32> for Number {
type Output = Number;
fn div(self, rhs: f32) -> Self {
@@ -194,7 +194,7 @@ impl ops::Div<f32> for Number {
}
}
-impl ops::Div<Number> for Number {
+impl Div<Number> for Number {
type Output = Number;
fn div(self, rhs: Self) -> Self {