Skip to content

Commit eddb280

Browse files
committed
Improve nalgebra postcard-schema implementations
* Add nalgebra v0.34 support to postcard-schema-ng * Add support for `nalgebra::Complex<T>`, which comes from `num-complex` * Support matrices with storage other than `ArrayStorage` * Add support for `nalgebra::Point<T, D>`, `nalgebra::Translation<T, D>`, and `nalgebra::Isometry<T, R, D>`
1 parent bee7f0f commit eddb280

11 files changed

Lines changed: 331 additions & 26 deletions

File tree

ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cargo_test() {
2929
cargo test --all "$@"
3030
}
3131

32-
cargo_test --features=alloc,experimental-derive,use-std,use-crc,derive,nalgebra-v0_33,heapless-v0_8,heapless-v0_9,embedded-io-v0_7
32+
cargo_test --features=alloc,experimental-derive,use-std,use-crc,derive,nalgebra-v0_33,nalgebra-v0_34,num-complex-v0_4,heapless-v0_8,heapless-v0_9,embedded-io-v0_7
3333

3434
# NOTE: we exclude postcard-dyn for these checks because it is std-only
3535

source/postcard-schema-ng/Cargo.toml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ version = "0.33.0"
6464
optional = true
6565
default-features = false
6666

67+
[dependencies.nalgebra_v0_34]
68+
package = "nalgebra"
69+
version = "0.34.0"
70+
optional = true
71+
default-features = false
72+
73+
[dependencies.num_complex_v0_4]
74+
package = "num-complex"
75+
version = "0.4"
76+
optional = true
77+
default-features = false
78+
6779
[dependencies.fixed_v1_0]
6880
package = "fixed"
6981
version = "1.0"
@@ -96,6 +108,18 @@ version = "0.33.0"
96108
default-features = false
97109
features = ["serde-serialize-no-std"]
98110

111+
[dev-dependencies.nalgebra_v0_34]
112+
package = "nalgebra"
113+
version = "0.34.0"
114+
default-features = false
115+
features = ["serde-serialize-no-std"]
116+
117+
[dev-dependencies.num_complex_v0_4]
118+
package = "num-complex"
119+
version = "0.4"
120+
default-features = false
121+
features = ["serde"]
122+
99123
[features]
100124
default = []
101125
use-std = ["serde/std"]
@@ -111,6 +135,8 @@ fixed-v1_0 = ["fixed_v1_0"]
111135
heapless-v0_7 = ["heapless_v0_7"]
112136
heapless-v0_8 = ["heapless_v0_8"]
113137
heapless-v0_9 = ["heapless_v0_9"]
114-
nalgebra-v0_33 = ["nalgebra_v0_33"]
138+
nalgebra-v0_33 = ["nalgebra_v0_33", "num-complex-v0_4"]
139+
nalgebra-v0_34 = ["nalgebra_v0_34", "num-complex-v0_4"]
140+
num-complex-v0_4 = ["num_complex_v0_4"]
115141
serde-big-array-v0_5 = ["serde-big-array_v0_5"]
116142
uuid-v1_0 = ["uuid_v1_0"]

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ pub mod heapless_v0_9;
3838
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
3939
pub mod nalgebra_v0_33;
4040

41+
#[cfg(feature = "nalgebra-v0_34")]
42+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
43+
pub mod nalgebra_v0_34;
44+
45+
#[cfg(feature = "num-complex-v0_4")]
46+
#[cfg_attr(
47+
docsrs,
48+
doc(cfg(any(
49+
feature = "num-complex-v0_4",
50+
feature = "nalgebra-v0_33",
51+
feature = "nalgebra-v0_34"
52+
)))
53+
)]
54+
pub mod num_complex_v0_4;
55+
4156
#[cfg(feature = "serde-big-array-v0_5")]
4257
#[cfg_attr(docsrs, doc(cfg(feature = "serde-big-array-v0_5")))]
4358
pub mod serde_big_array_v0_5;

source/postcard-schema-ng/src/impls/nalgebra_v0_33.rs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
//! Implementations of the [`Schema`] trait for the `nalgebra` crate v0.33
22
3-
use crate::{schema::DataModelType, Schema};
3+
use crate::{
4+
schema::{Data, DataModelType, NamedField},
5+
Schema,
6+
};
47

