Skip to content

Commit

Permalink
fix #[serde(default)]
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDertoni authored and patrickelectric committed May 3, 2023
1 parent ed96459 commit 5c70c2b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
6 changes: 5 additions & 1 deletion build/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,11 @@ impl MavMessage {
// If sent by an implementation that doesn't have the extensions fields
// then the recipient will see zero values for the extensions fields.
let serde_default = if field.is_extension {
quote!(#[cfg_attr(feature = "serde", serde(default))])
if field.enumtype.is_some() {
quote!(#[cfg_attr(feature = "serde", serde(default))])
} else {
quote!(#[cfg_attr(feature = "serde", serde(default = "crate::RustDefault::rust_default"))])
}
} else {
quote!()
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use self::connection::{connect, MavConnection};

mod utils;
#[allow(unused_imports)]
use utils::remove_trailing_zeroes;
use utils::{remove_trailing_zeroes, RustDefault};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down
92 changes: 92 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,95 @@ pub(crate) fn remove_trailing_zeroes(data: &mut [u8]) -> usize {

len
}

/// A trait very similar to `Default` but is only implemented for the equivalent Rust types to
/// `MavType`s. This is only needed because rust doesn't currently implement `Default` for arrays
/// of all sizes. In particular this trait is only ever used when the "serde" feature is enabled.
pub(crate) trait RustDefault: Copy {
fn rust_default() -> Self;
}

impl<T: RustDefault, const N: usize> RustDefault for [T; N] {
#[inline(always)]
fn rust_default() -> Self {
let val: T = RustDefault::rust_default();
[val; N]
}
}

impl RustDefault for u8 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for i8 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for u16 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for i16 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for u32 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for i32 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for u64 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for i64 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}

impl RustDefault for char {
#[inline(always)]
fn rust_default() -> Self {
'\0'
}
}

impl RustDefault for f32 {
#[inline(always)]
fn rust_default() -> Self {
0.0
}
}

impl RustDefault for f64 {
#[inline(always)]
fn rust_default() -> Self {
0.0
}
}

0 comments on commit 5c70c2b

Please sign in to comment.