mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-28 13:00:26 +00:00
Merge pull request #476 from Nikita240/hisbuf
Add serde implementations to HistoryBuffer
This commit is contained in:
commit
310c09d517
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
CHANGELOG.md merge=union
|
||||||
|
|
@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
- Added pool implementations for 64-bit architectures.
|
- Added pool implementations for 64-bit architectures.
|
||||||
- Added `IntoIterator` implementation for `LinearMap`
|
- Added `IntoIterator` implementation for `LinearMap`
|
||||||
- Added `Deque::{get, get_mut, get_unchecked, get_unchecked_mut}`.
|
- Added `Deque::{get, get_mut, get_unchecked, get_unchecked_mut}`.
|
||||||
|
- Added `serde::Serialize` and `serde::Deserialize` implementations to `HistoryBuffer`.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
41
src/de.rs
41
src/de.rs
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
binary_heap::Kind as BinaryHeapKind, BinaryHeap, Deque, IndexMap, IndexSet, LinearMap, String,
|
binary_heap::Kind as BinaryHeapKind, BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet,
|
||||||
Vec,
|
LinearMap, String, Vec,
|
||||||
};
|
};
|
||||||
use core::{
|
use core::{
|
||||||
fmt,
|
fmt,
|
||||||
@ -173,6 +173,43 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'de, T, const N: usize> Deserialize<'de> for HistoryBuffer<T, N>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
struct ValueVisitor<'de, T, const N: usize>(PhantomData<(&'de (), T)>);
|
||||||
|
|
||||||
|
impl<'de, T, const N: usize> serde::de::Visitor<'de> for ValueVisitor<'de, T, N>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
type Value = HistoryBuffer<T, N>;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
formatter.write_str("a sequence")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||||
|
where
|
||||||
|
A: SeqAccess<'de>,
|
||||||
|
{
|
||||||
|
let mut values = HistoryBuffer::new();
|
||||||
|
|
||||||
|
while let Some(value) = seq.next_element()? {
|
||||||
|
values.write(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(values)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deserializer.deserialize_seq(ValueVisitor(PhantomData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Dictionaries
|
// Dictionaries
|
||||||
|
|
||||||
impl<'de, K, V, S, const N: usize> Deserialize<'de> for IndexMap<K, V, BuildHasherDefault<S>, N>
|
impl<'de, K, V, S, const N: usize> Deserialize<'de> for IndexMap<K, V, BuildHasherDefault<S>, N>
|
||||||
|
18
src/ser.rs
18
src/ser.rs
@ -2,7 +2,7 @@ use core::hash::{BuildHasher, Hash};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
binary_heap::Kind as BinaryHeapKind, storage::Storage, vec::VecInner, BinaryHeap, Deque,
|
binary_heap::Kind as BinaryHeapKind, storage::Storage, vec::VecInner, BinaryHeap, Deque,
|
||||||
IndexMap, IndexSet, LinearMap, String,
|
HistoryBuffer, IndexMap, IndexSet, LinearMap, String,
|
||||||
};
|
};
|
||||||
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
|
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
|
||||||
|
|
||||||
@ -74,6 +74,22 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T, const N: usize> Serialize for HistoryBuffer<T, N>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
{
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut seq = serializer.serialize_seq(Some(self.len()))?;
|
||||||
|
for element in self.oldest_ordered() {
|
||||||
|
seq.serialize_element(element)?;
|
||||||
|
}
|
||||||
|
seq.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Dictionaries
|
// Dictionaries
|
||||||
|
|
||||||
impl<K, V, S, const N: usize> Serialize for IndexMap<K, V, S, N>
|
impl<K, V, S, const N: usize> Serialize for IndexMap<K, V, S, N>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user