Skip to content
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 support for skipped fields #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions source/postcard/src/ser/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ where
value.serialize(&mut **self)
}

#[inline]
fn skip_field(&mut self, _key: &'static str) -> Result<()> {
None::<()>.serialize(&mut **self)
}

#[inline]
fn end(self) -> Result<()> {
Ok(())
Expand All @@ -541,6 +546,11 @@ where
value.serialize(&mut **self)
}

#[inline]
fn skip_field(&mut self, _key: &'static str) -> Result<()> {
None::<()>.serialize(&mut **self)
}

#[inline]
fn end(self) -> Result<()> {
Ok(())
Expand Down
109 changes: 109 additions & 0 deletions source/postcard/tests/skiped_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::marker::PhantomData;

#[cfg(feature = "heapless")]
use heapless::Vec;

use postcard::from_bytes;
#[cfg(feature = "heapless")]
use postcard::to_vec;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct A {
a: u8,
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<u8>,
c: u8,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum E {
A(A),
B {
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<u64>,
},
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct C {
#[serde(skip)]
ghost: u8,
phantom: PhantomData<u16>,
}

#[test]
#[cfg(feature = "heapless")]
fn complete() {
let a = A {
a: 0xA,
b: Some(0xB),
c: 0xC,
};

let bytes: Vec<u8, 42> = to_vec(&a).expect("unable to serialize");

let restored: A = from_bytes(&bytes).expect("unable to deserialize");

assert_eq!(a, restored);
}

#[test]
#[cfg(feature = "heapless")]
fn partial_struct() {
let a = A {
a: 0xA,
b: None,
c: 0xC,
};

let bytes: Vec<u8, 42> = to_vec(&a).expect("unable to serialize");

let restored: A = from_bytes(&bytes).expect("unable to deserialize");

assert_eq!(a, restored);
}

#[test]
#[cfg(feature = "heapless")]
fn partial_enum_a() {
let e = E::A(A {
a: 0xA,
b: None,
c: 0xC,
});

let bytes: Vec<u8, 42> = to_vec(&e).expect("unable to serialize");

let restored: E = from_bytes(&bytes).expect("unable to deserialize");

assert_eq!(e, restored);
}

#[test]
#[cfg(feature = "heapless")]
fn partial_enum_b() {
let e = E::B { b: None };

let bytes: Vec<u8, 42> = to_vec(&e).expect("unable to serialize");

let restored: E = from_bytes(&bytes).expect("unable to deserialize");

assert_eq!(e, restored);
}

#[test]
#[cfg(feature = "heapless")]
fn empty_struct_unconditional() {
let c = C::default();

let bytes: Vec<u8, 42> = to_vec(&c).expect("unable to serialize");

assert_eq!(0, bytes.len());
//assert!(false, "{:?}", bytes);

let restored: C = from_bytes(&bytes).expect("unable to deserialize");

assert_eq!(c, restored);
}