我有以下带有转换逻辑的结构:
enum TagType {
DOUBLE,
BYTE,
}
impl TagType {
/// byte-size actually
fn size(&self) -> usize {
match self {
TagType::DOUBLE => 8,
TagType::BYTE => 1,
}
}
}
/// Entry with buffered data.
///
/// Should not be used for tags where the data fits in the offset field
/// byte-order of the data should be native-endian in this buffer
#[derive(Debug, PartialEq, Clone)]
pub struct BufferedEntry {
pub tag_type: TagType,
pub count: u64,
pub data: Vec<u8>,
}
impl<'a> TryFrom<&'a BufferedEntry> for &'a [f64] {
type Error = ();
fn try_from(val: &'a BufferedEntry) -> Result<Self, Self::Error> {
if val.data.len() != val.tag_type.size() * usize::try_from(val.count)? {
return Err(());
}
match val.tag_type {
TagType::DOUBLE => Ok(bytemuck::cast_slice(&val.data()[..])),
_ => Err(()),
}
}
}
现在,如果输入向量未正确对齐到 8 字节边界,这将导致 panic,而我无法做到这一点。或者,rkyv 有AlignedVec,这应该正是我需要的。然而,问题是:我该如何测试这一点?例如,我该如何编写一个分配非 64 对齐向量的测试?