Skip to content

Commit

Permalink
Derive FromZeroes & FromBytes for Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
muwo-barenhoff authored and birkenfeld committed Aug 9, 2024
1 parent b65baee commit a74527f
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/strings.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<const LEN: usize>([u8; LEN], u8); // one extra NULL byte

impl<const LEN: usize> String<LEN> {
Expand Down Expand Up @@ -128,14 +130,6 @@ unsafe impl<const LEN: usize> zerocopy::AsBytes for String<LEN> {
fn only_derive_is_allowed_to_implement_this_trait() { }
}

unsafe impl<const LEN: usize> zerocopy::FromZeroes for String<LEN> {
fn only_derive_is_allowed_to_implement_this_trait() { }
}

unsafe impl<const LEN: usize> zerocopy::FromBytes for String<LEN> {
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
Expand All @@ -148,7 +142,7 @@ unsafe impl<const LEN: usize> zerocopy::FromBytes for String<LEN> {
/// 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<const LEN: usize>([u16; LEN], u16); // one extra NULL byte

impl<const LEN: usize> WString<LEN> {
Expand Down Expand Up @@ -275,14 +269,6 @@ unsafe impl<const LEN: usize> zerocopy::AsBytes for WString<LEN> {
fn only_derive_is_allowed_to_implement_this_trait() { }
}

unsafe impl<const LEN: usize> zerocopy::FromZeroes for WString<LEN> {
fn only_derive_is_allowed_to_implement_this_trait() { }
}

unsafe impl<const LEN: usize> zerocopy::FromBytes for WString<LEN> {
fn only_derive_is_allowed_to_implement_this_trait() { }
}

// compatibility aliases

/// Alias for `String<80>`.
Expand Down

0 comments on commit a74527f

Please sign in to comment.