Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,22 +527,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.deserialize_seq(visitor)
}

/// Unsupported. Can’t make an arbitrary-sized map in no-std. Use a struct with a
/// known format, or implement a custom map deserializer / visitor:
/// https://serde.rs/deserialize-map.html
fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
unreachable!()
}

fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
Expand All @@ -561,6 +546,18 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
}
}

fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.deserialize_map(visitor)
}

fn deserialize_enum<V>(
self,
_name: &'static str,
Expand Down
56 changes: 56 additions & 0 deletions src/ser/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use serde::ser;

use heapless::ArrayLength;

use crate::ser::{Error, Result, Serializer};

pub struct SerializeMap<'a, B>
where
B: ArrayLength<u8>,
{
ser: &'a mut Serializer<B>,
first: bool,
}

impl<'a, B> SerializeMap<'a, B>
where
B: ArrayLength<u8>,
{
pub(crate) fn new(ser: &'a mut Serializer<B>) -> Self {
SerializeMap { ser, first: true }
}
}

impl<'a, B> ser::SerializeMap for SerializeMap<'a, B>
where
B: ArrayLength<u8>,
{
type Ok = ();
type Error = Error;

fn end(self) -> Result<Self::Ok> {
self.ser.buf.push(b'}')?;
Ok(())
}

fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()>
where
T: ser::Serialize,
{
if !self.first {
self.ser.buf.push(b',')?;
}
self.first = false;
key.serialize(&mut *self.ser)?;
self.ser.buf.extend_from_slice(b":")?;
Ok(())
}

fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: ser::Serialize,
{
value.serialize(&mut *self.ser)?;
Ok(())
}
}
8 changes: 6 additions & 2 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use serde::ser;

use heapless::{consts::*, String, Vec};

use self::map::SerializeMap;
use self::seq::SerializeSeq;
use self::struct_::SerializeStruct;

mod map;
mod seq;
mod struct_;

Expand Down Expand Up @@ -142,7 +144,7 @@ where
type SerializeTuple = SerializeSeq<'a, B>;
type SerializeTupleStruct = Unreachable;
type SerializeTupleVariant = Unreachable;
type SerializeMap = Unreachable;
type SerializeMap = SerializeMap<'a, B>;
type SerializeStruct = SerializeStruct<'a, B>;
type SerializeStructVariant = Unreachable;

Expand Down Expand Up @@ -301,7 +303,9 @@ where
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
unreachable!()
self.buf.push(b'{')?;

Ok(SerializeMap::new(self))
}

fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
Expand Down
18 changes: 9 additions & 9 deletions src/ser/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ pub struct SerializeStruct<'a, B>
where
B: ArrayLength<u8>,
{
de: &'a mut Serializer<B>,
ser: &'a mut Serializer<B>,
first: bool,
}

impl<'a, B> SerializeStruct<'a, B>
where
B: ArrayLength<u8>,
{
pub(crate) fn new(de: &'a mut Serializer<B>) -> Self {
SerializeStruct { de, first: true }
pub(crate) fn new(ser: &'a mut Serializer<B>) -> Self {
SerializeStruct { ser, first: true }
}
}

Expand All @@ -34,21 +34,21 @@ where
{
// XXX if `value` is `None` we not produce any output for this field
if !self.first {
self.de.buf.push(b',')?;
self.ser.buf.push(b',')?;
}
self.first = false;

self.de.buf.push(b'"')?;
self.de.buf.extend_from_slice(key.as_bytes())?;
self.de.buf.extend_from_slice(b"\":")?;
self.ser.buf.push(b'"')?;
self.ser.buf.extend_from_slice(key.as_bytes())?;
self.ser.buf.extend_from_slice(b"\":")?;

value.serialize(&mut *self.de)?;
value.serialize(&mut *self.ser)?;

Ok(())
}

fn end(self) -> Result<Self::Ok> {
self.de.buf.push(b'}')?;
self.ser.buf.push(b'}')?;
Ok(())
}
}