Skip to content

Commit

Permalink
Merge pull request #476 from Nikita240/hisbuf
Browse files Browse the repository at this point in the history
Add serde implementations to HistoryBuffer
  • Loading branch information
Dirbaio authored Jun 30, 2024
2 parents 8170f58 + 67d9d11 commit 310c09d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHANGELOG.md merge=union

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added pool implementations for 64-bit architectures.
- Added `IntoIterator` implementation for `LinearMap`
- Added `Deque::{get, get_mut, get_unchecked, get_unchecked_mut}`.
- Added `serde::Serialize` and `serde::Deserialize` implementations to `HistoryBuffer`.

### Changed

Expand Down
41 changes: 39 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
binary_heap::Kind as BinaryHeapKind, BinaryHeap, Deque, IndexMap, IndexSet, LinearMap, String,
Vec,
binary_heap::Kind as BinaryHeapKind, BinaryHeap, Deque, HistoryBuffer, IndexMap, IndexSet,
LinearMap, String, Vec,
};
use core::{
fmt,
Expand Down Expand Up @@ -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

impl<'de, K, V, S, const N: usize> Deserialize<'de> for IndexMap<K, V, BuildHasherDefault<S>, N>
Expand Down
18 changes: 17 additions & 1 deletion src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::hash::{BuildHasher, Hash};

use crate::{
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};

Expand Down Expand Up @@ -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

impl<K, V, S, const N: usize> Serialize for IndexMap<K, V, S, N>
Expand Down

0 comments on commit 310c09d

Please sign in to comment.