diff --git a/CHANGELOG.md b/CHANGELOG.md index a821c0cf..1115dfc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/miniconf/Cargo.toml b/miniconf/Cargo.toml index e1ff01c7..f2aab9c0 100644 --- a/miniconf/Cargo.toml +++ b/miniconf/Cargo.toml @@ -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 ", "Ryan Summers ", diff --git a/miniconf/src/iter.rs b/miniconf/src/iter.rs index 64870c86..34617cac 100644 --- a/miniconf/src/iter.rs +++ b/miniconf/src/iter.rs @@ -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)] @@ -77,7 +77,7 @@ pub struct NodeIter { // 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, @@ -87,7 +87,7 @@ pub struct NodeIter { impl Default for NodeIter { fn default() -> Self { Self { - state: Indices::default(), + state: [0; D], root: 0, // Marker to prevent initial index increment in `next()` depth: D + 1, @@ -116,7 +116,7 @@ impl + ?Sized, const Y: usize, N, const D: usize> NodeIter ExactSize { 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::().unwrap().count) } diff --git a/miniconf/src/key.rs b/miniconf/src/key.rs index 53f933b0..203e04d5 100644 --- a/miniconf/src/key.rs +++ b/miniconf/src/key.rs @@ -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 { - 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 { diff --git a/miniconf/src/node.rs b/miniconf/src/node.rs index 306c428c..8bb69bab 100644 --- a/miniconf/src/node.rs +++ b/miniconf/src/node.rs @@ -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.0.map(|s| { let pos = s @@ -324,18 +323,31 @@ impl + ?Sized> Transcode for Indices { M: TreeKey + ?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::(keys) } } +macro_rules! impl_transcode_slice { + ($($t:ty)+) => {$( + impl Transcode for [$t] { + fn transcode(&mut self, keys: K) -> Result + where + M: TreeKey + ?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::*;