Skip to content

Commit 1b119cb

Browse files
authored
chore: bump inventory 0.1 -> 0.3 (#9159)
* bump inventory 0.1 -> 0.3 * Fix clippy * Remove unnecessary unsafe
1 parent 431a194 commit 1b119cb

File tree

11 files changed

+99
-138
lines changed

11 files changed

+99
-138
lines changed

Cargo.lock

Lines changed: 6 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ napi-build = { version = "2.1.4" }
9090
napi-derive = { version = "3.0.0-alpha.22" }
9191

9292
# Serialize and Deserialize
93-
inventory = { version = "=0.1" }
93+
inventory = { version = "0.3.17" }
9494
rkyv = { version = "=0.8.8" }
9595

9696
# Must be pinned with the same swc versions

crates/rspack_cacheable/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ serde_json = { workspace = true }
2626
smol_str = { workspace = true }
2727
swc_core = { workspace = true, features = ["ecma_ast"] }
2828
ustr = { workspace = true }
29+
xxhash-rust = { workspace = true, features = ["const_xxh64"] }

crates/rspack_cacheable/src/dyn/mod.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use rkyv::{
1414
};
1515

1616
pub mod validation;
17+
mod vtable_ptr;
18+
19+
pub use vtable_ptr::VTablePtr;
20+
1721
use crate::{DeserializeError, Deserializer, SerializeError, Serializer};
1822

1923
/// A trait object that can be archived.
@@ -140,35 +144,35 @@ impl<T: ?Sized> ArchivedDynMetadata<T> {
140144
/// Returns the pointer metadata for the trait object this metadata refers to.
141145
pub fn lookup_metadata(&self) -> DynMetadata<T> {
142146
unsafe {
143-
std::mem::transmute(
144-
*DYN_REGISTRY
145-
.get(&self.dyn_id.to_native())
146-
.expect("attempted to get vtable for an unregistered impl"),
147-
)
147+
DYN_REGISTRY
148+
.get(&self.dyn_id.to_native())
149+
.expect("attempted to get vtable for an unregistered impl")
150+
.cast()
148151
}
149152
}
150153
}
151154

152155
pub struct DynEntry {
153156
dyn_id: u64,
154-
vtable: usize,
157+
vtable: VTablePtr,
155158
}
156159

157160
impl DynEntry {
158-
pub fn new(dyn_id: u64, vtable: usize) -> Self {
161+
pub const fn new(dyn_id: u64, vtable: VTablePtr) -> Self {
159162
Self { dyn_id, vtable }
160163
}
161164
}
162165

163166
inventory::collect!(DynEntry);
164167

165-
static DYN_REGISTRY: std::sync::LazyLock<HashMap<u64, usize>> = std::sync::LazyLock::new(|| {
166-
let mut result = HashMap::default();
167-
for entry in inventory::iter::<DynEntry> {
168-
let old_value = result.insert(entry.dyn_id, entry.vtable);
169-
if old_value.is_some() {
170-
panic!("cacheable_dyn init global REGISTRY error, duplicate implementation.")
168+
static DYN_REGISTRY: std::sync::LazyLock<HashMap<u64, VTablePtr>> =
169+
std::sync::LazyLock::new(|| {
170+
let mut result = HashMap::default();
171+
for entry in inventory::iter::<DynEntry> {
172+
let old_value = result.insert(entry.dyn_id, entry.vtable);
173+
if old_value.is_some() {
174+
panic!("cacheable_dyn init global REGISTRY error, duplicate implementation.")
175+
}
171176
}
172-
}
173-
result
174-
});
177+
result
178+
});

crates/rspack_cacheable/src/dyn/validation.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22

33
use rkyv::bytecheck::CheckBytes;
44

5+
use super::VTablePtr;
56
use crate::{DeserializeError, Validator};
67

78
type CheckBytesDyn = unsafe fn(*const u8, &mut Validator<'_>) -> Result<(), DeserializeError>;
@@ -20,13 +21,13 @@ where
2021
}
2122

2223
pub struct CheckBytesEntry {
23-
vtable: usize,
24+
vtable: VTablePtr,
2425
check_bytes_dyn: CheckBytesDyn,
2526
}
2627

2728
impl CheckBytesEntry {
2829
#[doc(hidden)]
29-
pub fn new(vtable: usize, check_bytes_dyn: CheckBytesDyn) -> Self {
30+
pub const fn new(vtable: VTablePtr, check_bytes_dyn: CheckBytesDyn) -> Self {
3031
Self {
3132
vtable,
3233
check_bytes_dyn,
@@ -36,7 +37,7 @@ impl CheckBytesEntry {
3637

3738
inventory::collect!(CheckBytesEntry);
3839

39-
pub static CHECK_BYTES_REGISTRY: std::sync::LazyLock<HashMap<usize, CheckBytesDyn>> =
40+
pub static CHECK_BYTES_REGISTRY: std::sync::LazyLock<HashMap<VTablePtr, CheckBytesDyn>> =
4041
std::sync::LazyLock::new(|| {
4142
let mut result = HashMap::default();
4243
for entry in inventory::iter::<CheckBytesEntry> {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use rkyv::ptr_meta::DynMetadata;
2+
3+
/// A untyped wrapper for vtable
4+
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
5+
pub struct VTablePtr(DynMetadata<()>);
6+
7+
impl VTablePtr {
8+
pub const fn new<T: ?Sized>(vtable: DynMetadata<T>) -> Self {
9+
// Creating VTablePtr is safe while using it is unsafe, just like raw pointers in rust.
10+
Self(unsafe { core::mem::transmute::<DynMetadata<T>, DynMetadata<()>>(vtable) })
11+
}
12+
13+
/// # Safety
14+
/// The casting target `T` must be consistent with the `T` in VTablePtr::new<T>
15+
/// Currently it is implemented by store VTablePtr as values in HashMap to associate the types with __DYN_ID
16+
pub const unsafe fn cast<T: ?Sized>(self) -> DynMetadata<T> {
17+
core::mem::transmute(self.0)
18+
}
19+
}
20+
unsafe impl Send for VTablePtr {}
21+
unsafe impl Sync for VTablePtr {}

crates/rspack_cacheable/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ pub mod __private {
1919

2020
pub use deserialize::{from_bytes, DeserializeError, Deserializer, Validator};
2121
pub use serialize::{to_bytes, SerializeError, Serializer};
22+
pub use xxhash_rust;

crates/rspack_cacheable_test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ rustc-hash = { workspace = true }
2121
serde_json = { workspace = true }
2222
swc_core = { workspace = true, features = ["ecma_ast"] }
2323
ustr = { workspace = true }
24+
xxhash-rust = { workspace = true, features = ["const_xxh64"] }

crates/rspack_cacheable_test/tests/macro/manual_cacheable_dyn.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rspack_cacheable::{cacheable, from_bytes, to_bytes};
1+
use rspack_cacheable::{cacheable, from_bytes, r#dyn::VTablePtr, to_bytes};
22

33
#[test]
44
#[cfg_attr(miri, ignore)]
@@ -99,7 +99,7 @@ fn test_manual_cacheable_dyn_macro() {
9999
value: *const Self,
100100
context: &mut Validator,
101101
) -> Result<(), DeserializeError> {
102-
let vtable: usize = std::mem::transmute(ptr_meta::metadata(value));
102+
let vtable = VTablePtr::new(ptr_meta::metadata(value));
103103
if let Some(check_bytes_dyn) = CHECK_BYTES_REGISTRY.get(&vtable) {
104104
check_bytes_dyn(value.cast(), context)?;
105105
Ok(())
@@ -115,13 +115,8 @@ fn test_manual_cacheable_dyn_macro() {
115115
color: String,
116116
}
117117

118-
static __DYN_ID_DOG_ANIMAL: std::sync::LazyLock<u64> = std::sync::LazyLock::new(|| {
119-
use std::hash::{DefaultHasher, Hash, Hasher};
120-
let mut hasher = DefaultHasher::new();
121-
module_path!().hash(&mut hasher);
122-
line!().hash(&mut hasher);
123-
hasher.finish()
124-
});
118+
const __DYN_ID_DOG_ANIMAL: u64 =
119+
xxhash_rust::const_xxh64::xxh64(concat!(module_path!(), ":", line!()).as_bytes(), 0);
125120

126121
impl Animal for Dog {
127122
fn color(&self) -> &str {
@@ -132,7 +127,7 @@ fn test_manual_cacheable_dyn_macro() {
132127
}
133128

134129
fn __dyn_id(&self) -> u64 {
135-
*__DYN_ID_DOG_ANIMAL
130+
__DYN_ID_DOG_ANIMAL
136131
}
137132
}
138133

@@ -149,14 +144,12 @@ fn test_manual_cacheable_dyn_macro() {
149144
DeserializeError, Deserializer,
150145
};
151146

152-
fn get_vtable() -> usize {
153-
unsafe {
154-
core::mem::transmute(ptr_meta::metadata(
155-
core::ptr::null::<Archived<Dog>>() as *const <dyn Animal as ArchiveUnsized>::Archived
156-
))
157-
}
147+
const fn get_vtable() -> VTablePtr {
148+
VTablePtr::new(ptr_meta::metadata(
149+
core::ptr::null::<Archived<Dog>>() as *const <dyn Animal as ArchiveUnsized>::Archived
150+
))
158151
}
159-
inventory::submit! { DynEntry::new(*__DYN_ID_DOG_ANIMAL, get_vtable()) }
152+
inventory::submit! { DynEntry::new(__DYN_ID_DOG_ANIMAL, get_vtable()) }
160153
inventory::submit! { CheckBytesEntry::new(get_vtable(), default_check_bytes_dyn::<Archived<Dog>>) }
161154

162155
impl DeserializeDyn<dyn Animal> for ArchivedDog
@@ -184,13 +177,8 @@ fn test_manual_cacheable_dyn_macro() {
184177
color: String,
185178
}
186179

187-
static __DYN_ID_CAT_ANIMAL: std::sync::LazyLock<u64> = std::sync::LazyLock::new(|| {
188-
use std::hash::{DefaultHasher, Hash, Hasher};
189-
let mut hasher = DefaultHasher::new();
190-
module_path!().hash(&mut hasher);
191-
line!().hash(&mut hasher);
192-
hasher.finish()
193-
});
180+
const __DYN_ID_CAT_ANIMAL: u64 =
181+
xxhash_rust::const_xxh64::xxh64(concat!(module_path!(), ":", line!()).as_bytes(), 0);
194182

195183
impl Animal for Cat {
196184
fn color(&self) -> &str {
@@ -201,7 +189,7 @@ fn test_manual_cacheable_dyn_macro() {
201189
}
202190

203191
fn __dyn_id(&self) -> u64 {
204-
*__DYN_ID_CAT_ANIMAL
192+
__DYN_ID_CAT_ANIMAL
205193
}
206194
}
207195

@@ -218,14 +206,12 @@ fn test_manual_cacheable_dyn_macro() {
218206
DeserializeError, Deserializer,
219207
};
220208

221-
fn get_vtable() -> usize {
222-
unsafe {
223-
core::mem::transmute(ptr_meta::metadata(
224-
core::ptr::null::<Archived<Cat>>() as *const <dyn Animal as ArchiveUnsized>::Archived
225-
))
226-
}
209+
const fn get_vtable() -> VTablePtr {
210+
VTablePtr::new(ptr_meta::metadata(
211+
core::ptr::null::<Archived<Cat>>() as *const <dyn Animal as ArchiveUnsized>::Archived
212+
))
227213
}
228-
inventory::submit! { DynEntry::new(*__DYN_ID_CAT_ANIMAL, get_vtable()) }
214+
inventory::submit! { DynEntry::new(__DYN_ID_CAT_ANIMAL, get_vtable()) }
229215
inventory::submit! { CheckBytesEntry::new(get_vtable(), default_check_bytes_dyn::<Archived<Cat>>) }
230216

231217
impl DeserializeDyn<dyn Animal> for ArchivedCat

0 commit comments

Comments
 (0)