Skip to content

Commit d9983ce

Browse files
committed
Add optional zerocopy trait derives
1 parent a0f5b4b commit d9983ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+1312
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ cargo-args = ["-Zbuild-std=core"]
132132

133133
[dependencies]
134134
rustc-std-workspace-core = { version = "1.0.0", optional = true }
135+
zerocopy = { version = "0.7.16", optional = true, features = ["derive"] }
135136

136137
[features]
137138
default = ["std"]

ci/build.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ if [ "${TOOLCHAIN}" = "nightly" ] ; then
1818
rustup component add rust-src
1919
fi
2020

21+
version_gte() {
22+
# Adapted from https://stackoverflow.com/a/4024263/836390
23+
[ "$2" = "$(printf '%s\n%s' "$1" "$2" | sort -V | head -n1)" ]
24+
}
25+
2126
test_target() {
2227
BUILD_CMD="${1}"
2328
TARGET="${2}"
@@ -66,15 +71,22 @@ test_target() {
6671
--target "${TARGET}" --features extra_traits
6772
fi
6873

69-
# Test the 'const-extern-fn' feature on nightly
70-
if [ "${RUST}" = "nightly" ]; then
74+
# Test the 'const-extern-fn' feature on nightly and the 'zerocopy' feature
75+
# any version starting with zerocopy's MSRV (1.61.0).
76+
if [ "${RUST}" = "nightly" ] || version_gte "${RUST}" "1.61.0"; then
77+
if [ "${RUST}" = "nightly" ]; then
78+
FEATURES="const-extern-fn,zerocopy"
79+
else
80+
FEATURES="zerocopy"
81+
fi
82+
7183
if [ "${NO_STD}" != "1" ]; then
7284
cargo "+${RUST}" "${BUILD_CMD}" -vv --no-default-features --target "${TARGET}" \
73-
--features const-extern-fn
85+
--features "$FEATURES"
7486
else
7587
RUSTFLAGS="-A improper_ctypes_definitions" cargo "+${RUST}" "${BUILD_CMD}" \
7688
-Z build-std=core,alloc -vv --no-default-features \
77-
--target "${TARGET}" --features const-extern-fn
89+
--target "${TARGET}" --features "$FEATURES"
7890
fi
7991
fi
8092

ci/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ if [ "$TARGET" = "s390x-unknown-linux-gnu" ]; then
101101
continue
102102
fi
103103
elif [ "$passed" = "2" ]; then
104-
if cargo test --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then
104+
if cargo test --features extra_traits,zerocopy --manifest-path libc-test/Cargo.toml --target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}; then
105105
break
106106
fi
107107
fi
@@ -114,6 +114,6 @@ else
114114

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

117-
RUST_BACKTRACE=1 cargo test --features extra_traits --manifest-path libc-test/Cargo.toml \
117+
RUST_BACKTRACE=1 cargo test --features extra_traits,zerocopy --manifest-path libc-test/Cargo.toml \
118118
--target "${TARGET}" ${LIBC_CI_ZBUILD_STD+"-Zbuild-std"}
119119
fi

libc-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ default = ["std"]
2525
std = ["libc/std"]
2626
align = ["libc/align"]
2727
extra_traits = ["libc/extra_traits"]
28+
zerocopy = ["libc/zerocopy"]
2829

2930
[[test]]
3031
name = "main"

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#![cfg_attr(not(feature = "rustc-dep-of-std"), no_std)]
2424
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]
2525

26+
#[cfg(feature = "zerocopy")]
27+
extern crate zerocopy;
28+
2629
#[macro_use]
2730
mod macros;
2831

src/unix/aix/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,29 @@ e! {
7070
}
7171

