@@ -39,7 +39,14 @@ macro_rules! define_id {
39
39
pub const unsafe fn new_unchecked( id: $primitive) -> Self {
40
40
Self { id: unsafe { NonZero :: <$primitive>:: new_unchecked( id) } }
41
41
}
42
-
42
+ /// Constructs a wrapper type from the numeric identifier.
43
+ ///
44
+ /// # Safety
45
+ ///
46
+ /// The passed `id` must not be zero.
47
+ pub fn new( id: $primitive) -> Option <Self > {
48
+ NonZero :: <$primitive>:: new( id) . map( |id| Self { id} )
49
+ }
43
50
/// Allows `const` conversion to a [`NonZeroU64`], useful with
44
51
/// [`crate::id_factory::IdFactory::new_const`].
45
52
pub const fn to_non_zero_u64( self ) -> NonZeroU64 {
@@ -153,13 +160,13 @@ impl TaskId {
153
160
}
154
161
155
162
macro_rules! make_serializable {
156
- ( $ty: ty, $get_global_name: path, $get_id : path , $ visitor_name: ident) => {
163
+ ( $ty: ty, $get_global_name: path, $visitor_name: ident) => {
157
164
impl Serialize for $ty {
158
165
fn serialize<S >( & self , serializer: S ) -> Result <S :: Ok , S :: Error >
159
166
where
160
167
S : serde:: Serializer ,
161
168
{
162
- serializer. serialize_str ( $get_global_name ( * self ) )
169
+ serializer. serialize_u32 ( self . id . into ( ) )
163
170
}
164
171
}
165
172
@@ -168,7 +175,7 @@ macro_rules! make_serializable {
168
175
where
169
176
D : serde:: Deserializer <' de>,
170
177
{
171
- deserializer. deserialize_str ( $visitor_name)
178
+ deserializer. deserialize_u32 ( $visitor_name)
172
179
}
173
180
}
174
181
@@ -178,14 +185,15 @@ macro_rules! make_serializable {
178
185
type Value = $ty;
179
186
180
187
fn expecting( & self , formatter: & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
181
- formatter. write_str( concat!( "a name of a registered " , stringify!( $ty) ) )
188
+ formatter. write_str( concat!( "an id of a registered " , stringify!( $ty) ) )
182
189
}
183
190
184
- fn visit_str <E >( self , v: & str ) -> Result <Self :: Value , E >
191
+ fn visit_u32 <E >( self , v: u32 ) -> Result <Self :: Value , E >
185
192
where
186
193
E : serde:: de:: Error ,
187
194
{
188
- $get_id( v) . ok_or_else( || E :: unknown_variant( v, & [ ] ) )
195
+ Self :: Value :: new( v)
196
+ . ok_or_else( || E :: unknown_variant( & format!( "{v}" ) , & [ "a non zero u32" ] ) )
189
197
}
190
198
}
191
199
@@ -203,12 +211,10 @@ macro_rules! make_serializable {
203
211
make_serializable ! (
204
212
ValueTypeId ,
205
213
registry:: get_value_type_global_name,
206
- registry:: get_value_type_id_by_global_name,
207
214
ValueTypeVisitor
208
215
) ;
209
216
make_serializable ! (
210
217
TraitTypeId ,
211
218
registry:: get_trait_type_global_name,
212
- registry:: get_trait_type_id_by_global_name,
213
219
TraitTypeVisitor
214
220
) ;
0 commit comments