58
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
6-
impl<T, const R: usize, const C: usize> Schema
7-
for nalgebra_v0_33::Matrix<
9+
impl<T, const R: usize, const C: usize, S> Schema
10+
for nalgebra_v0_33::Matrix<T, nalgebra_v0_33::Const<R>, nalgebra_v0_33::Const<C>, S>
11+
where
12+
T: Schema + nalgebra_v0_33::Scalar,
13+
S: nalgebra_v0_33::base::storage::Storage<
814
T,
915
nalgebra_v0_33::Const<R>,
1016
nalgebra_v0_33::Const<C>,
11-
nalgebra_v0_33::ArrayStorage<T, R, C>,
12-
>
13-
where
14-
T: Schema + nalgebra_v0_33::Scalar,
17+
>,
1518
{
1619
const SCHEMA: &'static DataModelType = &DataModelType::Tuple(flatten(&[[T::SCHEMA; R]; C]));
1720
}
1821

22+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
23+
impl<T: Schema + nalgebra_v0_33::Scalar, const D: usize> Schema
24+
for nalgebra_v0_33::OPoint<T, nalgebra_v0_33::Const<D>>
25+
where
26+
nalgebra_v0_33::base::default_allocator::DefaultAllocator:
27+
nalgebra_v0_33::base::allocator::Allocator<nalgebra_v0_33::Const<D>>,
28+
{
29+
const SCHEMA: &'static DataModelType =
30+
nalgebra_v0_33::OVector::<T, nalgebra_v0_33::Const<D>>::SCHEMA;
31+
}
32+
1933
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
2034
impl<T: Schema> Schema for nalgebra_v0_33::Unit<T> {
2135
const SCHEMA: &'static DataModelType = T::SCHEMA;
@@ -26,6 +40,32 @@ impl<T: Schema + nalgebra_v0_33::Scalar> Schema for nalgebra_v0_33::Quaternion<T
2640
const SCHEMA: &'static DataModelType = nalgebra_v0_33::Vector4::<T>::SCHEMA;
2741
}
2842

