Skip to content

Commit 4f343ab

Browse files
authored
refactor: split out impls (#3)
Signed-off-by: tison <[email protected]>
1 parent bc8f4ac commit 4f343ab

7 files changed

Lines changed: 619 additions & 622 deletions

File tree

serde-shape/src/impls/container.rs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Copyright 2026 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::DeserializeShape;
16+
use crate::DeserializeShapeContext;
17+
use crate::SerializeShape;
18+
use crate::SerializeShapeContext;
19+
use crate::ShapeRef;
20+
21+
impl<T> SerializeShape for Option<T>
22+
where
23+
T: SerializeShape,
24+
{
25+
fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
26+
ShapeRef::Option(Box::new(T::serialize_shape_in(context)))
27+
}
28+
}
29+
30+
impl<T> DeserializeShape for Option<T>
31+
where
32+
T: DeserializeShape,
33+
{
34+
fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
35+
ShapeRef::Option(Box::new(T::deserialize_shape_in(context)))
36+
}
37+
}
38+
39+
macro_rules! seq_shape {
40+
(
41+
$(
42+
($($generics:tt)*) $ty:ty
43+
where
44+
serialize { $($serialize_bounds:tt)* }
45+
deserialize { $($deserialize_bounds:tt)* }
46+
=> $item:ty;
47+
)+
48+
) => {
49+
$(
50+
impl<$($generics)*> SerializeShape for $ty
51+
where
52+
$($serialize_bounds)*
53+
{
54+
fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
55+
ShapeRef::Seq(Box::new(<$item as SerializeShape>::serialize_shape_in(context)))
56+
}
57+
}
58+
59+
impl<$($generics)*> DeserializeShape for $ty
60+
where
61+
$($deserialize_bounds)*
62+
{
63+
fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
64+
ShapeRef::Seq(Box::new(<$item as DeserializeShape>::deserialize_shape_in(context)))
65+
}
66+
}
67+
)+
68+
};
69+
}
70+
71+
seq_shape! {
72+
(T) Vec<T>
73+
where
74+
serialize { T: SerializeShape }
75+
deserialize { T: DeserializeShape }
76+
=> T;
77+
78+
(T) std::collections::VecDeque<T>
79+
where
80+
serialize { T: SerializeShape }
81+
deserialize { T: DeserializeShape }
82+
=> T;
83+
84+
(T) std::collections::LinkedList<T>
85+
where
86+
serialize { T: SerializeShape }
87+
deserialize { T: DeserializeShape }
88+
=> T;
89+
90+
(T) std::collections::BinaryHeap<T>
91+
where
92+
serialize { T: Ord + SerializeShape }
93+
deserialize { T: Ord + DeserializeShape }
94+
=> T;
95+
96+
(T) std::collections::BTreeSet<T>
97+
where
98+
serialize { T: SerializeShape }
99+
deserialize { T: DeserializeShape }
100+
=> T;
101+
102+
(T, S) std::collections::HashSet<T, S>
103+
where
104+
serialize { T: SerializeShape }
105+
deserialize { T: DeserializeShape }
106+
=> T;
107+
}
108+
109+
impl<T, const N: usize> SerializeShape for [T; N]
110+
where
111+
T: SerializeShape,
112+
{
113+
fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
114+
ShapeRef::Array {
115+
item: Box::new(T::serialize_shape_in(context)),
116+
len: N,
117+
}
118+
}
119+
}
120+
121+
impl<T, const N: usize> DeserializeShape for [T; N]
122+
where
123+
T: DeserializeShape,
124+
{
125+
fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
126+
ShapeRef::Array {
127+
item: Box::new(T::deserialize_shape_in(context)),
128+
len: N,
129+
}
130+
}
131+
}
132+
133+
macro_rules! map_shape {
134+
(
135+
$(
136+
($($generics:tt)*) $ty:ty
137+
where
138+
serialize { $($serialize_bounds:tt)* }
139+
deserialize { $($deserialize_bounds:tt)* }
140+
=> ($key:ty, $value:ty);
141+
)+
142+
) => {
143+
$(
144+
impl<$($generics)*> SerializeShape for $ty
145+
where
146+
$($serialize_bounds)*
147+
{
148+
fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
149+
ShapeRef::Map {
150+
key: Box::new(<$key as SerializeShape>::serialize_shape_in(context)),
151+
value: Box::new(<$value as SerializeShape>::serialize_shape_in(context)),
152+
}
153+
}
154+
}
155+
156+
impl<$($generics)*> DeserializeShape for $ty
157+
where
158+
$($deserialize_bounds)*
159+
{
160+
fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
161+
ShapeRef::Map {
162+
key: Box::new(<$key as DeserializeShape>::deserialize_shape_in(context)),
163+
value: Box::new(<$value as DeserializeShape>::deserialize_shape_in(context)),
164+
}
165+
}
166+
}
167+
)+
168+
};
169+
}
170+
171+
map_shape! {
172+
(K, V) std::collections::BTreeMap<K, V>
173+
where
174+
serialize {
175+
K: SerializeShape,
176+
V: SerializeShape
177+
}
178+
deserialize {
179+
K: DeserializeShape,
180+
V: DeserializeShape
181+
}
182+
=> (K, V);
183+
184+
(K, V, S) std::collections::HashMap<K, V, S>
185+
where
186+
serialize {
187+
K: SerializeShape,
188+
V: SerializeShape
189+
}
190+
deserialize {
191+
K: DeserializeShape,
192+
V: DeserializeShape
193+
}
194+
=> (K, V);
195+
}

serde-shape/src/impls/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2026 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod container;
16+
mod primitive;
17+
mod tuple;
18+
mod wrapper;

