Skip to content

Commit

Permalink
Switch to a no_std library
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-adult committed Oct 25, 2023
1 parent 3ad8d4a commit 0ccd4f3
Show file tree
Hide file tree
Showing 28 changed files with 134 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ documentation = "https://docs.rs/tree_iterators_rs/latest/tree_iterators_rs/"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
streaming-iterator = "0.1.9"
streaming-iterator = { version = "0.1.9", features = ["alloc"] }
serde = { version = "1.0.189", optional = true }
serde_derive = { version = "1.0.189", optional = true }

Expand Down
5 changes: 4 additions & 1 deletion src/bfs_iterators/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::VecDeque;
use alloc::{
collections::VecDeque,
vec::Vec
};
use streaming_iterator::StreamingIterator;

use crate::{
Expand Down
6 changes: 3 additions & 3 deletions src/bfs_iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod borrow;
macro_rules! bfs_next {
($get_value_and_children: ident) => {
fn next(&mut self) -> Option<Self::Item> {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(root) => {
let (value, children) = root.$get_value_and_children();
match children {
Expand Down Expand Up @@ -96,7 +96,7 @@ macro_rules! bfs_advance_iterator {
.push_back(Some(popped));
}
Some(mut value) => {
self.item_stack.push(std::mem::take(&mut value.value).unwrap());
self.item_stack.push(core::mem::take(&mut value.value).unwrap());
let has_children = !value.children.is_none();
self.traversal_stack.push(value);
stack_len += 1;
Expand Down Expand Up @@ -249,5 +249,5 @@ pub(crate) use get_mut;
#[derive(Debug, Default, Clone)]
struct TreeNodeVecDeque<T> {
value: Option<T>,
children: Option<std::collections::VecDeque<Option<Self>>>,
children: Option<alloc::collections::VecDeque<Option<Self>>>,
}
5 changes: 4 additions & 1 deletion src/bfs_iterators/mut_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::VecDeque;
use alloc::{
collections::VecDeque,
vec::Vec
};
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
5 changes: 4 additions & 1 deletion src/bfs_iterators/owned.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::VecDeque;
use alloc::{
collections::VecDeque,
vec::Vec
};
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
1 change: 1 addition & 0 deletions src/dfs_inorder_iterators/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::StreamingIterator;

use crate::{
Expand Down
1 change: 1 addition & 0 deletions src/dfs_inorder_iterators/mut_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
1 change: 1 addition & 0 deletions src/dfs_inorder_iterators/owned.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use crate::{
prelude::OwnedBinaryTreeNode,
leaves_iterators::depth_first::owned::OwnedBinaryLeavesIterator,
Expand Down
1 change: 1 addition & 0 deletions src/dfs_postorder_iterators/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::StreamingIterator;
use crate::{
prelude::{
Expand Down
4 changes: 2 additions & 2 deletions src/dfs_postorder_iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! dfs_postorder_next {
($get_value_and_children: ident) => {
fn next(&mut self) -> Option<Self::Item> {
loop {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(next) => {
let (value, children) = next.$get_value_and_children();
match children {
Expand Down Expand Up @@ -53,7 +53,7 @@ macro_rules! postorder_streaming_iterator_impl {
fn advance(&mut self) {
let mut is_first_iteration = true;
loop {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(next) => {
let (value, children) = next.$get_value_and_children();
match children {
Expand Down
1 change: 1 addition & 0 deletions src/dfs_postorder_iterators/mut_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
1 change: 1 addition & 0 deletions src/dfs_postorder_iterators/owned.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
1 change: 1 addition & 0 deletions src/dfs_preorder_iterators/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::StreamingIterator;
use crate::{
prelude::{
Expand Down
4 changes: 2 additions & 2 deletions src/dfs_preorder_iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod borrow;
macro_rules! dfs_preorder_next {
($get_value_and_children: ident) => {
fn next(&mut self) -> Option<Self::Item> {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(next) => {
let (value, children) = next.$get_value_and_children();
match children {
Expand Down Expand Up @@ -60,7 +60,7 @@ macro_rules! dfs_preorder_next {
macro_rules! advance_dfs {
($get_value_and_children: ident) => {
fn advance_dfs(&mut self) {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(next) => {
let (value, children) = next.$get_value_and_children();
match children {
Expand Down
1 change: 1 addition & 0 deletions src/dfs_preorder_iterators/mut_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
1 change: 1 addition & 0 deletions src/dfs_preorder_iterators/owned.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::vec::Vec;
use streaming_iterator::{
StreamingIterator,
StreamingIteratorMut
Expand Down
131 changes: 70 additions & 61 deletions src/examples.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#[cfg(test)]
pub (crate) mod tests {
use super::{create_example_binary_tree, create_example_tree};
pub mod tests {
extern crate std;
use std::println;
use alloc::string::String;
use super::super::prelude::*;
use std::collections::VecDeque;
use alloc::collections::VecDeque;
use alloc::boxed::Box;
use alloc::vec;
use super::{create_example_binary_tree, create_example_tree};

#[test]
fn bfs_example() {
Expand Down Expand Up @@ -258,6 +263,8 @@ pub (crate) mod tests {
println!("{}", result);
}

use alloc::string::ToString;
use alloc::vec::Vec;
use streaming_iterator::StreamingIterator;

#[test]
Expand Down Expand Up @@ -339,9 +346,68 @@ pub (crate) mod tests {
// result: 0 2 6
println!("{}", result);
}

mod custom_implemenation {
use crate::prelude::*;
use alloc::collections::LinkedList;

struct LLTreeNode<T> {
value: T,
children: LinkedList<LLTreeNode<T>>
}

use alloc::collections::linked_list::IntoIter;

impl<T> OwnedTreeNode for LLTreeNode<T> {
type OwnedValue = T;
type OwnedChildren = IntoIter<LLTreeNode<T>>;

fn get_value_and_children(self) -> (Self::OwnedValue, Option<Self::OwnedChildren>) {
(
self.value,
Some(self.children.into_iter())
)
}
}

use alloc::collections::linked_list::IterMut;

impl<'a, T> MutBorrowedTreeNode<'a> for LLTreeNode<T>
where Self: 'a {

type MutBorrowedValue = &'a mut T;
type MutBorrowedChildren = IterMut<'a, LLTreeNode<T>>;

fn get_value_and_children_iter_mut(&'a mut self) -> (Self::MutBorrowedValue, Option<Self::MutBorrowedChildren>) {
(
&mut self.value,
Some(self.children.iter_mut())
)
}
}

use alloc::collections::linked_list::Iter;

impl<'a, T> BorrowedTreeNode<'a> for LLTreeNode<T>
where Self: 'a {

type BorrowedValue = &'a T;
type BorrowedChildren = Iter<'a, LLTreeNode<T>>;

fn get_value_and_children_iter(&'a self) -> (Self::BorrowedValue, Option<Self::BorrowedChildren>) {
(
&self.value,
Some(self.children.iter())
)
}
}
}


}

use crate::prelude::*;
use alloc::{boxed::Box, vec};

pub fn create_example_binary_tree() -> BinaryTreeNode<usize> {
BinaryTreeNode {
Expand Down Expand Up @@ -483,61 +549,4 @@ pub fn create_example_tree() -> TreeNode<usize> {
}
])
}
}

mod custom_implemenation {
use crate::prelude::*;
use std::collections::LinkedList;

struct LLTreeNode<T> {
value: T,
children: LinkedList<LLTreeNode<T>>
}

use std::collections::linked_list::IntoIter;

impl<T> OwnedTreeNode for LLTreeNode<T> {
type OwnedValue = T;
type OwnedChildren = IntoIter<LLTreeNode<T>>;

fn get_value_and_children(self) -> (Self::OwnedValue, Option<Self::OwnedChildren>) {
(
self.value,
Some(self.children.into_iter())
)
}
}

use std::collections::linked_list::IterMut;

impl<'a, T> MutBorrowedTreeNode<'a> for LLTreeNode<T>
where Self: 'a {

type MutBorrowedValue = &'a mut T;
type MutBorrowedChildren = IterMut<'a, LLTreeNode<T>>;

fn get_value_and_children_iter_mut(&'a mut self) -> (Self::MutBorrowedValue, Option<Self::MutBorrowedChildren>) {
(
&mut self.value,
Some(self.children.iter_mut())
)
}
}

use std::collections::linked_list::Iter;

impl<'a, T> BorrowedTreeNode<'a> for LLTreeNode<T>
where Self: 'a {

type BorrowedValue = &'a T;
type BorrowedChildren = Iter<'a, LLTreeNode<T>>;

fn get_value_and_children_iter(&'a self) -> (Self::BorrowedValue, Option<Self::BorrowedChildren>) {
(
&self.value,
Some(self.children.iter())
)
}
}
}

}
8 changes: 4 additions & 4 deletions src/leaves_iterators/breadth_first/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::VecDeque;
use alloc::collections::VecDeque;

use crate::prelude::{
BorrowedTreeNode,
BinaryChildren,
BorrowedBinaryTreeNode
};

use crate::make_peekable_iterator::MakePeekableIterator;
use core::iter::Peekable;

use super::{
bfs_next,
Expand All @@ -18,7 +18,7 @@ pub struct BorrowedLeavesIterator<'a, Node>

pub (crate) root: Option<&'a Node>,
pub (crate) old_traversal_queue: VecDeque<Node::BorrowedChildren>,
pub (crate) new_traversal_queue: VecDeque<MakePeekableIterator<Node::BorrowedChildren>>,
pub (crate) new_traversal_queue: VecDeque<Peekable<Node::BorrowedChildren>>,
}

impl<'a, Node> BorrowedLeavesIterator<'a, Node>
Expand All @@ -39,7 +39,7 @@ pub struct BorrowedBinaryLeavesIterator<'a, Node>

pub (crate) root: Option<&'a Node>,
pub (crate) old_traversal_queue: VecDeque<BinaryChildren<&'a Node>>,
pub (crate) new_traversal_queue: VecDeque<MakePeekableIterator<BinaryChildren<&'a Node>>>,
pub (crate) new_traversal_queue: VecDeque<Peekable<BinaryChildren<&'a Node>>>,
}

impl<'a, Node> BorrowedBinaryLeavesIterator<'a, Node>
Expand Down
8 changes: 4 additions & 4 deletions src/leaves_iterators/breadth_first/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ pub mod borrow;
macro_rules! bfs_next {
($get_value_and_children: ident, $value_type: ty) => {
fn bfs_next(&mut self) -> Option<(bool, $value_type)> {
match std::mem::take(&mut self.root) {
match core::mem::take(&mut self.root) {
Some(root) => {
let (value, children) = root.$get_value_and_children();

let has_children;
match children {
None => { has_children = false; }
Some(children) => {
let mut peekable = MakePeekableIterator::new(children);
let mut peekable = children.peekable();
has_children = peekable.peek().is_some();
self.new_traversal_queue.push_back(peekable);
}
Expand Down Expand Up @@ -41,7 +41,7 @@ macro_rules! bfs_next {
match children {
None => { has_children = false; }
Some(children) => {
let mut peekable = MakePeekableIterator::new(children);
let mut peekable = children.peekable();
has_children = peekable.peek().is_some();
self.new_traversal_queue.push_back(peekable);
}
Expand All @@ -66,7 +66,7 @@ macro_rules! bfs_next {
match children {
None => { has_children = false; }
Some(children) => {
let mut peekable = MakePeekableIterator::new(children);
let mut peekable = children.peekable();
has_children = peekable.peek().is_some();
self.new_traversal_queue.push_back(peekable);
}
Expand Down
Loading

0 comments on commit 0ccd4f3

Please sign in to comment.