Skip to content

Commit 9df6dc7

Browse files
committed
serialize ids as u32s
1 parent 6b126b4 commit 9df6dc7

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

turbopack/crates/turbo-tasks/src/id.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ macro_rules! define_id {
3939
pub const unsafe fn new_unchecked(id: $primitive) -> Self {
4040
Self { id: unsafe { NonZero::<$primitive>::new_unchecked(id) } }
4141
}
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+
}
4350
/// Allows `const` conversion to a [`NonZeroU64`], useful with
4451
/// [`crate::id_factory::IdFactory::new_const`].
4552
pub const fn to_non_zero_u64(self) -> NonZeroU64 {
@@ -153,13 +160,13 @@ impl TaskId {
153160
}
154161

155162
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) => {
157164
impl Serialize for $ty {
158165
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
159166
where
160167
S: serde::Serializer,
161168
{
162-
serializer.serialize_str($get_global_name(*self))
169+
serializer.serialize_u32(self.id.into())
163170
}
164171
}
165172

@@ -168,7 +175,7 @@ macro_rules! make_serializable {
168175
where
169176
D: serde::Deserializer<'de>,
170177
{
171-
deserializer.deserialize_str($visitor_name)
178+
deserializer.deserialize_u32($visitor_name)
172179
}
173180
}
174181

@@ -178,14 +185,15 @@ macro_rules! make_serializable {
178185
type Value = $ty;
179186

180187
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)))
182189
}
183190

184-
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
191+
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
185192
where
186193
E: serde::de::Error,
187194
{
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"]))
189197
}
190198
}
191199

@@ -203,12 +211,10 @@ macro_rules! make_serializable {
203211
make_serializable!(
204212
ValueTypeId,
205213
registry::get_value_type_global_name,
206-
registry::get_value_type_id_by_global_name,
207214
ValueTypeVisitor
208215
);
209216
make_serializable!(
210217
TraitTypeId,
211218
registry::get_trait_type_global_name,
212-
registry::get_trait_type_id_by_global_name,
213219
TraitTypeVisitor
214220
);

turbopack/crates/turbo-tasks/src/registry.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ pub fn get_trait_type_id(trait_type: &'static TraitType) -> TraitTypeId {
145145
}
146146
}
147147

148-
pub fn get_trait_type_id_by_global_name(global_name: &str) -> Option<TraitTypeId> {
149-
TRAITS
150-
.global_name_to_trait
151-
.get(global_name)
152-
.map(|(id, _)| *id)
153-
}
154-
155148
pub fn get_trait(id: TraitTypeId) -> &'static TraitType {
156149
TRAITS.id_to_trait[*id as usize - 1]
157150
}

0 commit comments

Comments
 (0)