diff --git a/source/postcard-schema/Cargo.toml b/source/postcard-schema/Cargo.toml index 1a48c592..c8a65abd 100644 --- a/source/postcard-schema/Cargo.toml +++ b/source/postcard-schema/Cargo.toml @@ -86,6 +86,7 @@ alloc = ["serde/alloc"] derive = ["postcard-derive"] core-net = [] +core-num-saturating = [] defmt-v0_3 = ["defmt_v0_3"] uuid-v1_0 = ["uuid_v1_0"] diff --git a/source/postcard-schema/src/impls/builtins_nostd.rs b/source/postcard-schema/src/impls/builtins_nostd.rs index 2d26be65..7deb12da 100644 --- a/source/postcard-schema/src/impls/builtins_nostd.rs +++ b/source/postcard-schema/src/impls/builtins_nostd.rs @@ -7,11 +7,13 @@ use crate::{ Schema, }; use core::{ + marker::PhantomData, num::{ NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, }, ops::{Range, RangeFrom, RangeInclusive, RangeTo}, + time::Duration, }; macro_rules! impl_schema { @@ -240,3 +242,36 @@ impl Schema for core::net::SocketAddr { ], }; } + +impl Schema for core::num::Wrapping { + const SCHEMA: &'static DataModelType = T::SCHEMA; +} + +#[cfg(feature = "core-num-saturating")] +#[cfg_attr(docsrs, doc(cfg(feature = "core-num-saturating")))] +impl Schema for core::num::Saturating { + const SCHEMA: &'static DataModelType = T::SCHEMA; +} + +impl Schema for Duration { + const SCHEMA: &'static DataModelType = &DataModelType::Struct { + name: "Duration", + data: Data::Struct(&[ + &NamedField { + name: "secs", + ty: u64::SCHEMA, + }, + &NamedField { + name: "nanos", + ty: u32::SCHEMA, + }, + ]), + }; +} + +impl Schema for PhantomData { + const SCHEMA: &'static DataModelType = &DataModelType::Struct { + name: "PhantomData", + data: Data::Unit, + }; +} diff --git a/source/postcard/Cargo.toml b/source/postcard/Cargo.toml index 729afafa..26e48b01 100644 --- a/source/postcard/Cargo.toml +++ b/source/postcard/Cargo.toml @@ -82,6 +82,7 @@ use-crc = ["crc"] # # NOT subject to SemVer guarantees! experimental-derive = ["postcard-derive"] +core-num-saturating = [] crc = ["dep:crc"] defmt = ["dep:defmt"] heapless = ["dep:heapless"] diff --git a/source/postcard/src/max_size.rs b/source/postcard/src/max_size.rs index 361e08c7..3f93e16f 100644 --- a/source/postcard/src/max_size.rs +++ b/source/postcard/src/max_size.rs @@ -15,6 +15,7 @@ use core::{ NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, }, ops::{Range, RangeFrom, RangeInclusive, RangeTo}, + time::Duration, }; /// This trait is used to enforce the maximum size required to @@ -161,6 +162,10 @@ impl MaxSize for NonZeroUsize { const POSTCARD_MAX_SIZE: usize = usize::POSTCARD_MAX_SIZE; } +impl MaxSize for Duration { + const POSTCARD_MAX_SIZE: usize = u64::POSTCARD_MAX_SIZE + u32::POSTCARD_MAX_SIZE; +} + impl MaxSize for PhantomData { const POSTCARD_MAX_SIZE: usize = 0; } @@ -218,6 +223,16 @@ impl MaxSize for RangeTo { const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE; } +impl MaxSize for core::num::Wrapping { + const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE; +} + +#[cfg(all(feature = "core-num-saturating", feature = "experimental-derive"))] +#[cfg_attr(docsrs, doc(cfg(feature = "core-num-saturating")))] +impl MaxSize for core::num::Saturating { + const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE; +} + #[cfg(feature = "alloc")] #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl MaxSize for Box {