@@ -41,9 +41,8 @@ macro_rules! define_id {
41
41
}
42
42
/// Constructs a wrapper type from the numeric identifier.
43
43
///
44
- /// # Safety
45
- ///
46
- /// The passed `id` must not be zero.
44
+ /// Returns `None` if the provided `id` is zero, otherwise returns
45
+ /// `Some(Self)` containing the wrapped non-zero identifier.
47
46
pub fn new( id: $primitive) -> Option <Self > {
48
47
NonZero :: <$primitive>:: new( id) . map( |id| Self { id} )
49
48
}
@@ -161,7 +160,7 @@ impl TaskId {
161
160
}
162
161
163
162
macro_rules! make_serializable {
164
- ( $ty: ty, $get_object: path, $visitor_name: ident) => {
163
+ ( $ty: ty, $get_object: path, $validate_type_id : path , $ visitor_name: ident) => {
165
164
impl Serialize for $ty {
166
165
fn serialize<S >( & self , serializer: S ) -> Result <S :: Ok , S :: Error >
167
166
where
@@ -193,8 +192,16 @@ macro_rules! make_serializable {
193
192
where
194
193
E : serde:: de:: Error ,
195
194
{
196
- Self :: Value :: new( v)
197
- . ok_or_else( || E :: unknown_variant( & format!( "{v}" ) , & [ "a non zero u32" ] ) )
195
+ match Self :: Value :: new( v) {
196
+ Some ( value) => {
197
+ if let Some ( error) = $validate_type_id( value) {
198
+ Err ( E :: custom( error) )
199
+ } else {
200
+ Ok ( value)
201
+ }
202
+ }
203
+ None => Err ( E :: unknown_variant( & format!( "{v}" ) , & [ "a non zero u32" ] ) ) ,
204
+ }
198
205
}
199
206
}
200
207
@@ -209,10 +216,21 @@ macro_rules! make_serializable {
209
216
} ;
210
217
}
211
218
212
- make_serializable ! ( ValueTypeId , registry:: get_value_type, ValueTypeVisitor ) ;
213
- make_serializable ! ( TraitTypeId , registry:: get_trait, TraitTypeVisitor ) ;
219
+ make_serializable ! (
220
+ ValueTypeId ,
221
+ registry:: get_value_type,
222
+ registry:: validate_value_type_id,
223
+ ValueTypeVisitor
224
+ ) ;
225
+ make_serializable ! (
226
+ TraitTypeId ,
227
+ registry:: get_trait,
228
+ registry:: validate_trait_type_id,
229
+ TraitTypeVisitor
230
+ ) ;
214
231
make_serializable ! (
215
232
FunctionId ,
216
233
registry:: get_native_function,
234
+ registry:: validate_function_id,
217
235
FunctionTypeVisitor
218
236
) ;
0 commit comments