Skip to content

Commit a09fc15

Browse files
committed
Implement Schema and MaxSize for more core types
- core::time::Duration - core::marker::PhantomData - core::num::{Wrapping,Saturating} - Added feature flags for Saturating since it's relatively new (1.74)
1 parent 16863c4 commit a09fc15

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

source/postcard-schema/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ alloc = ["serde/alloc"]
8686
derive = ["postcard-derive"]
8787

8888
core-net = []
89+
core-num-saturating = []
8990

9091
defmt-v0_3 = ["defmt_v0_3"]
9192
uuid-v1_0 = ["uuid_v1_0"]

source/postcard-schema/src/impls/builtins_nostd.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use crate::{
77
Schema,
88
};
99
use core::{
10+
marker::PhantomData,
1011
num::{
1112
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
1213
NonZeroU32, NonZeroU64, NonZeroU8,
1314
},
1415
ops::{Range, RangeFrom, RangeInclusive, RangeTo},
16+
time::Duration,
1517
};
1618

1719
macro_rules! impl_schema {
@@ -240,3 +242,36 @@ impl Schema for core::net::SocketAddr {
240242
],
241243
};
242244
}
245+
246+
impl<T: Schema> Schema for core::num::Wrapping<T> {
247+
const SCHEMA: &'static DataModelType = T::SCHEMA;
248+
}
249+
250+
#[cfg(feature = "core-num-saturating")]
251+
#[cfg_attr(docsrs, doc(cfg(feature = "core-num-saturating")))]
252+
impl<T: Schema> Schema for core::num::Saturating<T> {
253+
const SCHEMA: &'static DataModelType = T::SCHEMA;
254+
}
255+
256+
impl Schema for Duration {
257+
const SCHEMA: &'static DataModelType = &DataModelType::Struct {
258+
name: "Duration",
259+
data: Data::Struct(&[
260+
&NamedField {
261+
name: "secs",
262+
ty: u64::SCHEMA,
263+
},
264+
&NamedField {
265+
name: "nanos",
266+
ty: u32::SCHEMA,
267+
},
268+
]),
269+
};
270+
}
271+
272+
impl<T: ?Sized> Schema for PhantomData<T> {
273+
const SCHEMA: &'static DataModelType = &DataModelType::Struct {
274+
name: "PhantomData",
275+
data: Data::Unit,
276+
};
277+
}

source/postcard/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ use-crc = ["crc"]
8282
#
8383
# NOT subject to SemVer guarantees!
8484
experimental-derive = ["postcard-derive"]
85+
experimental-derive-core-num-saturating = []
8586
crc = ["dep:crc"]
8687
defmt = ["dep:defmt"]
8788
heapless = ["dep:heapless"]

source/postcard/src/max_size.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::{
1515
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
1616
},
1717
ops::{Range, RangeFrom, RangeInclusive, RangeTo},
18+
time::Duration,
1819
};
1920

2021
/// This trait is used to enforce the maximum size required to
@@ -161,6 +162,10 @@ impl MaxSize for NonZeroUsize {
161162
const POSTCARD_MAX_SIZE: usize = usize::POSTCARD_MAX_SIZE;
162163
}
163164

165+
impl MaxSize for Duration {
166+
const POSTCARD_MAX_SIZE: usize = u64::POSTCARD_MAX_SIZE + u32::POSTCARD_MAX_SIZE;
167+
}
168+
164169
impl<T> MaxSize for PhantomData<T> {
165170
const POSTCARD_MAX_SIZE: usize = 0;
166171
}
@@ -218,6 +223,16 @@ impl<T: MaxSize> MaxSize for RangeTo<T> {
218223
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
219224
}
220225

226+
impl<T: MaxSize> MaxSize for core::num::Wrapping<T> {
227+
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
228+
}
229+
230+
#[cfg(feature = "experimental-derive-core-num-saturating")]
231+
#[cfg_attr(docsrs, doc(cfg(feature = "experimental-derive-core-num-saturating")))]
232+
impl<T: MaxSize> MaxSize for core::num::Saturating<T> {
233+
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
234+
}
235+
221236
#[cfg(feature = "alloc")]
222237
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
223238
impl<T: MaxSize> MaxSize for Box<T> {

0 commit comments

Comments
 (0)