Skip to content

Commit

Permalink
Add map::Value imps for bool and Option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
korken89 committed Aug 4, 2024
1 parent 163dfc5 commit 73ff739
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Unreleased

- Added `Value` impls for `bool` and `Option<T: Value>`

## 3.0.1 25-07-24

- Add `defmt` attributes to cache types.
Expand Down
35 changes: 35 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,41 @@ impl<'a, const N: usize> Value<'a> for [u8; N] {
}
}

impl<'a> Value<'a> for bool {
fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, SerializationError> {
<u8 as Value>::serialize_into(&(*self as u8), buffer)
}

fn deserialize_from(buffer: &'a [u8]) -> Result<Self, SerializationError>
where
Self: Sized,
{
Ok(<u8 as Value>::deserialize_from(buffer)? != 0)
}
}

impl<'a, T: Value<'a>> Value<'a> for Option<T> {
fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, SerializationError> {
if let Some(val) = self {
<bool as Value>::serialize_into(&true, buffer)?;
<T as Value>::serialize_into(val, buffer)
} else {
<bool as Value>::serialize_into(&false, buffer)
}
}

fn deserialize_from(buffer: &'a [u8]) -> Result<Self, SerializationError>
where
Self: Sized,
{
if <bool as Value>::deserialize_from(buffer)? {
Ok(Some(<T as Value>::deserialize_from(buffer)?))
} else {
Ok(None)
}
}
}

macro_rules! impl_map_item_num {
($int:ty) => {
impl<'a> Value<'a> for $int {
Expand Down

0 comments on commit 73ff739

Please sign in to comment.