Skip to content

Commit 1a4ba41

Browse files
committed
Backport all schemas from postcard-schema-ng to postcard-schema
1 parent d652bea commit 1a4ba41

7 files changed

Lines changed: 249 additions & 5 deletions

File tree

source/postcard-schema/Cargo.toml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ version = "0.33.0"
5858
optional = true
5959
default-features = false
6060

61+
[dependencies.fixed_v1_0]
62+
package = "fixed"
63+
version = "1.0"
64+
optional = true
65+
default-features = false
6166
[dependencies.postcard-derive]
6267
path = "../postcard-derive"
6368
version = "0.2.0"
@@ -68,6 +73,11 @@ package = "defmt"
6873
version = "0.3.5"
6974
optional = true
7075

76+
[dependencies.serde-big-array_v0_5]
77+
package = "serde-big-array"
78+
version = "0.5.1"
79+
optional = true
80+
7181
[dev-dependencies.postcard]
7282
path = "../postcard"
7383
version = "1.0"
@@ -85,9 +95,14 @@ use-std = ["serde/std"]
8595
alloc = ["serde/alloc"]
8696
derive = ["postcard-derive"]
8797

88-
defmt-v0_3 = ["defmt_v0_3"]
89-
uuid-v1_0 = ["uuid_v1_0"]
98+
core-net = []
99+
core-num-saturating = []
100+
90101
chrono-v0_4 = ["chrono_v0_4"]
102+
defmt-v0_3 = ["defmt_v0_3"]
103+
fixed-v1_0 = ["fixed_v1_0"]
91104
heapless-v0_7 = ["heapless_v0_7"]
92105
heapless-v0_8 = ["heapless_v0_8"]
93106
nalgebra-v0_33 = ["nalgebra_v0_33"]
107+
serde-big-array-v0_5 = ["serde-big-array_v0_5"]
108+
uuid-v1_0 = ["uuid_v1_0"]

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ use crate::{
77

88
extern crate alloc;
99

10+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
1011
impl<T: Schema> Schema for alloc::vec::Vec<T> {
1112
const SCHEMA: &'static NamedType = &NamedType {
1213
name: "Vec<T>",
1314
ty: &DataModelType::Seq(T::SCHEMA),
1415
};
1516
}
1617

18+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
1719
impl Schema for alloc::string::String {
1820
const SCHEMA: &'static NamedType = &NamedType {
1921
name: "String",
2022
ty: &DataModelType::String,
2123
};
2224
}
2325

26+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
2427
impl<K: Schema, V: Schema> Schema for alloc::collections::BTreeMap<K, V> {
2528
const SCHEMA: &'static NamedType = &NamedType {
2629
name: "BTreeMap<K, V>",
@@ -31,9 +34,26 @@ impl<K: Schema, V: Schema> Schema for alloc::collections::BTreeMap<K, V> {
3134
};
3235
}
3336

37+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
3438
impl<K: Schema> Schema for alloc::collections::BTreeSet<K> {
3539
const SCHEMA: &'static NamedType = &NamedType {
3640
name: "BTreeSet<K>",
3741
ty: &DataModelType::Seq(K::SCHEMA),
3842
};
3943
}
44+
45+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
46+
impl<T: Schema> Schema for alloc::boxed::Box<T> {
47+
const SCHEMA: &'static NamedType = &NamedType {
48+
name: "Box<T>",
49+
ty: T::SCHEMA.ty,
50+
};
51+
}
52+
53+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
54+
impl<T: ?Sized + Schema + alloc::borrow::ToOwned> Schema for alloc::borrow::Cow<'_, T> {
55+
const SCHEMA: &'static NamedType = &NamedType {
56+
name: "Cow<'_, T>",
57+
ty: T::SCHEMA.ty,
58+
};
59+
}

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

