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 13f5030
Show file tree
Hide file tree
Showing 2 changed files with 38 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
36 changes: 36 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,42 @@ 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 {
<u8 as Value>::serialize_into(&1, buffer)?;
<T as Value>::serialize_into(val, buffer)
} else {
<u8 as Value>::serialize_into(&0, buffer)
}
}

fn deserialize_from(buffer: &'a [u8]) -> Result<Self, SerializationError>
where
Self: Sized,
{
let descriminant = <u8 as Value>::deserialize_from(buffer)?;
match descriminant {
0 => Ok(None),
1 => Ok(Some(<T as Value>::deserialize_from(buffer)?)),
_ => Err(SerializationError::InvalidFormat),
}
}
}

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

0 comments on commit 13f5030

Please sign in to comment.