Skip to main content

serde_shape/impls/
primitive.rs

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
15use alloc::string::String;
16
17use crate::DeserializeShape;
18use crate::DeserializeShapeContext;
19use crate::SerializeShape;
20use crate::SerializeShapeContext;
21use crate::ShapeRef;
22
23macro_rules! primitive_shape {
24    ($($ty:ty => $shape:expr;)+) => {
25        $(
26            impl SerializeShape for $ty {
27                fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
28                    $shape
29                }
30            }
31
32            impl DeserializeShape for $ty {
33                fn deserialize_shape_in(_context: &mut DeserializeShapeContext) -> ShapeRef {
34                    $shape
35                }
36            }
37        )+
38    };
39}
40
41primitive_shape! {
42    () => ShapeRef::Unit;
43    bool => ShapeRef::Bool;
44    char => ShapeRef::Char;
45    i8 => ShapeRef::I8;
46    i16 => ShapeRef::I16;
47    i32 => ShapeRef::I32;
48    i64 => ShapeRef::I64;
49    i128 => ShapeRef::I128;
50    isize => ShapeRef::Isize;
51    u8 => ShapeRef::U8;
52    u16 => ShapeRef::U16;
53    u32 => ShapeRef::U32;
54    u64 => ShapeRef::U64;
55    u128 => ShapeRef::U128;
56    usize => ShapeRef::Usize;
57    f32 => ShapeRef::F32;
58    f64 => ShapeRef::F64;
59    String => ShapeRef::String;
60    core::num::NonZeroI8 => ShapeRef::I8;
61    core::num::NonZeroI16 => ShapeRef::I16;
62    core::num::NonZeroI32 => ShapeRef::I32;
63    core::num::NonZeroI64 => ShapeRef::I64;
64    core::num::NonZeroI128 => ShapeRef::I128;
65    core::num::NonZeroIsize => ShapeRef::Isize;
66    core::num::NonZeroU8 => ShapeRef::U8;
67    core::num::NonZeroU16 => ShapeRef::U16;
68    core::num::NonZeroU32 => ShapeRef::U32;
69    core::num::NonZeroU64 => ShapeRef::U64;
70    core::num::NonZeroU128 => ShapeRef::U128;
71    core::num::NonZeroUsize => ShapeRef::Usize;
72}
73
74impl SerializeShape for str {
75    fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
76        ShapeRef::String
77    }
78}
79
80impl SerializeShape for core::fmt::Arguments<'_> {
81    fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
82        ShapeRef::String
83    }
84}
85
86#[cfg(feature = "std")]
87impl SerializeShape for std::path::Path {
88    fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
89        ShapeRef::String
90    }
91}
92
93#[cfg(feature = "std")]
94impl SerializeShape for std::path::PathBuf {
95    fn serialize_shape_in(_context: &mut SerializeShapeContext) -> ShapeRef {
96        ShapeRef::String
97    }
98}
99
100#[cfg(feature = "std")]
101impl DeserializeShape for std::path::PathBuf {
102    fn deserialize_shape_in(_context: &mut DeserializeShapeContext) -> ShapeRef {
103        ShapeRef::String
104    }
105}
106
107#[cfg(all(feature = "std", target_has_atomic = "8"))]
108primitive_shape! {
109    core::sync::atomic::AtomicBool => ShapeRef::Bool;
110    core::sync::atomic::AtomicI8 => ShapeRef::I8;
111    core::sync::atomic::AtomicU8 => ShapeRef::U8;
112}
113
114#[cfg(all(feature = "std", target_has_atomic = "16"))]
115primitive_shape! {
116    core::sync::atomic::AtomicI16 => ShapeRef::I16;
117    core::sync::atomic::AtomicU16 => ShapeRef::U16;
118}
119
120#[cfg(all(feature = "std", target_has_atomic = "32"))]
121primitive_shape! {
122    core::sync::atomic::AtomicI32 => ShapeRef::I32;
123    core::sync::atomic::AtomicU32 => ShapeRef::U32;
124}
125
126#[cfg(all(feature = "std", target_has_atomic = "64"))]
127primitive_shape! {
128    core::sync::atomic::AtomicI64 => ShapeRef::I64;
129    core::sync::atomic::AtomicU64 => ShapeRef::U64;
130}
131
132#[cfg(all(feature = "std", target_has_atomic = "ptr"))]
133primitive_shape! {
134    core::sync::atomic::AtomicIsize => ShapeRef::Isize;
135    core::sync::atomic::AtomicUsize => ShapeRef::Usize;
136}