Lines changed: 133 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 {
@@ -155,3 +157,134 @@ impl<T: Schema> Schema for RangeTo<T> {
155157
}]),
156158
};
157159
}
160+
161+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
162+
impl Schema for core::net::Ipv4Addr {
163+
const SCHEMA: &'static NamedType = &NamedType {
164+
name: "Ipv4Addr",
165+
ty: &DataModelType::Struct(&[&NamedValue {
166+
name: "octets",
167+
ty: <[u8; 4]>::SCHEMA,
168+
}]),
169+
};
170+
}
171+
172+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
173+
impl Schema for core::net::Ipv6Addr {
174+
const SCHEMA: &'static NamedType = &NamedType {
175+
name: "Ipv6Addr",
176+
ty: &DataModelType::Struct(&[&NamedValue {
177+
name: "octets",
178+
ty: <[u8; 16]>::SCHEMA,
179+
}]),
180+
};
181+
}
182+
183+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
184+
impl Schema for core::net::IpAddr {
185+
const SCHEMA: &'static NamedType = &NamedType {
186+
name: "IpAddr",
187+
ty: &DataModelType::Enum(&[
188+
&NamedVariant {
189+
name: "V4",
190+
ty: &DataModelVariant::NewtypeVariant(core::net::Ipv4Addr::SCHEMA),
191+
},
192+
&NamedVariant {
193+
name: "V6",
194+
ty: &DataModelVariant::NewtypeVariant(core::net::Ipv6Addr::SCHEMA),
195+
},
196+
]),
197+
};
198+
}
199+
200+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
201+
impl Schema for core::net::SocketAddrV4 {
202+
const SCHEMA: &'static NamedType = &NamedType {
203+
name: "SocketAddrV4",
204+
ty: &DataModelType::Struct(&[
205+
&NamedValue {
206+
name: "ip",
207+
ty: core::net::Ipv4Addr::SCHEMA,
208+
},
209+
&NamedValue {
210+
name: "port",
211+
ty: u16::SCHEMA,
212+
},
213+
]),
214+
};
215+
}
216+
217+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
218+
impl Schema for core::net::SocketAddrV6 {
219+
const SCHEMA: &'static NamedType = &NamedType {
220+
name: "SocketAddrV6",
221+
ty: &DataModelType::Struct(&[
222+
&NamedValue {
223+
name: "ip",
224+
ty: core::net::Ipv6Addr::SCHEMA,
225+
},
226+
&NamedValue {
227+
name: "port",
228+
ty: u16::SCHEMA,
229+
},
230+
&NamedValue {
231+
name: "flowinfo",
232+
ty: u32::SCHEMA,
233+
},
234+
&NamedValue {
235+
name: "scope_id",
236+
ty: u32::SCHEMA,
237+
},
238+
]),
239+
};
240+
}
241+
242+
#[cfg_attr(docsrs, doc(cfg(feature = "core-net")))]
243+
impl Schema for core::net::SocketAddr {
244+
const SCHEMA: &'static NamedType = &NamedType {
245+
name: "SocketAddr",
246+
ty: &DataModelType::Enum(&[
247+
&NamedVariant {
248+
name: "V4",
249+
ty: &DataModelVariant::NewtypeVariant(core::net::SocketAddrV4::SCHEMA),
250+
},
251+
&NamedVariant {
252+
name: "V6",
253+
ty: &DataModelVariant::NewtypeVariant(core::net::SocketAddrV6::SCHEMA),
254+
},
255+
]),
256+
};
257+
}
258+
259+
impl<T: Schema> Schema for core::num::Wrapping<T> {
260+
const SCHEMA: &'static NamedType = T::SCHEMA;
261+
}
262+
263+
#[cfg(feature = "core-num-saturating")]
264+
#[cfg_attr(docsrs, doc(cfg(feature = "core-num-saturating")))]
265+
impl<T: Schema> Schema for core::num::Saturating<T> {
266+
const SCHEMA: &'static NamedType = T::SCHEMA;
267+
}
268+
269+
impl Schema for Duration {
270+
const SCHEMA: &'static NamedType = &NamedType {
271+
name: "Duration",
272+
ty: &DataModelType::Struct(&[
273+
&NamedValue {
274+
name: "secs",
275+
ty: u64::SCHEMA,
276+
},
277+
&NamedValue {
278+
name: "nanos",
279+
ty: u32::SCHEMA,
280+
},
281+
]),
282+
};
283+
}
284+
285+
impl<T: ?Sized> Schema for PhantomData<T> {
286+
const SCHEMA: &'static NamedType = &NamedType {
287+
name: "PhantomData",
288+
ty: &DataModelType::Unit,
289+
};
290+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ impl<K: Schema> Schema for std::collections::BTreeSet<K> {
6666
ty: &DataModelType::Seq(K::SCHEMA),
6767
};
6868
}
69+
70+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
71+
impl<T: Schema> Schema for std::boxed::Box<T> {
72+
const SCHEMA: &'static NamedType = T::SCHEMA;
73+
}
74+
75+
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
76+
impl<T: ?Sized + Schema + std::borrow::ToOwned> Schema for std::borrow::Cow<'_, T> {
77+
const SCHEMA: &'static NamedType = T::SCHEMA;
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Implementations of the [`Schema`] trait for the `fixed` crate v1.0
2+
3+
use crate::{schema::NamedType, Schema};
4+
5+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
6+
impl<Frac> Schema for fixed_v1_0::FixedI8<Frac> {
7+
const SCHEMA: &'static NamedType = i8::SCHEMA;
8+
}
9+
10+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
11+
impl<Frac> Schema for fixed_v1_0::FixedI16<Frac> {
12+
const SCHEMA: &'static NamedType = i16::SCHEMA;
13+
}
14+
15+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
16+
impl<Frac> Schema for fixed_v1_0::FixedI32<Frac> {
17+
const SCHEMA: &'static NamedType = i32::SCHEMA;
18+
}
19+
20+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
21+
impl<Frac> Schema for fixed_v1_0::FixedI64<Frac> {
22+
const SCHEMA: &'static NamedType = i64::SCHEMA;
23+
}
24+
25+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
26+
impl<Frac> Schema for fixed_v1_0::FixedI128<Frac> {
27+
const SCHEMA: &'static NamedType = i128::SCHEMA;
28+
}
29+
30+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
31+
impl<Frac> Schema for fixed_v1_0::FixedU8<Frac> {
32+
const SCHEMA: &'static NamedType = u8::SCHEMA;
33+
}
34+
35+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
36+
impl<Frac> Schema for fixed_v1_0::FixedU16<Frac> {
37+
const SCHEMA: &'static NamedType = u16::SCHEMA;
38+
}
39+
40+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
41+
impl<Frac> Schema for fixed_v1_0::FixedU32<Frac> {
42+
const SCHEMA: &'static NamedType = u32::SCHEMA;
43+
}
44+
45+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
46+
impl<Frac> Schema for fixed_v1_0::FixedU64<Frac> {
47+
const SCHEMA: &'static NamedType = u64::SCHEMA;
48+
}
49+
50+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
51+
impl<Frac> Schema for fixed_v1_0::FixedU128<Frac> {
52+
const SCHEMA: &'static NamedType = u128::SCHEMA;
53+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub mod builtins_std;
2121
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-v0_4")))]
2222
pub mod chrono_v0_4;
2323

24-
#[cfg(feature = "uuid-v1_0")]
25-
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-v1_0")))]
26-
pub mod uuid_v1_0;
24+
#[cfg(feature = "fixed-v1_0")]
25+
#[cfg_attr(docsrs, doc(cfg(feature = "fixed-v1_0")))]
26+
pub mod fixed_v1_0;
2727

2828
#[cfg(feature = "heapless-v0_7")]
2929
#[cfg_attr(docsrs, doc(cfg(feature = "heapless-v0_7")))]
@@ -37,6 +37,10 @@ pub mod heapless_v0_8;
3737
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
3838
pub mod nalgebra_v0_33;
3939

40+
#[cfg(feature = "serde-big-array-v0_5")]
41+
#[cfg_attr(docsrs, doc(cfg(feature = "serde-big-array-v0_5")))]
42+
pub mod serde_big_array_v0_5;
43+
4044
impl Schema for NamedType {
4145
const SCHEMA: &'static NamedType = &NamedType {
4246
name: "NamedType",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Implementations of the [`Schema`] trait for the `serde-big-array` crate v0.5.
2+
3+
use crate::{schema::NamedType, Schema};
4+
5+
use serde_big_array_v0_5::Array;
6+
7+
impl<T: Schema, const N: usize> Schema for Array<T, N> {
8+
const SCHEMA: &'static NamedType = <[T; N] as Schema>::SCHEMA;
9+
}

0 commit comments

Comments
 (0)