serde-shape/src/impls/primitive.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2026 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::DeserializeShape;
16+
use crate::DeserializeShapeContext;
17+
use crate::SerializeShape;
18+
use crate::SerializeShapeContext;
19+
use crate::ShapeRef;
20+
21+
macro_rules! primitive_shape {
22+
($($ty:ty => $shape:expr;)+) => {
23+
$(
24+
impl SerializeShape for $ty {
25+
fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
26+
$shape
27+
}
28+
}
29+
30+
impl DeserializeShape for $ty {
31+
fn deserialize_shape_in(_context: &mut DeserializeShapeContext) -> ShapeRef {
32+
$shape
33+
}
34+
}
35+
)+
36+
};
37+
}
38+
39+
primitive_shape! {
40+
() => ShapeRef::Unit;
41+
bool => ShapeRef::Bool;
42+
char => ShapeRef::Char;
43+
i8 => ShapeRef::I8;
44+
i16 => ShapeRef::I16;
45+
i32 => ShapeRef::I32;
46+
i64 => ShapeRef::I64;
47+
i128 => ShapeRef::I128;
48+
isize => ShapeRef::Isize;
49+
u8 => ShapeRef::U8;
50+
u16 => ShapeRef::U16;
51+
u32 => ShapeRef::U32;
52+
u64 => ShapeRef::U64;
53+
u128 => ShapeRef::U128;
54+
usize => ShapeRef::Usize;
55+
f32 => ShapeRef::F32;
56+
f64 => ShapeRef::F64;
57+
str => ShapeRef::String;
58+
[u8] => ShapeRef::Bytes;
59+
String => ShapeRef::String;
60+
std::path::Path => ShapeRef::String;
61+
std::path::PathBuf => ShapeRef::String;
62+
std::net::IpAddr => ShapeRef::String;
63+
std::net::Ipv4Addr => ShapeRef::String;
64+
std::net::Ipv6Addr => ShapeRef::String;
65+
std::net::SocketAddr => ShapeRef::String;
66+
std::net::SocketAddrV4 => ShapeRef::String;
67+
std::net::SocketAddrV6 => ShapeRef::String;
68+
std::num::NonZeroI8 => ShapeRef::I8;
69+
std::num::NonZeroI16 => ShapeRef::I16;
70+
std::num::NonZeroI32 => ShapeRef::I32;
71+
std::num::NonZeroI64 => ShapeRef::I64;
72+
std::num::NonZeroI128 => ShapeRef::I128;
73+
std::num::NonZeroIsize => ShapeRef::Isize;
74+
std::num::NonZeroU8 => ShapeRef::U8;
75+
std::num::NonZeroU16 => ShapeRef::U16;
76+
std::num::NonZeroU32 => ShapeRef::U32;
77+
std::num::NonZeroU64 => ShapeRef::U64;
78+
std::num::NonZeroU128 => ShapeRef::U128;
79+
std::num::NonZeroUsize => ShapeRef::Usize;
80+
}
81+
82+
#[cfg(target_has_atomic = "8")]
83+
primitive_shape! {
84+
std::sync::atomic::AtomicBool => ShapeRef::Bool;
85+
std::sync::atomic::AtomicI8 => ShapeRef::I8;
86+
std::sync::atomic::AtomicU8 => ShapeRef::U8;
87+
}
88+
89+
#[cfg(target_has_atomic = "16")]
90+
primitive_shape! {
91+
std::sync::atomic::AtomicI16 => ShapeRef::I16;
92+
std::sync::atomic::AtomicU16 => ShapeRef::U16;
93+
}
94+
95+
#[cfg(target_has_atomic = "32")]
96+
primitive_shape! {
97+
std::sync::atomic::AtomicI32 => ShapeRef::I32;
98+
std::sync::atomic::AtomicU32 => ShapeRef::U32;
99+
}
100+
101+
#[cfg(target_has_atomic = "64")]
102+
primitive_shape! {
103+
std::sync::atomic::AtomicI64 => ShapeRef::I64;
104+
std::sync::atomic::AtomicU64 => ShapeRef::U64;
105+
}
106+
107+
#[cfg(target_has_atomic = "ptr")]
108+
primitive_shape! {
109+
std::sync::atomic::AtomicIsize => ShapeRef::Isize;
110+
std::sync::atomic::AtomicUsize => ShapeRef::Usize;
111+
}

serde-shape/src/impls/tuple.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2026 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::DeserializeShape;
16+
use crate::DeserializeShapeContext;
17+
use crate::SerializeShape;
18+
use crate::SerializeShapeContext;
19+
use crate::ShapeRef;
20+
21+
macro_rules! tuple_shape {
22+
($($($name:ident),+;)+) => {
23+
$(
24+
impl<$($name),+> SerializeShape for ($($name,)+)
25+
where
26+
$($name: SerializeShape,)+
27+
{
28+
fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
29+
ShapeRef::Tuple(vec![$($name::serialize_shape_in(context),)+])
30+
}
31+
}
32+
33+
impl<$($name),+> DeserializeShape for ($($name,)+)
34+
where
35+
$($name: DeserializeShape,)+
36+
{
37+
fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
38+
ShapeRef::Tuple(vec![$($name::deserialize_shape_in(context),)+])
39+
}
40+
}
41+
)+
42+
};
43+
}
44+
45+
tuple_shape! {
46+
T0;
47+
T0, T1;
48+
T0, T1, T2;
49+
T0, T1, T2, T3;
50+
T0, T1, T2, T3, T4;
51+
T0, T1, T2, T3, T4, T5;
52+
T0, T1, T2, T3, T4, T5, T6;
53+
T0, T1, T2, T3, T4, T5, T6, T7;
54+
T0, T1, T2, T3, T4, T5, T6, T7, T8;
55+
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9;
56+
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10;
57+
T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11;
58+
}

0 commit comments

Comments
 (0)