Created
November 9, 2024 12:04
-
-
Save tnishinaga/e2de15c9d123c3549e64395329b22d02 to your computer and use it in GitHub Desktop.
zerocopy research
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(prelude_import)] | |
#[prelude_import] | |
use std::prelude::rust_2021::*; | |
#[macro_use] | |
extern crate std; | |
use zerocopy::*; | |
#[repr(C)] | |
struct PacketHeader { | |
src_port: [u8; 2], | |
dst_port: [u8; 2], | |
length: [u8; 2], | |
checksum: [u8; 2], | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::TryFromBytes for PacketHeader | |
where | |
[u8; 2]: ::zerocopy::TryFromBytes, | |
[u8; 2]: ::zerocopy::TryFromBytes, | |
[u8; 2]: ::zerocopy::TryFromBytes, | |
[u8; 2]: ::zerocopy::TryFromBytes, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
fn is_bit_valid<___ZerocopyAliasing>( | |
_candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>, | |
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool | |
where | |
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing | |
+ ::zerocopy::pointer::invariant::AtLeast< | |
::zerocopy::pointer::invariant::Shared, | |
>, | |
{ | |
if false { | |
fn assert_is_from_bytes<T>() | |
where | |
T: ::zerocopy::FromBytes, | |
T: ?::zerocopy::util::macro_util::core_reexport::marker::Sized, | |
{} | |
assert_is_from_bytes::<Self>(); | |
} | |
true | |
} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::FromZeros for PacketHeader | |
where | |
[u8; 2]: ::zerocopy::FromZeros, | |
[u8; 2]: ::zerocopy::FromZeros, | |
[u8; 2]: ::zerocopy::FromZeros, | |
[u8; 2]: ::zerocopy::FromZeros, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::FromBytes for PacketHeader | |
where | |
[u8; 2]: ::zerocopy::FromBytes, | |
[u8; 2]: ::zerocopy::FromBytes, | |
[u8; 2]: ::zerocopy::FromBytes, | |
[u8; 2]: ::zerocopy::FromBytes, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::KnownLayout for PacketHeader | |
where | |
[u8; 2]: ::zerocopy::KnownLayout, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
type PointerMetadata = <[u8; 2] as ::zerocopy::KnownLayout>::PointerMetadata; | |
const LAYOUT: ::zerocopy::DstLayout = { | |
use ::zerocopy::util::macro_util::core_reexport::num::NonZeroUsize; | |
use ::zerocopy::{DstLayout, KnownLayout}; | |
let repr_align = ::zerocopy::util::macro_util::core_reexport::option::Option::None; | |
let repr_packed = ::zerocopy::util::macro_util::core_reexport::option::Option::None; | |
DstLayout::new_zst(repr_align) | |
.extend(DstLayout::for_type::<[u8; 2]>(), repr_packed) | |
.extend(DstLayout::for_type::<[u8; 2]>(), repr_packed) | |
.extend(DstLayout::for_type::<[u8; 2]>(), repr_packed) | |
.extend(<[u8; 2] as KnownLayout>::LAYOUT, repr_packed) | |
.pad_to_align() | |
}; | |
#[inline(always)] | |
fn raw_from_ptr_len( | |
bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>, | |
meta: Self::PointerMetadata, | |
) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> { | |
use ::zerocopy::KnownLayout; | |
let trailing = <[u8; 2] as KnownLayout>::raw_from_ptr_len(bytes, meta); | |
let slf = trailing.as_ptr() as *mut Self; | |
unsafe { | |
::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(slf) | |
} | |
} | |
#[inline(always)] | |
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata { | |
<[u8; 2]>::pointer_to_metadata(ptr as *mut _) | |
} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::Immutable for PacketHeader | |
where | |
[u8; 2]: ::zerocopy::Immutable, | |
[u8; 2]: ::zerocopy::Immutable, | |
[u8; 2]: ::zerocopy::Immutable, | |
[u8; 2]: ::zerocopy::Immutable, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
#[repr(C)] | |
struct Packet { | |
header: PacketHeader, | |
body: [u8], | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::TryFromBytes for Packet | |
where | |
PacketHeader: ::zerocopy::TryFromBytes, | |
[u8]: ::zerocopy::TryFromBytes, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
fn is_bit_valid<___ZerocopyAliasing>( | |
_candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>, | |
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool | |
where | |
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing | |
+ ::zerocopy::pointer::invariant::AtLeast< | |
::zerocopy::pointer::invariant::Shared, | |
>, | |
{ | |
if false { | |
fn assert_is_from_bytes<T>() | |
where | |
T: ::zerocopy::FromBytes, | |
T: ?::zerocopy::util::macro_util::core_reexport::marker::Sized, | |
{} | |
assert_is_from_bytes::<Self>(); | |
} | |
true | |
} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::FromZeros for Packet | |
where | |
PacketHeader: ::zerocopy::FromZeros, | |
[u8]: ::zerocopy::FromZeros, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::FromBytes for Packet | |
where | |
PacketHeader: ::zerocopy::FromBytes, | |
[u8]: ::zerocopy::FromBytes, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::KnownLayout for Packet | |
where | |
[u8]: ::zerocopy::KnownLayout, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
type PointerMetadata = <[u8] as ::zerocopy::KnownLayout>::PointerMetadata; | |
const LAYOUT: ::zerocopy::DstLayout = { | |
use ::zerocopy::util::macro_util::core_reexport::num::NonZeroUsize; | |
use ::zerocopy::{DstLayout, KnownLayout}; | |
let repr_align = ::zerocopy::util::macro_util::core_reexport::option::Option::None; | |
let repr_packed = ::zerocopy::util::macro_util::core_reexport::option::Option::None; | |
DstLayout::new_zst(repr_align) | |
.extend(DstLayout::for_type::<PacketHeader>(), repr_packed) | |
.extend(<[u8] as KnownLayout>::LAYOUT, repr_packed) | |
.pad_to_align() | |
}; | |
#[inline(always)] | |
fn raw_from_ptr_len( | |
bytes: ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<u8>, | |
meta: Self::PointerMetadata, | |
) -> ::zerocopy::util::macro_util::core_reexport::ptr::NonNull<Self> { | |
use ::zerocopy::KnownLayout; | |
let trailing = <[u8] as KnownLayout>::raw_from_ptr_len(bytes, meta); | |
let slf = trailing.as_ptr() as *mut Self; | |
unsafe { | |
::zerocopy::util::macro_util::core_reexport::ptr::NonNull::new_unchecked(slf) | |
} | |
} | |
#[inline(always)] | |
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata { | |
<[u8]>::pointer_to_metadata(ptr as *mut _) | |
} | |
} | |
#[allow(deprecated)] | |
unsafe impl ::zerocopy::Immutable for Packet | |
where | |
PacketHeader: ::zerocopy::Immutable, | |
[u8]: ::zerocopy::Immutable, | |
{ | |
fn only_derive_is_allowed_to_implement_this_trait() {} | |
} | |
fn main() { | |
let bytes = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11][..]; | |
let packet = Packet::ref_from_bytes(bytes).unwrap(); | |
match (&packet.header.src_port, &[0, 1]) { | |
(left_val, right_val) => { | |
if !(*left_val == *right_val) { | |
let kind = ::core::panicking::AssertKind::Eq; | |
::core::panicking::assert_failed( | |
kind, | |
&*left_val, | |
&*right_val, | |
::core::option::Option::None, | |
); | |
} | |
} | |
}; | |
match (&packet.header.dst_port, &[2, 3]) { | |
(left_val, right_val) => { | |
if !(*left_val == *right_val) { | |
let kind = ::core::panicking::AssertKind::Eq; | |
::core::panicking::assert_failed( | |
kind, | |
&*left_val, | |
&*right_val, | |
::core::option::Option::None, | |
); | |
} | |
} | |
}; | |
match (&packet.header.length, &[4, 5]) { | |
(left_val, right_val) => { | |
if !(*left_val == *right_val) { | |
let kind = ::core::panicking::AssertKind::Eq; | |
::core::panicking::assert_failed( | |
kind, | |
&*left_val, | |
&*right_val, | |
::core::option::Option::None, | |
); | |
} | |
} | |
}; | |
match (&packet.header.checksum, &[6, 7]) { | |
(left_val, right_val) => { | |
if !(*left_val == *right_val) { | |
let kind = ::core::panicking::AssertKind::Eq; | |
::core::panicking::assert_failed( | |
kind, | |
&*left_val, | |
&*right_val, | |
::core::option::Option::None, | |
); | |
} | |
} | |
}; | |
match (&packet.body, &[8, 9, 10, 11]) { | |
(left_val, right_val) => { | |
if !(*left_val == *right_val) { | |
let kind = ::core::panicking::AssertKind::Eq; | |
::core::panicking::assert_failed( | |
kind, | |
&*left_val, | |
&*right_val, | |
::core::option::Option::None, | |
); | |
} | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use zerocopy::*; | |
#[derive(FromBytes, KnownLayout, Immutable)] | |
#[repr(C)] | |
struct PacketHeader { | |
src_port: [u8; 2], | |
dst_port: [u8; 2], | |
length: [u8; 2], | |
checksum: [u8; 2], | |
} | |
#[derive(FromBytes, KnownLayout, Immutable)] | |
#[repr(C)] | |
struct Packet { | |
header: PacketHeader, | |
body: [u8], | |
} | |
fn main() { | |
let bytes = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11][..]; | |
let packet = Packet::ref_from_bytes(bytes).unwrap(); | |
assert_eq!(packet.header.src_port, [0, 1]); | |
assert_eq!(packet.header.dst_port, [2, 3]); | |
assert_eq!(packet.header.length, [4, 5]); | |
assert_eq!(packet.header.checksum, [6, 7]); | |
assert_eq!(packet.body, [8, 9, 10, 11]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment