Skip to content

Commit

Permalink
Merge pull request #255 from quartiq/indices
Browse files Browse the repository at this point in the history
indices
  • Loading branch information
jordens authored Oct 20, 2024
2 parents 4ce8bec + f60eee4 commit 6f0d12a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.16.3](https://github.com/quartiq/miniconf/compare/v0.16.2...v0.16.3) - 2024-10-20

### Added

* `Transcode` slices of any integer type

## [0.16.2](https://github.com/quartiq/miniconf/compare/v0.16.1...v0.16.2) - 2024-10-17

### Added
Expand Down
2 changes: 1 addition & 1 deletion miniconf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "miniconf"
# Sync all crate versions and the py client
version = "0.16.2"
version = "0.16.3"
authors = [
"James Irwin <irwineffect@gmail.com>",
"Ryan Summers <ryan.summers@vertigo-designs.com>",
Expand Down
8 changes: 4 additions & 4 deletions miniconf/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::marker::PhantomData;

use crate::{Indices, IntoKeys, KeyLookup, Keys, Metadata, Node, Transcode, Traversal, TreeKey};
use crate::{IntoKeys, KeyLookup, Keys, Metadata, Node, Transcode, Traversal, TreeKey};

/// Counting wrapper for iterators with known exact size
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct NodeIter<M: ?Sized, const Y: usize, N, const D: usize = Y> {
// We can't use Packed as state since we need to be able to modify the
// indices directly. Packed erases knowledge of the bit widths of the individual
// indices.
state: Indices<[usize; D]>,
state: [usize; D],
root: usize,
depth: usize,
_n: PhantomData<N>,
Expand All @@ -87,7 +87,7 @@ pub struct NodeIter<M: ?Sized, const Y: usize, N, const D: usize = Y> {
impl<M: ?Sized, const Y: usize, N, const D: usize> Default for NodeIter<M, Y, N, D> {
fn default() -> Self {
Self {
state: Indices::default(),
state: [0; D],
root: 0,
// Marker to prevent initial index increment in `next()`
depth: D + 1,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<M: TreeKey<Y> + ?Sized, const Y: usize, N, const D: usize> NodeIter<M, Y, N
pub fn exact_size(self) -> ExactSize<Self> {
assert!(self.depth == D + 1);
assert!(self.root == 0);
debug_assert_eq!(&self.state, &Indices::default()); // ensured by depth = D + 1 marker
debug_assert_eq!(&self.state, &[0; D]); // ensured by depth = D + 1 marker
assert!(D >= Y);
ExactSize::new(self, M::traverse_all::<Metadata>().unwrap().count)
}
Expand Down
11 changes: 6 additions & 5 deletions miniconf/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ where
}

// index
macro_rules! impl_key {
macro_rules! impl_key_integer {
($($t:ty)+) => {$(
impl Key for $t {
#[inline]
fn find(&self, _lookup: &KeyLookup) -> Option<usize> {
Some(*self as _)
(*self).try_into().ok()
}
})+
};
}
)+};
}
impl_key!(usize u8 u16 u32);
impl_key_integer!(usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128);

// name
impl Key for str {
Expand Down
32 changes: 22 additions & 10 deletions miniconf/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ impl<'a, const S: char> PathIter<'a, S> {
impl<'a, const S: char> Iterator for PathIter<'a, S> {
type Item = &'a str;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.map(|s| {
let pos = s
Expand Down Expand Up @@ -324,18 +323,31 @@ impl<T: AsMut<[usize]> + ?Sized> Transcode for Indices<T> {
M: TreeKey<Y> + ?Sized,
K: IntoKeys,
{
let mut it = self.0.as_mut().iter_mut();
M::traverse_by_key(keys.into_keys(), |index, _name, _len| {
it.next()
.map(|idx| {
*idx = index;
})
.ok_or(())
})
.try_into()
self.0.as_mut().transcode::<M, Y, _>(keys)
}
}

macro_rules! impl_transcode_slice {
($($t:ty)+) => {$(
impl Transcode for [$t] {
fn transcode<M, const Y: usize, K>(&mut self, keys: K) -> Result<Node, Traversal>
where
M: TreeKey<Y> + ?Sized,
K: IntoKeys,
{
let mut it = self.iter_mut();
M::traverse_by_key(keys.into_keys(), |index, _name, _len| {
let idx = it.next().ok_or(())?;
*idx = index.try_into().or(Err(()))?;
Ok(())
})
.try_into()
}
}
)+};
}
impl_transcode_slice!(usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128);

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 6f0d12a

Please sign in to comment.