7272
s! {
73+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
7374
pub struct fsid_t {
7475
pub val: [::c_uint; 2],
7576
}
7677

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

83+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
8184
pub struct timezone {
8285
pub tz_minuteswest: ::c_int,
8386
pub tz_dsttime: ::c_int,
8487
}
8588

89+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
8690
pub struct ip_mreq {
8791
pub imr_multiaddr: in_addr,
8892
pub imr_interface: in_addr,
8993
}
9094

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

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

113+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
107114
pub struct flock64 {
108115
pub l_type: ::c_short,
109116
pub l_whence: ::c_short,
@@ -124,6 +131,7 @@ s! {
124131
pub msg_flags: ::c_int,
125132
}
126133

134+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
127135
pub struct statvfs64 {
128136
pub f_bsize: ::blksize64_t,
129137
pub f_frsize: ::blksize64_t,
@@ -170,6 +178,7 @@ s! {
170178
pub int_n_sign_posn: ::c_char,
171179
}
172180

181+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
173182
pub struct tm {
174183
pub tm_sec: ::c_int,
175184
pub tm_min: ::c_int,
@@ -194,22 +203,26 @@ s! {
194203
pub ai_eflags: ::c_int,
195204
}
196205

206+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
197207
pub struct in_addr {
198208
pub s_addr: in_addr_t
199209
}
200210

211+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
201212
pub struct ip_mreq_source {
202213
pub imr_multiaddr: in_addr,
203214
pub imr_sourceaddr: in_addr,
204215
pub imr_interface: in_addr,
205216
}
206217

218+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
207219
pub struct sockaddr {
208220
pub sa_len: ::c_uchar,
209221
pub sa_family: sa_family_t,
210222
pub sa_data: [::c_char; 14],
211223
}
212224

225+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
213226
pub struct sockaddr_dl {
214227
pub sdl_len: ::c_uchar,
215228
pub sdl_family: ::c_uchar,
@@ -221,6 +234,7 @@ s! {
221234
pub sdl_data: [::c_char; 120],
222235
}
223236

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

246+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
232247
pub struct sockaddr_in6 {
233248
pub sin6_len: ::c_uchar,
234249
pub sin6_family: ::c_uchar,
@@ -238,6 +253,7 @@ s! {
238253
pub sin6_scope_id: ::uint32_t
239254
}
240255

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

265+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
249266
pub struct sockaddr_un {
250267
pub sun_len: ::c_uchar,
251268
pub sun_family: sa_family_t,
252269
pub sun_path: [::c_char; 1023]
253270
}
254271

272+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
255273
pub struct st_timespec {
256274
pub tv_sec: ::time_t,
257275
pub tv_nsec: ::c_int,
258276
}
259277

278+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
260279
pub struct statfs64 {
261280
pub f_version: ::c_int,
262281
pub f_type: ::c_int,
@@ -288,6 +307,7 @@ s! {
288307
pub pw_shell: *mut ::c_char
289308
}
290309

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

319+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
299320
pub struct xutsname {
300321
pub nid: ::c_uint,
301322
pub reserved: ::c_int,
302323
pub longnid: ::c_ulonglong,
303324
}
304325

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

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

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

361+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
337362
pub struct poll_ctl {
338363
pub cmd: ::c_short,
339364
pub events: ::c_short,
@@ -352,11 +377,13 @@ s! {
352377
pub bytes_sent: ::uint64_t,
353378
}
354379

380+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
355381
pub struct mmsghdr {
356382
pub msg_hdr: ::msghdr,
357383
pub msg_len: ::c_uint,
358384
}
359385

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

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

418+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
390419
pub struct mallinfo {
391420
pub arena: ::c_ulong,
392421
pub ordblks: ::c_int,
@@ -400,11 +429,13 @@ s! {
400429
pub keepcost: ::c_int,
401430
}
402431

432+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
403433
pub struct utmp_exit_status {
404434
pub e_termination: ::c_short,
405435
pub e_exit: ::c_short,
406436
}
407437

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

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

473+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
441474
pub struct rlimit64 {
442475
pub rlim_cur: rlim64_t,
443476
pub rlim_max: rlim64_t,
444477
}
445478

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

498+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
464499
pub struct stat64 {
465500
pub st_dev: dev_t,
466501
pub st_ino: ino_t,
@@ -493,6 +528,7 @@ s! {
493528
pub mnt_passno: ::c_int,
494529
}
495530

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

548+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
512549
pub struct mq_attr {
513550
pub mq_flags: ::c_long,
514551
pub mq_maxmsg: ::c_long,
515552
pub mq_msgsize: ::c_long,
516553
pub mq_curmsgs: ::c_long,
517554
}
518555

556+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
519557
pub struct sembuf {
520558
pub sem_num: ::c_ushort,
521559
pub sem_op: ::c_short,
@@ -527,6 +565,7 @@ s! {
527565
pub if_name: *mut ::c_char,
528566
}
529567

568+
#[cfg_attr(feature = "zerocopy", derive(zerocopy::FromZeroes, zerocopy::FromBytes, zerocopy::AsBytes))]
530569
pub struct itimerspec {
531570
pub it_interval: ::timespec,
532571
pub it_value: ::timespec,

0 commit comments

Comments
 (0)