Rollup merge of #114519 - the8472:dirent-offset-of, r=dtolnay

use offset_of! to calculate dirent64 field offsets

r? `@dtolnay`
This commit is contained in:
Matthias Krüger 2023-08-06 17:26:30 +02:00 committed by GitHub
commit 3d1c36e917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 25 deletions

View File

@ -298,6 +298,7 @@
#![feature(maybe_uninit_slice)] #![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array)] #![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_write_slice)] #![feature(maybe_uninit_write_slice)]
#![feature(offset_of)]
#![feature(panic_can_unwind)] #![feature(panic_can_unwind)]
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(panic_internals)] #![feature(panic_internals)]

View File

@ -7,17 +7,6 @@ use crate::ffi::{CStr, OsStr, OsString};
use crate::fmt; use crate::fmt;
use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom}; use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
use crate::mem; use crate::mem;
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "solaris",
target_os = "fuchsia",
target_os = "redox",
target_os = "illumos",
target_os = "nto",
target_os = "vita",
))]
use crate::mem::MaybeUninit;
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd}; use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
use crate::path::{Path, PathBuf}; use crate::path::{Path, PathBuf};
use crate::ptr; use crate::ptr;
@ -712,22 +701,10 @@ impl Iterator for ReadDir {
// requires the full extent of *entry_ptr to be in bounds of the same // requires the full extent of *entry_ptr to be in bounds of the same
// allocation, which is not necessarily the case here. // allocation, which is not necessarily the case here.
// //
// Absent any other way to obtain a pointer to `(*entry_ptr).d_name` // Instead we must access fields individually through their offsets.
// legally in Rust analogously to how it would be done in C, we instead
// need to make our own non-libc allocation that conforms to the weird
// imaginary definition of dirent64, and use that for a field offset
// computation.
macro_rules! offset_ptr { macro_rules! offset_ptr {
($entry_ptr:expr, $field:ident) => {{ ($entry_ptr:expr, $field:ident) => {{
const OFFSET: isize = { const OFFSET: isize = mem::offset_of!(dirent64, $field) as isize;
let delusion = MaybeUninit::<dirent64>::uninit();
let entry_ptr = delusion.as_ptr();
unsafe {
ptr::addr_of!((*entry_ptr).$field)
.cast::<u8>()
.offset_from(entry_ptr.cast::<u8>())
}
};
if true { if true {
// Cast to the same type determined by the else branch. // Cast to the same type determined by the else branch.
$entry_ptr.byte_offset(OFFSET).cast::<_>() $entry_ptr.byte_offset(OFFSET).cast::<_>()