Skip to content

Commit 3d1c36e

Browse files
authored
Rollup merge of #114519 - the8472:dirent-offset-of, r=dtolnay
use offset_of! to calculate dirent64 field offsets r? `@dtolnay`
2 parents 13de583 + 20c25d6 commit 3d1c36e

File tree

2 files changed

+3
-25
lines changed

2 files changed

+3
-25
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
#![feature(maybe_uninit_slice)]
299299
#![feature(maybe_uninit_uninit_array)]
300300
#![feature(maybe_uninit_write_slice)]
301+
#![feature(offset_of)]
301302
#![feature(panic_can_unwind)]
302303
#![feature(panic_info_message)]
303304
#![feature(panic_internals)]

library/std/src/sys/unix/fs.rs

+2-25
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ use crate::ffi::{CStr, OsStr, OsString};
77
use crate::fmt;
88
use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
99
use crate::mem;
10-
#[cfg(any(
11-
target_os = "android",
12-
target_os = "linux",
13-
target_os = "solaris",
14-
target_os = "fuchsia",
15-
target_os = "redox",
16-
target_os = "illumos",
17-
target_os = "nto",
18-
target_os = "vita",
19-
))]
20-
use crate::mem::MaybeUninit;
2110
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
2211
use crate::path::{Path, PathBuf};
2312
use crate::ptr;
@@ -712,22 +701,10 @@ impl Iterator for ReadDir {
712701
// requires the full extent of *entry_ptr to be in bounds of the same
713702
// allocation, which is not necessarily the case here.
714703
//
715-
// Absent any other way to obtain a pointer to `(*entry_ptr).d_name`
716-
// legally in Rust analogously to how it would be done in C, we instead
717-
// need to make our own non-libc allocation that conforms to the weird
718-
// imaginary definition of dirent64, and use that for a field offset
719-
// computation.
704+
// Instead we must access fields individually through their offsets.
720705
macro_rules! offset_ptr {
721706
($entry_ptr:expr, $field:ident) => {{
722-
const OFFSET: isize = {
723-
let delusion = MaybeUninit::<dirent64>::uninit();
724-
let entry_ptr = delusion.as_ptr();
725-
unsafe {
726-
ptr::addr_of!((*entry_ptr).$field)
727-
.cast::<u8>()
728-
.offset_from(entry_ptr.cast::<u8>())
729-
}
730-
};
707+
const OFFSET: isize = mem::offset_of!(dirent64, $field) as isize;
731708
if true {
732709
// Cast to the same type determined by the else branch.
733710
$entry_ptr.byte_offset(OFFSET).cast::<_>()

0 commit comments

Comments
 (0)