-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements and a new tree!
macro
#110
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
175896a
fix(arena): return slice iterator instead of impl trait
alexmozaidze 0a9e5e4
feat(arena): expose underlying nodes as `&[Node<T>]`
alexmozaidze 62d37d2
feat(traverse): implement `DoubleEndedIterator` for sibling iterators
alexmozaidze 155027b
refactor(traverse): use `.and_then()` instead of `.map().flatten()`
alexmozaidze f4137c3
fix(id): deprecate `NodeId::reverse_children`
alexmozaidze 25bc593
feat: add indextree-macros and the `tree!` macro
alexmozaidze 003736d
feat(macros): made `tree!` macro more ergonomic
alexmozaidze 8561907
refactor(macros): silence `clippy::useless_transmute` warnings
alexmozaidze bd11914
refactor: ran `cargo fmt` and removed `required_version` from `.rustf…
alexmozaidze 0347466
fix(macros): use `TypeId::of` instead of `type_name*` for type checking
alexmozaidze f12c4fb
feat(macros): use autoref specialization instead of unsound assumptio…
alexmozaidze 72dca49
fix(macros): improve identifier sandboxing for `tree!`
alexmozaidze deb4c98
fix(macros): allow a trailing comma in `tree!`
alexmozaidze 43eb8e9
feat(macros): allow omitting `=> {}` for root node in `tree!`
alexmozaidze 1deb430
build(macros): add tests for `tree!`
alexmozaidze 8c09d1c
refactor(macros): update HACK comment for `tree!`
alexmozaidze cb9fe98
fix(macros): refactor and optimize `tree!` macro
alexmozaidze 62cb693
style: ran `cargo fmt`
alexmozaidze 42131c8
docs(macros): add documentation to `tree!`
alexmozaidze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,6 @@ | ||
[package] | ||
name = "indextree" | ||
version = "4.7.0" | ||
license = "MIT" | ||
readme = "README.md" | ||
keywords = ["tree", "arena", "index", "indextree", "trie"] | ||
authors = ["Sascha Grunert <mail@saschagruenrt.de>"] | ||
repository = "https://github.com/saschagrunert/indextree" | ||
homepage = "https://github.com/saschagrunert/indextree" | ||
documentation = "https://docs.rs/indextree" | ||
description = "Arena based tree structure by using indices instead of reference counted pointers" | ||
categories = ["data-structures"] | ||
edition = "2021" | ||
|
||
[features] | ||
default = ["std"] | ||
deser = ["serde"] | ||
par_iter = ["rayon"] | ||
std = [] | ||
|
||
[dependencies] | ||
rayon = { version = "1.7.0", optional = true } | ||
serde = { version = "1.0.154", features = ["derive"], optional = true } | ||
|
||
[[example]] | ||
name = "parallel_iteration" | ||
required-features = ["par_iter"] | ||
|
||
[[example]] | ||
name = "simple" | ||
[workspace] | ||
resolver = "2" | ||
members = [ | ||
"indextree", | ||
"indextree-macros", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "indextree-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.86" | ||
quote = "1.0.36" | ||
thiserror = "1.0.61" | ||
either = "1.13.0" | ||
syn = { version = "2.0.71", features = ["extra-traits", "full", "visit"] } | ||
itertools = "0.13.0" | ||
strum = { version = "0.26.3", features = ["derive"] } | ||
|
||
[dev-dependencies] | ||
indextree = { path = "../indextree" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,309 @@ | ||
use either::Either; | ||
use itertools::Itertools; | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use strum::EnumDiscriminants; | ||
use syn::{ | ||
braced, | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
punctuated::Punctuated, | ||
Expr, Token, | ||
}; | ||
|
||
#[derive(Clone, Debug)] | ||
struct IndexNode { | ||
node: Expr, | ||
children: Punctuated<Self, Token![,]>, | ||
} | ||
|
||
impl Parse for IndexNode { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let node = input.parse::<Expr>()?; | ||
|
||
if input.parse::<Token![=>]>().is_err() { | ||
return Ok(IndexNode { | ||
node, | ||
children: Punctuated::new(), | ||
}); | ||
} | ||
|
||
let children_stream; | ||
braced!(children_stream in input); | ||
let children = children_stream.parse_terminated(Self::parse, Token![,])?; | ||
alexmozaidze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(IndexNode { node, children }) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
struct IndexTree { | ||
arena: Expr, | ||
root_node: Expr, | ||
nodes: Punctuated<IndexNode, Token![,]>, | ||
} | ||
|
||
impl Parse for IndexTree { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let arena = input.parse::<Expr>()?; | ||
|
||
input.parse::<Token![,]>()?; | ||
|
||
let root_node = input.parse::<Expr>()?; | ||
|
||
let nodes = if input.parse::<Token![=>]>().is_ok() { | ||
let braced_nodes; | ||
braced!(braced_nodes in input); | ||
braced_nodes.parse_terminated(IndexNode::parse, Token![,])? | ||
} else { | ||
Punctuated::new() | ||
}; | ||
|
||
let _ = input.parse::<Token![,]>(); | ||
|
||
Ok(IndexTree { | ||
arena, | ||
root_node, | ||
nodes, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Clone, EnumDiscriminants, Debug)] | ||
#[strum_discriminants(name(ActionKind))] | ||
enum Action { | ||
Append(Expr), | ||
Parent, | ||
Nest, | ||
} | ||
|
||
impl ToTokens for Action { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
tokens.extend(self.to_stream()) | ||
} | ||
} | ||
|
||
impl Action { | ||
fn to_stream(&self) -> TokenStream { | ||
match self { | ||
Action::Append(expr) => quote! { | ||
__last = __node.append_value(#expr, __arena); | ||
}, | ||
Action::Parent => quote! { | ||
let __temp = ::indextree::Arena::get(__arena, __node); | ||
let __temp = ::core::option::Option::unwrap(__temp); | ||
let __temp = ::indextree::Node::parent(__temp); | ||
let __temp = ::core::option::Option::unwrap(__temp); | ||
__node = __temp; | ||
}, | ||
Action::Nest => quote! { | ||
__node = __last; | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
struct NestingLevelMarker; | ||
|
||
#[derive(Clone, Debug)] | ||
struct ActionStream { | ||
count: usize, | ||
kind: ActionKind, | ||
stream: TokenStream, | ||
} | ||
|
||
impl ToTokens for ActionStream { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
tokens.extend(self.stream.clone()); | ||
} | ||
} | ||
|
||
/// Construct a tree for a given arena. | ||
/// | ||
/// This macro creates a tree in an [`Arena`] with a pre-defined layout. If the root node is of | ||
/// type [`NodeId`], then that [`NodeId`] is used for the root node, but if it's any other type, | ||
/// then it creates a new root node on-the-fly. The macro returns [`NodeId`] of the root node. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use indextree::{Arena, macros::tree}; | ||
/// # let mut arena = Arena::new(); | ||
/// let root_node = arena.new_node("root node"); | ||
/// tree!( | ||
/// &mut arena, | ||
/// root_node => { | ||
/// "1", | ||
/// "2" => { | ||
/// "2_1" => { "2_1_1" }, | ||
/// "2_2", | ||
/// }, | ||
/// "3", | ||
/// } | ||
/// ); | ||
/// | ||
/// let automagical_root_node = tree!( | ||
/// &mut arena, | ||
/// "root node, but automagically created" => { | ||
/// "1", | ||
/// "2" => { | ||
/// "2_1" => { "2_1_1" }, | ||
/// "2_2", | ||
/// }, | ||
/// "3", | ||
/// } | ||
/// ); | ||
/// ``` | ||
/// | ||
/// Note that you can anchor the root node in the macro to any node at any nesting. So you can take | ||
/// an already existing node of a tree and attach another tree to it: | ||
/// ``` | ||
/// # use indextree::{Arena, macros::tree}; | ||
/// # let mut arena = Arena::new(); | ||
/// let root_node = tree!( | ||
/// &mut arena, | ||
/// "root node" => { | ||
/// "1", | ||
/// "2", | ||
/// "3", | ||
/// } | ||
/// ); | ||
/// | ||
/// let node_1 = arena.get(root_node).unwrap().first_child().unwrap(); | ||
/// let node_2 = arena.get(node_1).unwrap().next_sibling().unwrap(); | ||
/// tree!( | ||
/// &mut arena, | ||
/// node_2 => { | ||
/// "2_1" => { "2_1_1" }, | ||
/// "2_2", | ||
/// } | ||
/// ); | ||
/// ``` | ||
/// | ||
/// It is also possible to create an empty root_node, although, I'm not sure why you'd want to do | ||
/// that. | ||
/// ``` | ||
/// # use indextree::{Arena, macros::tree}; | ||
/// # let mut arena = Arena::new(); | ||
/// let root_node = tree!( | ||
/// &mut arena, | ||
/// "my root node", | ||
/// ); | ||
/// ``` | ||
/// Empty nodes can also be defined as `=> {}` | ||
/// ``` | ||
/// # use indextree::{Arena, macros::tree}; | ||
/// # let mut arena = Arena::new(); | ||
/// let root_node = tree!( | ||
/// &mut arena, | ||
/// "my root node" => {}, | ||
/// ); | ||
/// ``` | ||
/// | ||
/// [`Arena`]: https://docs.rs/indextree/latest/indextree/struct.Arena.html | ||
/// [`NodeId`]: https://docs.rs/indextree/latest/indextree/struct.NodeId.html | ||
#[proc_macro] | ||
pub fn tree(input: proc_macro::TokenStream) -> proc_macro::TokenStream { | ||
let IndexTree { | ||
arena, | ||
root_node, | ||
nodes, | ||
} = parse_macro_input!(input as IndexTree); | ||
|
||
let mut stack: Vec<Either<_, NestingLevelMarker>> = | ||
nodes.into_iter().map(Either::Left).rev().collect(); | ||
|
||
let mut action_buffer: Vec<Action> = Vec::new(); | ||
|
||
while let Some(item) = stack.pop() { | ||
let Either::Left(IndexNode { node, children }) = item else { | ||
action_buffer.push(Action::Parent); | ||
continue; | ||
}; | ||
|
||
action_buffer.push(Action::Append(node)); | ||
|
||
if children.is_empty() { | ||
continue; | ||
} | ||
|
||
// going one level deeper | ||
stack.push(Either::Right(NestingLevelMarker)); | ||
action_buffer.push(Action::Nest); | ||
stack.extend(children.into_iter().map(Either::Left).rev()); | ||
} | ||
|
||
let mut actions: Vec<ActionStream> = action_buffer | ||
.into_iter() | ||
.map(|action| ActionStream { | ||
count: 1, | ||
kind: ActionKind::from(&action), | ||
stream: action.to_stream(), | ||
}) | ||
.coalesce(|action1, action2| { | ||
if action1.kind != action2.kind { | ||
return Err((action1, action2)); | ||
} | ||
|
||
let count = action1.count + action2.count; | ||
let kind = action1.kind; | ||
let mut stream = action1.stream; | ||
stream.extend(action2.stream); | ||
Ok(ActionStream { | ||
count, | ||
kind, | ||
stream, | ||
}) | ||
}) | ||
.collect(); | ||
|
||
let is_last_action_useless = actions | ||
.last() | ||
.map(|last| last.kind == ActionKind::Parent) | ||
.unwrap_or(false); | ||
if is_last_action_useless { | ||
actions.pop(); | ||
} | ||
|
||
// HACK(alexmozaidze): Due to the fact that specialization is unstable, we must resort to | ||
// autoref specialization trick. | ||
// https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md | ||
quote! {{ | ||
let mut __arena: &mut ::indextree::Arena<_> = #arena; | ||
|
||
#[repr(transparent)] | ||
struct __Wrapping<__T>(::core::mem::ManuallyDrop<__T>); | ||
|
||
trait __ToNodeId<__T> { | ||
fn __to_node_id(&mut self, __arena: &mut ::indextree::Arena<__T>) -> ::indextree::NodeId; | ||
} | ||
|
||
trait __NodeIdToNodeId<__T> { | ||
fn __to_node_id(&mut self, __arena: &mut ::indextree::Arena<__T>) -> ::indextree::NodeId; | ||
} | ||
|
||
impl<__T> __NodeIdToNodeId<__T> for __Wrapping<::indextree::NodeId> { | ||
fn __to_node_id(&mut self, __arena: &mut ::indextree::Arena<__T>) -> ::indextree::NodeId { | ||
unsafe { ::core::mem::ManuallyDrop::take(&mut self.0) } | ||
} | ||
} | ||
|
||
impl<__T> __ToNodeId<__T> for &mut __Wrapping<__T> { | ||
fn __to_node_id(&mut self, __arena: &mut ::indextree::Arena<__T>) -> ::indextree::NodeId { | ||
::indextree::Arena::new_node(__arena, unsafe { ::core::mem::ManuallyDrop::take(&mut self.0) }) | ||
} | ||
} | ||
|
||
let __root_node: ::indextree::NodeId = { | ||
let mut __root_node = __Wrapping(::core::mem::ManuallyDrop::new(#root_node)); | ||
(&mut __root_node).__to_node_id(__arena) | ||
}; | ||
let mut __node: ::indextree::NodeId = __root_node; | ||
let mut __last: ::indextree::NodeId; | ||
|
||
#(#actions)* | ||
|
||
__root_node | ||
}}.into() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this file as part of this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, editorconfig helps with consistency on different editors (I personally like tab-indentation more, but I set it up to whatever was used in the project). I can remove it if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it should be fine.