Skip to content

Commit

Permalink
impls: Bound
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Nov 12, 2024
1 parent 8704861 commit c80b95b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* `std` and `alloc` features and `Tree*` impls for `Box`, `Rc`, `Arc`, `Cow`, `Mutex`,
`RwLock`, `Cell`, `RefCell`
* `std` and `alloc` features and `Tree*` impls for `Box`, `Rc`, `RcWeak`, `Arc`, `ArcWeak`,
`Cow`, `Mutex`, `RwLock`, `Cell`, `RefCell`, `Bound`

## [0.17.0](https://github.com/quartiq/miniconf/compare/v0.16.3...v0.17.0) - 2024-10-25

Expand Down
89 changes: 88 additions & 1 deletion miniconf/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::cell::{Cell, RefCell};
use core::ops::Bound;
use core::{any::Any, num::NonZero};

use serde::{Deserializer, Serializer};
Expand Down Expand Up @@ -242,7 +243,7 @@ impl<T: TreeAny> TreeAny for Option<T> {
/////////////////////////////////////////////////////////////////////////////////////////

const RESULT_LOOKUP: KeyLookup = KeyLookup {
len: NonZero::<usize>::MIN.saturating_add(1),
len: NonZero::<usize>::MIN.saturating_add(1), // 2
names: Some(&["Ok", "Err"]),
};

Expand Down Expand Up @@ -328,6 +329,92 @@ impl<T: TreeAny, E: TreeAny> TreeAny for Result<T, E> {

/////////////////////////////////////////////////////////////////////////////////////////

const BOUND_LOOKUP: KeyLookup = KeyLookup {
len: NonZero::<usize>::MIN.saturating_add(1),
names: Some(&["Included", "Excluded"]),
};

impl<T: TreeKey> TreeKey for Bound<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
W::internal()
.merge(&T::traverse_all()?, Some(0), &BOUND_LOOKUP)?
.merge(&T::traverse_all()?, Some(1), &BOUND_LOOKUP)
}

#[inline]
fn traverse_by_key<K, F, G>(mut keys: K, func: F) -> Result<usize, Error<G>>
where
K: Keys,
F: FnMut(usize, Option<&'static str>, NonZero<usize>) -> Result<(), G>,
{
Error::increment_result(match keys.next(&BOUND_LOOKUP)? {
0..=1 => T::traverse_by_key(keys, func),
_ => unreachable!(),
})
}
}

impl<T: TreeSerialize> TreeSerialize for Bound<T> {
#[inline]
fn serialize_by_key<K, S>(&self, mut keys: K, ser: S) -> Result<usize, Error<S::Error>>
where
K: Keys,
S: Serializer,
{
Error::increment_result(match (keys.next(&BOUND_LOOKUP)?, self) {
(0, Self::Included(value)) | (1, Self::Excluded(value)) => {
value.serialize_by_key(keys, ser)
}
_ => Err(Traversal::Absent(0).into()),
})
}
}

impl<'de, T: TreeDeserialize<'de>> TreeDeserialize<'de> for Bound<T> {
#[inline]
fn deserialize_by_key<K, D>(&mut self, mut keys: K, de: D) -> Result<usize, Error<D::Error>>
where
K: Keys,
D: Deserializer<'de>,
{
Error::increment_result(match (keys.next(&BOUND_LOOKUP)?, self) {
(0, Self::Included(value)) | (1, Self::Excluded(value)) => {
value.deserialize_by_key(keys, de)
}
_ => Err(Traversal::Absent(0).into()),
})
}
}

impl<T: TreeAny> TreeAny for Bound<T> {
#[inline]
fn ref_any_by_key<K>(&self, mut keys: K) -> Result<&dyn Any, Traversal>
where
K: Keys,
{
match (keys.next(&BOUND_LOOKUP)?, self) {
(0, Self::Included(value)) | (1, Self::Excluded(value)) => value.ref_any_by_key(keys),
_ => Err(Traversal::Absent(0)),
}
.map_err(Traversal::increment)
}

#[inline]
fn mut_any_by_key<K>(&mut self, mut keys: K) -> Result<&mut dyn Any, Traversal>
where
K: Keys,
{
match (keys.next(&BOUND_LOOKUP)?, self) {
(0, Self::Included(value)) | (1, Self::Excluded(value)) => value.mut_any_by_key(keys),
_ => Err(Traversal::Absent(0)),
}
.map_err(Traversal::increment)
}
}

/////////////////////////////////////////////////////////////////////////////////////////

impl<T: TreeKey> TreeKey for Cell<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
Expand Down

0 comments on commit c80b95b

Please sign in to comment.