From a74527fe9ab4d5d5d9c773a2556eabfb9107d6c3 Mon Sep 17 00:00:00 2001 From: Markus Barenhoff Date: Tue, 12 Dec 2023 07:36:57 +0100 Subject: [PATCH] Derive FromZeroes & FromBytes for Strings --- src/strings.rs | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/strings.rs b/src/strings.rs index 4e94a85..1ce459a 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -1,5 +1,7 @@ //! Const-generic string types for representing fixed-length strings. +use zerocopy::{FromBytes, FromZeroes}; + /// Represents a fixed-length byte string. /// /// This type can be created from a `&str` or `&[u8]` if their byte @@ -10,7 +12,7 @@ /// /// It can be converted to a Rust `String` if it is UTF8 encoded. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, FromBytes, FromZeroes)] pub struct String([u8; LEN], u8); // one extra NULL byte impl String { @@ -128,14 +130,6 @@ unsafe impl zerocopy::AsBytes for String { fn only_derive_is_allowed_to_implement_this_trait() { } } -unsafe impl zerocopy::FromZeroes for String { - fn only_derive_is_allowed_to_implement_this_trait() { } -} - -unsafe impl zerocopy::FromBytes for String { - fn only_derive_is_allowed_to_implement_this_trait() { } -} - /// Represents a fixed-length wide string. /// /// This type can be created from a `&[u16]` if its length does not @@ -148,7 +142,7 @@ unsafe impl zerocopy::FromBytes for String { /// It can be converted to a Rust `String` if it is properly UTF16 /// encoded. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, FromBytes, FromZeroes)] pub struct WString([u16; LEN], u16); // one extra NULL byte impl WString { @@ -275,14 +269,6 @@ unsafe impl zerocopy::AsBytes for WString { fn only_derive_is_allowed_to_implement_this_trait() { } } -unsafe impl zerocopy::FromZeroes for WString { - fn only_derive_is_allowed_to_implement_this_trait() { } -} - -unsafe impl zerocopy::FromBytes for WString { - fn only_derive_is_allowed_to_implement_this_trait() { } -} - // compatibility aliases /// Alias for `String<80>`.