假如我要派生下面一个结构体的 bytemuck::Pod 和 bytemuck::Zeroable 方法
#[derive(Debug,Clone, Copy,bytemuck::Pod,bytemuck::Zeroable)]
#[repr(C)]
pub struct View0{
pos:f32,
size:u128,
}
报出如下错位
source type: `View0` (192 bits)
target type: `TypeWithoutPadding` (160 bits)
通过 expand 发现问题出在如下自动派生的检查
const _: fn() = || {
struct TypeWithoutPadding([u8; ::core::mem::size_of::<f32>() + ::core::mem::size_of::<u128>()]);
let _ = ::core::mem::transmute::<View0, TypeWithoutPadding>;
};
这个检查要求结构体的所有成员属性大小之和等于结构体大小之和,由于我的结构体中间会为了保持内存对齐,而在pos和*size之间填充一些字节所以上述大小不相等、报错。
我想知道这个检查的意义,如下手动实现避免了检查会不会有什么影响?
unsafe impl bytemuck::Zeroable for View0{
}
unsafe impl bytemuck::Pod for View0 {
}