绷不住了,迷枵的 Rust 代码已经开始 Hack 成这样了:
#[inline]
pub fn register_component_checked(&mut self) -> ComponentId {
use core::mem;
if let Some(&required) = get_required_component_id(/* .. */) {
// 注: `ComponentId` 是 `NonZeroU32`,内存模型与 `u32` 保持一致
let slice: &[u32] = unsafe {
mem::transmute::<&[ComponentId], &[u32]>(&self.check_stack)
};
if slice.contains(unsafe { mem::transmute::<&ComponentId, &u32>(&required) }) {
handle_recursion_error(/* .. */);
}
return required;
}
/* .. */
}
因为标准库切片的 contains 对 u32 等基础类型有 SIMD 优化…