Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional zerocopy trait derives #3407

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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"] }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a TODO for myself:

  • Rename to zerocopy-0-7
  • Add zerocopy-0-6 (to be removed before merge) to test that multiple version trains work

Since libc doesn't specify an edition, we have to use the legacy approach of importing macros via #[macro_use] extern crate zerocopy;, which may not play nicely with having macros from multiple versions of the same crate. Instead, we may need to update build.rs to detect when the Rust version is early enough that editions are supported, and instruct rustc to use a recent-enough version that we can explicitly import different versions under different names. This would be necessary in case a build requires multiple zerocopy versions simultaneously.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See for more discussion: google/zerocopy#557

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once rust-lang/libs-team#72 gets a conclusion, we can specify the edition.


[features]
default = ["std"]
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" = "$(printf '%s\n%s' "$1" "$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
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#![cfg_attr(not(feature = "rustc-dep-of-std"), no_std)]
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]

#[cfg(feature = "zerocopy")]
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 @@ -70,24 +70,29 @@ e! {
}

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

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

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

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

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

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

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

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

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

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

#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::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(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::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(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
pub struct sockaddr_dl {
pub sdl_len: ::c_uchar,
pub sdl_family: ::c_uchar,
Expand All @@ -221,6 +234,7 @@ s! {
pub sdl_data: [::c_char; 120],
}

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

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

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

#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::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(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
pub struct st_timespec {
pub tv_sec: ::time_t,
pub tv_nsec: ::c_int,
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::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(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
pub struct sembuf {
pub sem_num: ::c_ushort,
pub sem_op: ::c_short,
Expand All @@ -527,6 +565,7 @@ s! {
pub if_name: *mut ::c_char,
}

#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
pub struct itimerspec {
pub it_interval: ::timespec,
pub it_value: ::timespec,
Expand Down
Loading
Loading