43+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
44+
impl<T: Schema + nalgebra_v0_33::Scalar, const D: usize> Schema
45+
for nalgebra_v0_33::Translation<T, D>
46+
{
47+
const SCHEMA: &'static DataModelType = nalgebra_v0_33::SVector::<T, D>::SCHEMA;
48+
}
49+
50+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
51+
impl<T: Schema + nalgebra_v0_33::Scalar, R: Schema, const D: usize> Schema
52+
for nalgebra_v0_33::Isometry<T, R, D>
53+
{
54+
const SCHEMA: &'static DataModelType = &DataModelType::Struct {
55+
name: "Isometry",
56+
data: Data::Struct(&[
57+
&NamedField {
58+
name: "rotation",
59+
ty: R::SCHEMA,
60+
},
61+
&NamedField {
62+
name: "translation",
63+
ty: nalgebra_v0_33::Translation::<T, D>::SCHEMA,
64+
},
65+
]),
66+
};
67+
}
68+
2969
/// Const version of the const-unstable [`<[[T; N]]>::as_flattened()`]
3070
const fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] {
3171
const {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! Implementations of the [`Schema`] trait for the `nalgebra` crate v0.34
2+
3+
use crate::{
4+
schema::{Data, DataModelType, NamedField},
5+
Schema,
6+
};
7+
8+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
9+
impl<T, const R: usize, const C: usize, S> Schema
10+
for nalgebra_v0_34::Matrix<T, nalgebra_v0_34::Const<R>, nalgebra_v0_34::Const<C>, S>
11+
where
12+
T: Schema + nalgebra_v0_34::Scalar,
13+
S: nalgebra_v0_34::base::storage::Storage<
14+
T,
15+
nalgebra_v0_34::Const<R>,
16+
nalgebra_v0_34::Const<C>,
17+
>,
18+
{
19+
const SCHEMA: &'static DataModelType = &DataModelType::Tuple(flatten(&[[T::SCHEMA; R]; C]));
20+
}
21+
22+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
23+
impl<T: Schema + nalgebra_v0_34::Scalar, const D: usize> Schema
24+
for nalgebra_v0_34::OPoint<T, nalgebra_v0_34::Const<D>>
25+
where
26+
nalgebra_v0_34::base::default_allocator::DefaultAllocator:
27+
nalgebra_v0_34::base::allocator::Allocator<nalgebra_v0_34::Const<D>>,
28+
{
29+
const SCHEMA: &'static DataModelType =
30+
nalgebra_v0_34::OVector::<T, nalgebra_v0_34::Const<D>>::SCHEMA;
31+
}
32+
33+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
34+
impl<T: Schema> Schema for nalgebra_v0_34::Unit<T> {
35+
const SCHEMA: &'static DataModelType = T::SCHEMA;
36+
}
37+
38+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
39+
impl<T: Schema + nalgebra_v0_34::Scalar> Schema for nalgebra_v0_34::Quaternion<T> {
40+
const SCHEMA: &'static DataModelType = nalgebra_v0_34::Vector4::<T>::SCHEMA;
41+
}
42+
43+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
44+
impl<T: Schema + nalgebra_v0_34::Scalar, const D: usize> Schema
45+
for nalgebra_v0_34::Translation<T, D>
46+
{
47+
const SCHEMA: &'static DataModelType = nalgebra_v0_34::SVector::<T, D>::SCHEMA;
48+
}
49+
50+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
51+
impl<T: Schema + nalgebra_v0_34::Scalar, R: Schema, const D: usize> Schema
52+
for nalgebra_v0_34::Isometry<T, R, D>
53+
{
54+
const SCHEMA: &'static DataModelType = &DataModelType::Struct {
55+
name: "Isometry",
56+
data: Data::Struct(&[
57+
&NamedField {
58+
name: "rotation",
59+
ty: R::SCHEMA,
60+
},
61+
&NamedField {
62+
name: "translation",
63+
ty: nalgebra_v0_34::Translation::<T, D>::SCHEMA,
64+
},
65+
]),
66+
};
67+
}
68+
69+
/// Const version of the const-unstable [`<[[T; N]]>::as_flattened()`]
70+
const fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] {
71+
const {
72+
assert!(size_of::<T>() != 0);
73+
}
74+
// SAFETY: `self.len() * N` cannot overflow because `self` is
75+
// already in the address space.
76+
let len = unsafe { slice.len().unchecked_mul(N) };
77+
// SAFETY: `[T]` is layout-identical to `[T; N]`
78+
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), len) }
79+
}
80+
81+
#[test]
82+
fn flattened() {
83+
type T = nalgebra_v0_34::SMatrix<u8, 3, 3>;
84+
assert_eq!(T::SCHEMA, <[u8; 9]>::SCHEMA);
85+
}
86+
87+
#[test]
88+
fn smoke() {
89+
let x = nalgebra_v0_34::SMatrix::<u8, 3, 3>::new(1, 2, 3, 4, 5, 6, 7, 8, 9);
90+
let y = postcard::to_stdvec(&x).unwrap();
91+
assert_eq!(&[1, 4, 7, 2, 5, 8, 3, 6, 9], y.as_slice());
92+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Implementations of the [`Schema`] trait for the `num-complex` crate v0.4
2+
3+
use crate::{schema::DataModelType, Schema};
4+
5+
#[cfg_attr(
6+
docsrs,
7+
doc(cfg(any(
8+
feature = "num-complex-v0_4",
9+
feature = "nalgebra-v0_33",
10+
feature = "nalgebra-v0_34"
11+
)))
12+
)]
13+
impl<T: Schema> Schema for num_complex_v0_4::Complex<T> {
14+
const SCHEMA: &'static DataModelType = <[T; 2]>::SCHEMA;
15+
}

source/postcard-schema/Cargo.toml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ version = "0.34.1"
7070
optional = true
7171
default-features = false
7272

73+
[dependencies.num_complex_v0_4]
74+
package = "num-complex"
75+
version = "0.4"
76+
optional = true
77+
default-features = false
78+
7379
[dependencies.fixed_v1_0]
7480
package = "fixed"
7581
version = "1.0"
@@ -107,6 +113,12 @@ version = "0.34.1"
107113
default-features = false
108114
features = ["serde-serialize-no-std"]
109115

116+
[dev-dependencies.num_complex_v0_4]
117+
package = "num-complex"
118+
version = "0.4"
119+
default-features = false
120+
features = ["serde"]
121+
110122
[features]
111123
default = []
112124
use-std = ["serde/std"]
@@ -122,7 +134,8 @@ fixed-v1_0 = ["fixed_v1_0"]
122134
heapless-v0_7 = ["heapless_v0_7"]
123135
heapless-v0_8 = ["heapless_v0_8"]
124136
heapless-v0_9 = ["heapless_v0_9"]
125-
nalgebra-v0_33 = ["nalgebra_v0_33"]
126-
nalgebra-v0_34 = ["nalgebra_v0_34"]
137+
nalgebra-v0_33 = ["nalgebra_v0_33", "num-complex-v0_4"]
138+
nalgebra-v0_34 = ["nalgebra_v0_34", "num-complex-v0_4"]
139+
num-complex-v0_4 = ["num_complex_v0_4"]
127140
serde-big-array-v0_5 = ["serde-big-array_v0_5"]
128141
uuid-v1_0 = ["uuid_v1_0"]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ pub mod nalgebra_v0_33;
4545
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))]
4646
pub mod nalgebra_v0_34;
4747

