From 9a284d2942e792bf28b8f4417f4e0a00f809051b Mon Sep 17 00:00:00 2001 From: vincent-herlemont Date: Sat, 5 Oct 2024 10:25:13 +0200 Subject: [PATCH] fix: update zerocopy usage in header and wrapper modules to use new traits --- libraries/native_model/src/header.rs | 4 ++-- libraries/native_model/src/wrapper.rs | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/libraries/native_model/src/header.rs b/libraries/native_model/src/header.rs index db836feb..d2bee226 100644 --- a/libraries/native_model/src/header.rs +++ b/libraries/native_model/src/header.rs @@ -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, diff --git a/libraries/native_model/src/wrapper.rs b/libraries/native_model/src/wrapper.rs index 5b13443d..c60c036e 100644 --- a/libraries/native_model/src/wrapper.rs +++ b/libraries/native_model/src/wrapper.rs @@ -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 { - header: Ref, // Deprecated: Rename LayoutVerified to Ref #203 +pub struct Wrapper { + header: Ref, value: T, } -impl Wrapper { +impl Wrapper { pub fn deserialize(packed: T) -> Option { - 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 Wrapper { } } -impl Wrapper { +impl Wrapper { pub fn set_type_id(&mut self, type_id: u32) { self.header.id = U32::new(type_id); }