fix: update zerocopy usage in header and wrapper modules to use new traits

This commit is contained in:
vincent-herlemont
2024-10-05 10:25:13 +02:00
parent 888b795129
commit 60bcfb5cb2
2 changed files with 8 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
use zerocopy::little_endian::U32;
use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(FromZeroes, FromBytes, AsBytes, Debug)]
#[derive(FromBytes, IntoBytes, Immutable, KnownLayout, Debug)]
#[repr(C)]
pub struct Header {
pub(crate) id: U32,

View File

@@ -1,16 +1,15 @@
use crate::header::Header;
use zerocopy::little_endian::U32;
use zerocopy::{AsBytes, ByteSlice, ByteSliceMut, Ref};
use zerocopy::{SplitByteSlice, SplitByteSliceMut, Ref, IntoBytes};
#[derive(Debug)]
pub struct Wrapper<T: ByteSlice> {
header: Ref<T, Header>, // Deprecated: Rename LayoutVerified to Ref #203
pub struct Wrapper<T: SplitByteSlice> {
header: Ref<T, Header>,
value: T,
}
impl<T: ByteSlice> Wrapper<T> {
impl<T: SplitByteSlice> Wrapper<T> {
pub fn deserialize(packed: T) -> Option<Self> {
let (header_lv, rest) = Ref::<_, Header>::new_from_prefix(packed)?;
let (header_lv, rest) = Ref::<_, Header>::from_prefix(packed).ok()?;
let native_model = Self {
header: header_lv,
value: rest,
@@ -35,7 +34,7 @@ impl<T: ByteSlice> Wrapper<T> {
}
}
impl<T: ByteSliceMut> Wrapper<T> {
impl<T: SplitByteSliceMut> Wrapper<T> {
pub fn set_type_id(&mut self, type_id: u32) {
self.header.id = U32::new(type_id);
}