Skip to content

Commit

Permalink
walk: always index
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Nov 20, 2024
1 parent 12fc793 commit 39a0240
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
24 changes: 12 additions & 12 deletions miniconf/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! impl_tuple {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let k = KeyLookup::numbered($n);
let mut walk = W::internal();
$(walk = walk.merge(&$t::traverse_all()?, Some($i), &k)?;)+
$(walk = walk.merge(&$t::traverse_all()?, $i, &k)?;)+
Ok(walk)
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl<const L: usize, const R: usize> Assert<L, R> {
impl<T: TreeKey, const N: usize> TreeKey for [T; N] {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let () = Assert::<N, 0>::GREATER; // internal nodes must have at least one leaf
W::internal().merge(&T::traverse_all()?, None, &KeyLookup::homogeneous(N))
W::internal().merge(&T::traverse_all()?, 0, &KeyLookup::homogeneous(N))
}

fn traverse_by_key<K, F, E>(mut keys: K, mut func: F) -> Result<usize, Error<E>>
Expand Down Expand Up @@ -248,8 +248,8 @@ impl<T: TreeKey, E: TreeKey> TreeKey for Result<T, E> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
W::internal()
.merge(&T::traverse_all()?, Some(0), &RESULT_LOOKUP)?
.merge(&E::traverse_all()?, Some(1), &RESULT_LOOKUP)
.merge(&T::traverse_all()?, 0, &RESULT_LOOKUP)?
.merge(&E::traverse_all()?, 1, &RESULT_LOOKUP)
}

#[inline]
Expand Down Expand Up @@ -333,8 +333,8 @@ impl<T: TreeKey> TreeKey for Bound<T> {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let t = T::traverse_all()?;
W::internal()
.merge(&t, Some(0), &BOUND_LOOKUP)?
.merge(&t, Some(1), &BOUND_LOOKUP)
.merge(&t, 0, &BOUND_LOOKUP)?
.merge(&t, 1, &BOUND_LOOKUP)
}

#[inline]
Expand Down Expand Up @@ -417,8 +417,8 @@ impl<T: TreeKey> TreeKey for Range<T> {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let t = T::traverse_all()?;
W::internal()
.merge(&t, Some(0), &RANGE_LOOKUP)?
.merge(&t, Some(1), &RANGE_LOOKUP)
.merge(&t, 0, &RANGE_LOOKUP)?
.merge(&t, 1, &RANGE_LOOKUP)
}

#[inline]
Expand Down Expand Up @@ -499,8 +499,8 @@ impl<T: TreeKey> TreeKey for RangeInclusive<T> {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let t = T::traverse_all()?;
W::internal()
.merge(&t, Some(0), &RANGE_LOOKUP)?
.merge(&t, Some(1), &RANGE_LOOKUP)
.merge(&t, 0, &RANGE_LOOKUP)?
.merge(&t, 1, &RANGE_LOOKUP)
}

#[inline]
Expand Down Expand Up @@ -538,7 +538,7 @@ const RANGE_FROM_LOOKUP: KeyLookup = KeyLookup::Named(&["start"]);
impl<T: TreeKey> TreeKey for RangeFrom<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
W::internal().merge(&T::traverse_all()?, Some(0), &RANGE_FROM_LOOKUP)
W::internal().merge(&T::traverse_all()?, 0, &RANGE_FROM_LOOKUP)
}

#[inline]
Expand Down Expand Up @@ -615,7 +615,7 @@ const RANGE_TO_LOOKUP: KeyLookup = KeyLookup::Named(&["end"]);
impl<T: TreeKey> TreeKey for RangeTo<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
W::internal().merge(&T::traverse_all()?, Some(0), &RANGE_TO_LOOKUP)
W::internal().merge(&T::traverse_all()?, 0, &RANGE_TO_LOOKUP)
}

#[inline]
Expand Down
11 changes: 3 additions & 8 deletions miniconf/src/trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ impl Walk for TreeWalk {
fn leaf() -> Self {
Self(Tree::new(None))
}
fn merge(
mut self,
walk: &Self,
index: Option<usize>,
lookup: &KeyLookup,
) -> Result<Self, Self::Error> {
fn merge(mut self, walk: &Self, index: usize, lookup: &KeyLookup) -> Result<Self, Self::Error> {
if let Some(l) = self.0.root().data() {
debug_assert_eq!(l, lookup);
}
self.0
.root_mut()
.data_mut()
.get_or_insert_with(|| lookup.clone());
if let Some(index) = index {
debug_assert_eq!(index, self.0.root().degree());
if matches!(lookup, KeyLookup::Homogeneous(_)) {
debug_assert_eq!(index, 0);
}
self.0.push_back(walk.0.clone());
Ok(self)
Expand Down
34 changes: 11 additions & 23 deletions miniconf/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ pub trait Walk: Sized {
///
/// # Args
/// * `walk`: The walk of the node to merge.
/// * `index`: Either the node index in case of a single node
/// or `None`, in case of `lookup.len` nodes of homogeneous type.
/// * `index`: Either the child index or zero for homogeneous.
/// * `lookup`: The namespace the node(s) are in.
fn merge(
self,
walk: &Self,
index: Option<usize>,
lookup: &KeyLookup,
) -> Result<Self, Self::Error>;
fn merge(self, walk: &Self, index: usize, lookup: &KeyLookup) -> Result<Self, Self::Error>;
}

impl Walk for Metadata {
Expand All @@ -80,25 +74,19 @@ impl Walk for Metadata {
}

#[inline]
fn merge(
mut self,
meta: &Self,
index: Option<usize>,
lookup: &KeyLookup,
) -> Result<Self, Self::Error> {
let (ident_len, count) = match lookup {
KeyLookup::Named(names) => (names[index.unwrap()].len(), 1),
KeyLookup::Numbered(_len) => (
index.unwrap().checked_ilog10().unwrap_or_default() as usize + 1,
1,
),
fn merge(mut self, meta: &Self, index: usize, lookup: &KeyLookup) -> Result<Self, Self::Error> {
let (len, count) = match lookup {
KeyLookup::Named(names) => (names[index].len(), 1),
KeyLookup::Numbered(_len) => {
(index.checked_ilog10().unwrap_or_default() as usize + 1, 1)
}
KeyLookup::Homogeneous(len) => {
assert!(index.is_none());
(len.get().ilog10() as usize + 1, len.get())
debug_assert_eq!(index, 0);
(len.ilog10() as usize + 1, len.get())
}
};
self.max_depth = self.max_depth.max(1 + meta.max_depth);
self.max_length = self.max_length.max(ident_len + meta.max_length);
self.max_length = self.max_length.max(len + meta.max_length);
debug_assert_ne!(meta.count, 0);
self.count += count * meta.count;
self.max_bits = self
Expand Down
2 changes: 1 addition & 1 deletion miniconf_derive/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Tree {
if self.flatten.is_present() {
quote!(walk = #w;)
} else {
quote!(walk = walk.merge(&#w, Some(#i), &Self::__MINICONF_LOOKUP)?;)
quote!(walk = walk.merge(&#w, #i, &Self::__MINICONF_LOOKUP)?;)
}
});
let traverse_arms = fields.iter().enumerate().map(|(i, f)| f.traverse_by_key(i));
Expand Down

0 comments on commit 39a0240

Please sign in to comment.