48+
#[cfg(any(
49+
feature = "num-complex-v0_4",
50+
feature = "nalgebra-v0_33",
51+
feature = "nalgebra-v0_34"
52+
))]
53+
#[cfg_attr(docsrs, doc(cfg(feature = "num-complex-v0_4")))]
54+
pub mod num_complex_v0_4;
55+
4856
#[cfg(feature = "serde-big-array-v0_5")]
4957
#[cfg_attr(docsrs, doc(cfg(feature = "serde-big-array-v0_5")))]
5058
pub mod serde_big_array_v0_5;

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
//! Implementations of the [`Schema`] trait for the `nalgebra` crate v0.33
22
33
use crate::{
4-
schema::{DataModelType, NamedType},
4+
schema::{DataModelType, NamedType, NamedValue},
55
Schema,
66
};
77

88
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
9-
impl<T, const R: usize, const C: usize> Schema
10-
for nalgebra_v0_33::Matrix<
9+
impl<T, const R: usize, const C: usize, S> Schema
10+
for nalgebra_v0_33::Matrix<T, nalgebra_v0_33::Const<R>, nalgebra_v0_33::Const<C>, S>
11+
where
12+
T: Schema + nalgebra_v0_33::Scalar,
13+
S: nalgebra_v0_33::base::storage::Storage<
1114
T,
1215
nalgebra_v0_33::Const<R>,
1316
nalgebra_v0_33::Const<C>,
14-
nalgebra_v0_33::ArrayStorage<T, R, C>,
15-
>
16-
where
17-
T: Schema + nalgebra_v0_33::Scalar,
17+
>,
1818
{
1919
const SCHEMA: &'static NamedType = &NamedType {
20-
name: "nalgebra::Matrix<T, R, C, ArrayStorage<T, R, C>>",
20+
name: "nalgebra::Matrix<T, R, C, S>",
2121
ty: &DataModelType::Tuple(flatten(&[[T::SCHEMA; R]; C])),
2222
};
2323
}
2424

25+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
26+
impl<T: Schema + nalgebra_v0_33::Scalar, const D: usize> Schema
27+
for nalgebra_v0_33::OPoint<T, nalgebra_v0_33::Const<D>>
28+
where
29+
nalgebra_v0_33::base::default_allocator::DefaultAllocator:
30+
nalgebra_v0_33::base::allocator::Allocator<nalgebra_v0_33::Const<D>>,
31+
{
32+
const SCHEMA: &'static NamedType = &NamedType {
33+
name: "nalgebra::OPoint<T, D>",
34+
ty: nalgebra_v0_33::OVector::<T, nalgebra_v0_33::Const<D>>::SCHEMA.ty,
35+
};
36+
}
37+
2538
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
2639
impl<T: Schema> Schema for nalgebra_v0_33::Unit<T> {
2740
const SCHEMA: &'static NamedType = T::SCHEMA;
@@ -32,6 +45,32 @@ impl<T: Schema + nalgebra_v0_33::Scalar> Schema for nalgebra_v0_33::Quaternion<T
3245
const SCHEMA: &'static NamedType = nalgebra_v0_33::Vector4::<T>::SCHEMA;
3346
}
3447

48+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
49+
impl<T: Schema + nalgebra_v0_33::Scalar, const D: usize> Schema
50+
for nalgebra_v0_33::Translation<T, D>
51+
{
52+
const SCHEMA: &'static NamedType = nalgebra_v0_33::SVector::<T, D>::SCHEMA;
53+
}
54+
55+
#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_33")))]
56+
impl<T: Schema + nalgebra_v0_33::Scalar, R: Schema, const D: usize> Schema
57+
for nalgebra_v0_33::Isometry<T, R, D>
58+
{
59+
const SCHEMA: &'static NamedType = &NamedType {
60+
name: "nalgebra::Isometry<T, R, D>",
61+
ty: &DataModelType::Struct(&[
62+
&NamedValue {
63+
name: "rotation",
64+
ty: R::SCHEMA,
65+
},
66+
&NamedValue {
67+
name: "translation",
68+
ty: nalgebra_v0_33::Translation::<T, D>::SCHEMA,
69+
},
70+
]),
71+
};
72+
}
73+
3574
/// Const version of the const-unstable [`<[[T; N]]>::as_flattened()`]
3675
const fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] {
3776
const {

0 commit comments

Comments
 (0)