Skip to content

Commit

Permalink
Add optional zerocopy trait derives
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Oct 27, 2023
1 parent 0644a87 commit c038816
Show file tree
Hide file tree
Showing 152 changed files with 1,307 additions and 15 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.16", optional = true, features = ["derive"] }

[features]
default = ["std"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ newer Rust features are only available on newer Rust toolchains:
| `core::ffi::c_void` | 1.30.0 |
| `repr(packed(N))` | 1.33.0 |
| `cfg(target_vendor)` | 1.33.0 |
| `zerocopy` | 1.61.0 |
| `const-extern-fn` | 1.62.0 |

## Platform support
Expand Down
20 changes: 16 additions & 4 deletions ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ if [ "${TOOLCHAIN}" = "nightly" ] ; then
rustup component add rust-src
fi

version_gte() {
# Adapted from https://stackoverflow.com/a/4024263/836390
[ "$2" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}

test_target() {
BUILD_CMD="${1}"
TARGET="${2}"
Expand Down Expand Up @@ -66,15 +71,22 @@ test_target() {
--target "${TARGET}" --features extra_traits
fi

# Test the 'const-extern-fn' feature on nightly
if [ "${RUST}" = "nightly" ]; then
# Test the 'const-extern-fn' feature on nightly and the 'zerocopy' feature
# any version starting with zerocopy's MSRV (1.61.0).
if [ "${RUST}" = "nightly" ] || version_gte "${RUST}" "1.61.0"; then
if [ "${RUST}" = "nightly" ]; then
FEATURES="const-extern-fn,zerocopy"
else
FEATURES="zerocopy"
fi

if [ "${NO_STD}" != "1" ]; then
cargo "+${RUST}" "${BUILD_CMD}" -vv --no-default-features --target "${TARGET}" \
--features const-extern-fn
--features "$FEATURES"
else
RUSTFLAGS="-A improper_ctypes_definitions" cargo "+${RUST}" "${BUILD_CMD}" \
-Z build-std=core,alloc -vv --no-default-features \
--target "${TARGET}" --features const-extern-fn
--target "${TARGET}" --features "$FEATURES"
fi
fi

Expand Down
4 changes: 2 additions & 2 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ if [ "$TARGET" = "s390x-unknown-linux-gnu" ]; then
continue
fi
elif [ "$passed" = "2" ]; then
if cargo test --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then
if cargo test --features extra_traits,zerocopy --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then
break
fi
fi
Expand All @@ -114,6 +114,6 @@ else

cargo test --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}

RUST_BACKTRACE=1 cargo test --features extra_traits --manifest-path libc-test/Cargo.toml \
RUST_BACKTRACE=1 cargo test --features extra_traits,zerocopy --manifest-path libc-test/Cargo.toml \
--target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
fi
1 change: 1 addition & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default = ["std"]
std = ["libc/std"]
align = ["libc/align"]
extra_traits = ["libc/extra_traits"]
zerocopy = ["libc/zerocopy"]

[[test]]
name = "main"
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
Loading

0 comments on commit c038816

Please sign in to comment.