-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add serde #139
base: main
Are you sure you want to change the base?
Add serde #139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
use crate::ringbuffer_trait::{RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator}; | ||
use crate::RingBuffer; | ||
use core::iter::FromIterator; | ||
use core::mem::MaybeUninit; | ||
use core::mem::{self, ManuallyDrop}; | ||
use core::mem::{self, ManuallyDrop, MaybeUninit}; | ||
use core::ops::{Index, IndexMut}; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{de::MapAccess, ser::SerializeStruct}; | ||
|
||
/// The `ConstGenericRingBuffer` struct is a `RingBuffer` implementation which does not require `alloc` but | ||
/// uses const generics instead. | ||
/// | ||
|
@@ -40,6 +42,145 @@ pub struct ConstGenericRingBuffer<T, const CAP: usize> { | |
writeptr: usize, | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
impl<T, const CAP: usize> serde::Serialize for ConstGenericRingBuffer<T, CAP> | ||
where | ||
T: serde::Serialize, | ||
{ | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
// Create a temporary Vec to store the valid elements | ||
let mut elements = alloc::vec::Vec::with_capacity(CAP); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this alloc really necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since it's basically thrown away |
||
|
||
// Handle the case where the buffer might be empty | ||
if self.readptr != self.writeptr { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be better to just use the iterator here |
||
let mut read_idx = self.readptr; | ||
|
||
// If writeptr > readptr, elements are contiguous | ||
if self.writeptr > self.readptr { | ||
for idx in self.readptr..self.writeptr { | ||
unsafe { | ||
elements.push(&*self.buf[idx].as_ptr()); | ||
} | ||
} | ||
} else { | ||
// Handle wrapped around case | ||
// First read from readptr to end | ||
while read_idx < CAP { | ||
unsafe { | ||
elements.push(&*self.buf[read_idx].as_ptr()); | ||
} | ||
read_idx += 1; | ||
} | ||
// Then from start to writeptr | ||
read_idx = 0; | ||
while read_idx < self.writeptr { | ||
unsafe { | ||
elements.push(&*self.buf[read_idx].as_ptr()); | ||
} | ||
read_idx += 1; | ||
} | ||
} | ||
} | ||
|
||
// Serialize the elements along with the buffer metadata | ||
let mut state = serializer.serialize_struct("ConstGenericRingBuffer", 3)?; | ||
state.serialize_field("elements", &elements)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need to serialize the read and writeptr tbh. Those aren't useful in a serialization and I'd be very scared deserializing them. Since soundness depends on the state of these pointers, deserializing malformed data could be unsound. Semantically, a serialized ringbufer is just a collection of elements, which when deserialized can just be pushed into a new ringbuffer, which might end up with different states for readptr/writeptr |
||
state.serialize_field("readptr", &self.readptr)?; | ||
state.serialize_field("writeptr", &self.writeptr)?; | ||
state.end() | ||
} | ||
} | ||
#[cfg(feature = "serde")] | ||
impl<'de, T, const CAP: usize> serde::Deserialize<'de> for ConstGenericRingBuffer<T, CAP> | ||
where | ||
T: serde::Deserialize<'de>, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
struct RingBufferVisitor<T, const CAP: usize>(core::marker::PhantomData<T>); | ||
|
||
impl<'de, T, const CAP: usize> serde::de::Visitor<'de> for RingBufferVisitor<T, CAP> | ||
where | ||
T: serde::Deserialize<'de>, | ||
{ | ||
type Value = ConstGenericRingBuffer<T, CAP>; | ||
|
||
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { | ||
formatter.write_str("struct ConstGenericRingBuffer") | ||
} | ||
|
||
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error> | ||
where | ||
V: MapAccess<'de>, | ||
{ | ||
let mut elements: Option<alloc::vec::Vec<T>> = None; | ||
let mut readptr: Option<usize> = None; | ||
let mut writeptr: Option<usize> = None; | ||
|
||
while let Some(key) = map.next_key()? { | ||
match key { | ||
"elements" => { | ||
if elements.is_some() { | ||
return Err(serde::de::Error::duplicate_field("elements")); | ||
} | ||
elements = Some(map.next_value()?); | ||
} | ||
"readptr" => { | ||
if readptr.is_some() { | ||
return Err(serde::de::Error::duplicate_field("readptr")); | ||
} | ||
readptr = Some(map.next_value()?); | ||
} | ||
"writeptr" => { | ||
if writeptr.is_some() { | ||
return Err(serde::de::Error::duplicate_field("writeptr")); | ||
} | ||
writeptr = Some(map.next_value()?); | ||
} | ||
_ => { | ||
return Err(serde::de::Error::unknown_field( | ||
key, | ||
&["elements", "readptr", "writeptr"], | ||
)) | ||
} | ||
} | ||
} | ||
|
||
let elements = | ||
elements.ok_or_else(|| serde::de::Error::missing_field("elements"))?; | ||
let readptr = readptr.ok_or_else(|| serde::de::Error::missing_field("readptr"))?; | ||
let writeptr = | ||
writeptr.ok_or_else(|| serde::de::Error::missing_field("writeptr"))?; | ||
|
||
// Create a new ring buffer with uninitialized memory | ||
let mut buf: [MaybeUninit<T>; CAP] = unsafe { MaybeUninit::uninit().assume_init() }; | ||
|
||
// Initialize elements in the buffer | ||
for (idx, element) in elements.into_iter().enumerate() { | ||
buf[idx] = MaybeUninit::new(element); | ||
} | ||
|
||
Ok(ConstGenericRingBuffer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsound, when data is malformed. if readptr is maliciously (or accidentally) modified it'd read uninitialized elements |
||
buf, | ||
readptr, | ||
writeptr, | ||
}) | ||
} | ||
} | ||
|
||
deserializer.deserialize_struct( | ||
"ConstGenericRingBuffer", | ||
&["elements", "readptr", "writeptr"], | ||
RingBufferVisitor(core::marker::PhantomData), | ||
) | ||
} | ||
} | ||
|
||
impl<T, const CAP: usize> From<[T; CAP]> for ConstGenericRingBuffer<T, CAP> { | ||
fn from(value: [T; CAP]) -> Self { | ||
let v = ManuallyDrop::new(value); | ||
|
@@ -496,4 +637,19 @@ mod tests { | |
vec![1, 2, 3] | ||
); | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
#[test] | ||
fn serde() { | ||
let a: &[i32] = &[]; | ||
let b = ConstGenericRingBuffer::<i32, 3>::from(a); | ||
let c = serde_json::to_string(&b).unwrap(); | ||
let d = serde_json::from_str(&c).unwrap(); | ||
assert_eq!(b, d); | ||
let a: &[i32] = &[1, 2, 3]; | ||
let b = ConstGenericRingBuffer::<i32, 3>::from(a); | ||
let c = serde_json::to_string(&b).unwrap(); | ||
let d = serde_json::from_str(&c).unwrap(); | ||
assert_eq!(b, d); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is an iterator over the valid elements, with
self.iter()