Skip to content

Commit

Permalink
Add optional zerocopy trait derives
Browse files Browse the repository at this point in the history
TODO: Make use of this new feature in tests
  • Loading branch information
joshlf committed Oct 25, 2023
1 parent 43f0c3e commit 9dd0f1b
Show file tree
Hide file tree
Showing 31 changed files with 363 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ cargo-args = ["-Zbuild-std=core"]

[dependencies]
rustc-std-workspace-core = { version = "1.0.0", optional = true }
zerocopy = { version = "0.7.15", optional = true, features = ["derive"] }

[features]
default = ["std"]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]
#![cfg_attr(libc_const_extern_fn_unstable, feature(const_extern_fn))]

#[cfg(feature = "zerocopy")]
#[macro_use]
extern crate zerocopy;

#[macro_use]
mod macros;

Expand Down
39 changes: 39 additions & 0 deletions src/unix/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,29 @@ e! {
}

s! {
#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct fsid_t {
pub val: [::c_uint; 2],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct fsid64_t {
pub val: [::uint64_t; 2],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct timezone {
pub tz_minuteswest: ::c_int,
pub tz_dsttime: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct ip_mreq {
pub imr_multiaddr: in_addr,
pub imr_interface: in_addr,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct dirent {
pub d_offset: ::c_ulong,
pub d_ino: ::ino_t,
Expand All @@ -97,6 +102,7 @@ s! {
pub d_name: [::c_char; 256]
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct termios {
pub c_iflag: ::tcflag_t,
pub c_oflag: ::tcflag_t,
Expand All @@ -105,6 +111,7 @@ s! {
pub c_cc: [::cc_t; ::NCCS]
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct flock64 {
pub l_type: ::c_short,
pub l_whence: ::c_short,
Expand All @@ -125,6 +132,7 @@ s! {
pub msg_flags: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct statvfs64 {
pub f_bsize: ::blksize64_t,
pub f_frsize: ::blksize64_t,
Expand Down Expand Up @@ -171,6 +179,7 @@ s! {
pub int_n_sign_posn: ::c_char,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct tm {
pub tm_sec: ::c_int,
pub tm_min: ::c_int,
Expand All @@ -195,22 +204,26 @@ s! {
pub ai_eflags: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct in_addr {
pub s_addr: in_addr_t
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct ip_mreq_source {
pub imr_multiaddr: in_addr,
pub imr_sourceaddr: in_addr,
pub imr_interface: in_addr,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr {
pub sa_len: ::c_uchar,
pub sa_family: sa_family_t,
pub sa_data: [::c_char; 14],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr_dl {
pub sdl_len: ::c_uchar,
pub sdl_family: ::c_uchar,
Expand All @@ -222,6 +235,7 @@ s! {
pub sdl_data: [::c_char; 120],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr_in {
pub sin_len: ::c_uchar,
pub sin_family: sa_family_t,
Expand All @@ -230,6 +244,7 @@ s! {
pub sin_zero: [::c_char; 8]
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr_in6 {
pub sin6_len: ::c_uchar,
pub sin6_family: ::c_uchar,
Expand All @@ -239,6 +254,7 @@ s! {
pub sin6_scope_id: ::uint32_t
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr_storage {
pub __ss_len: ::c_uchar,
pub ss_family: sa_family_t,
Expand All @@ -247,17 +263,20 @@ s! {
__ss_pad2: [::c_char; 1265],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sockaddr_un {
pub sun_len: ::c_uchar,
pub sun_family: sa_family_t,
pub sun_path: [::c_char; 1023]
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct st_timespec {
pub tv_sec: ::time_t,
pub tv_nsec: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct statfs64 {
pub f_version: ::c_int,
pub f_type: ::c_int,
Expand Down Expand Up @@ -289,6 +308,7 @@ s! {
pub pw_shell: *mut ::c_char
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct utsname {
pub sysname: [::c_char; 32],
pub nodename: [::c_char; 32],
Expand All @@ -297,12 +317,14 @@ s! {
pub machine: [::c_char; 32],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct xutsname {
pub nid: ::c_uint,
pub reserved: ::c_int,
pub longnid: ::c_ulonglong,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct cmsghdr {
pub cmsg_len: ::socklen_t,
pub cmsg_level: ::c_int,
Expand All @@ -318,10 +340,12 @@ s! {
}

// Should be union with another 'sival_int'
#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sigval64 {
pub sival_ptr: ::c_ulonglong,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sigevent64 {
pub sigev_value: sigval64,
pub sigev_signo: ::c_int,
Expand All @@ -335,6 +359,7 @@ s! {
pub sevt_signo: signal_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct poll_ctl {
pub cmd: ::c_short,
pub events: ::c_short,
Expand All @@ -353,11 +378,13 @@ s! {
pub bytes_sent: ::uint64_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct mmsghdr {
pub msg_hdr: ::msghdr,
pub msg_len: ::c_uint,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sched_param {
pub sched_priority: ::c_int,
pub sched_policy: ::c_int,
Expand All @@ -371,6 +398,7 @@ s! {
pub __pad: [::c_int; 4],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct posix_spawnattr_t {
pub posix_attr_flags: ::c_short,
pub posix_attr_pgroup: ::pid_t,
Expand All @@ -388,6 +416,7 @@ s! {
pub gl_ptx: *mut ::c_void,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct mallinfo {
pub arena: ::c_ulong,
pub ordblks: ::c_int,
Expand All @@ -401,11 +430,13 @@ s! {
pub keepcost: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct utmp_exit_status {
pub e_termination: ::c_short,
pub e_exit: ::c_short,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct utmp {
pub ut_user: [::c_char; 256],
pub ut_id: [::c_char; 14],
Expand All @@ -420,6 +451,7 @@ s! {
pub __reservedV: [::c_int; 6],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct regmatch_t {
pub rm_so: regoff_t,
pub rm_eo: regoff_t,
Expand All @@ -439,11 +471,13 @@ s! {
pub __unused: [*mut ::c_void; 34],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct rlimit64 {
pub rlim_cur: rlim64_t,
pub rlim_max: rlim64_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct shmid_ds {
pub shm_perm: ipc_perm,
pub shm_segsz: ::size_t,
Expand All @@ -462,6 +496,7 @@ s! {
pub shm_reserved1: ::int64_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct stat64 {
pub st_dev: dev_t,
pub st_ino: ino_t,
Expand Down Expand Up @@ -494,6 +529,7 @@ s! {
pub mnt_passno: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct ipc_perm {
pub uid: ::uid_t,
pub gid: ::gid_t,
Expand All @@ -510,13 +546,15 @@ s! {
pub data: *mut ::c_void,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct mq_attr {
pub mq_flags: ::c_long,
pub mq_maxmsg: ::c_long,
pub mq_msgsize: ::c_long,
pub mq_curmsgs: ::c_long,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sembuf {
pub sem_num: ::c_ushort,
pub sem_op: ::c_short,
Expand All @@ -528,6 +566,7 @@ s! {
pub if_name: *mut ::c_char,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct itimerspec {
pub it_interval: ::timespec,
pub it_value: ::timespec,
Expand Down
14 changes: 14 additions & 0 deletions src/unix/aix/powerpc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ pub type c_long = i64;
pub type c_ulong = u64;

s! {
#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct sigset_t {
pub ss_set: [c_ulong; 4],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct fd_set {
pub fds_bits: [c_long; 1024],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct flock {
pub l_type: ::c_short,
pub l_whence: ::c_short,
Expand All @@ -20,6 +23,7 @@ s! {
pub l_len: ::off_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct statvfs {
pub f_bsize: ::c_ulong,
pub f_frsize: ::c_ulong,
Expand All @@ -37,18 +41,22 @@ s! {
pub f_filler: [::c_ulong; 16]
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct pthread_rwlock_t {
__rw_word: [::c_long; 10],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct pthread_cond_t {
__cv_word: [::c_long; 6],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct pthread_mutex_t {
__mt_word: [::c_long; 8],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct stat {
pub st_dev: ::dev_t,
pub st_ino: ::ino_t,
Expand All @@ -73,6 +81,7 @@ s! {
pub st_size: ::off_t,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct statfs {
pub f_version: ::c_int,
pub f_type: ::c_int,
Expand Down Expand Up @@ -125,6 +134,7 @@ s! {
pub __pad: [::c_int; 1],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct mcontext_t {
pub gpr: [::c_ulonglong; 32],
pub msr: ::c_ulonglong,
Expand All @@ -145,6 +155,7 @@ s! {
pub excp_type: ::c_int,
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct utmpx {
pub ut_user: [::c_char; 256],
pub ut_id: [::c_char; 14],
Expand All @@ -158,14 +169,17 @@ s! {
pub __reservedV: [::c_int; 6],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct pthread_spinlock_t {
pub __sp_word: [::c_long; 3],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct pthread_barrier_t {
pub __br_word: [::c_long; 5],
}

#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
pub struct msqid_ds {
pub msg_perm: ::ipc_perm,
pub msg_first: ::c_uint,
Expand Down
1 change: 1 addition & 0 deletions src/unix/align.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
s! {
#[cfg_attr(feature = "zerocopy", derive(FromZeroes, FromBytes, AsBytes))]
#[repr(align(4))]
pub struct in6_addr {
pub s6_addr: [u8; 16],
Expand Down
Loading

0 comments on commit 9dd0f1b

Please sign in to comment.