Skip to content

Commit 5ddef54

Browse files
committed
Auto merge of rust-lang#77674 - cuviper:direntry-diet, r=dtolnay
unix/vxworks: make DirEntry slightly smaller `DirEntry` contains a `ReadDir` handle, which used to just be a wrapper on `Arc<InnerReadDir>`. Commit af75314 added `end_of_stream: bool` which is not needed by `DirEntry`, but adds 8 bytes after padding. We can let `DirEntry` have an `Arc<InnerReadDir>` directly to avoid that.
2 parents 7b06cb1 + 1d06b07 commit 5ddef54

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

+21-7
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,14 @@ struct InnerReadDir {
183183
root: PathBuf,
184184
}
185185

186-
#[derive(Clone)]
187186
pub struct ReadDir {
188187
inner: Arc<InnerReadDir>,
188+
#[cfg(not(any(
189+
target_os = "solaris",
190+
target_os = "illumos",
191+
target_os = "fuchsia",
192+
target_os = "redox",
193+
)))]
189194
end_of_stream: bool,
190195
}
191196

@@ -196,7 +201,7 @@ unsafe impl Sync for Dir {}
196201

197202
pub struct DirEntry {
198203
entry: dirent64,
199-
dir: ReadDir,
204+
dir: Arc<InnerReadDir>,
200205
// We need to store an owned copy of the entry name
201206
// on Solaris and Fuchsia because a) it uses a zero-length
202207
// array to store the name, b) its lifetime between readdir
@@ -443,7 +448,7 @@ impl Iterator for ReadDir {
443448
name: slice::from_raw_parts(name as *const u8, namelen as usize)
444449
.to_owned()
445450
.into_boxed_slice(),
446-
dir: self.clone(),
451+
dir: Arc::clone(&self.inner),
447452
};
448453
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
449454
return Some(Ok(ret));
@@ -464,7 +469,7 @@ impl Iterator for ReadDir {
464469
}
465470

466471
unsafe {
467-
let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
472+
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
468473
let mut entry_ptr = ptr::null_mut();
469474
loop {
470475
if readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
@@ -497,7 +502,7 @@ impl Drop for Dir {
497502

498503
impl DirEntry {
499504
pub fn path(&self) -> PathBuf {
500-
self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
505+
self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
501506
}
502507

503508
pub fn file_name(&self) -> OsString {
@@ -506,7 +511,7 @@ impl DirEntry {
506511

507512
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "android"))]
508513
pub fn metadata(&self) -> io::Result<FileAttr> {
509-
let fd = cvt(unsafe { dirfd(self.dir.inner.dirp.0) })?;
514+
let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?;
510515
let name = self.entry.d_name.as_ptr();
511516

512517
cfg_has_statx! {
@@ -944,7 +949,16 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
944949
Err(Error::last_os_error())
945950
} else {
946951
let inner = InnerReadDir { dirp: Dir(ptr), root };
947-
Ok(ReadDir { inner: Arc::new(inner), end_of_stream: false })
952+
Ok(ReadDir {
953+
inner: Arc::new(inner),
954+
#[cfg(not(any(
955+
target_os = "solaris",
956+
target_os = "illumos",
957+
target_os = "fuchsia",
958+
target_os = "redox",
959+
)))]
960+
end_of_stream: false,
961+
})
948962
}
949963
}
950964
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct InnerReadDir {
2727
root: PathBuf,
2828
}
2929

30-
#[derive(Clone)]
3130
pub struct ReadDir {
3231
inner: Arc<InnerReadDir>,
3332
end_of_stream: bool,
@@ -40,7 +39,7 @@ unsafe impl Sync for Dir {}
4039

4140
pub struct DirEntry {
4241
entry: dirent,
43-
dir: ReadDir,
42+
dir: Arc<InnerReadDir>,
4443
}
4544

4645
#[derive(Clone, Debug)]
@@ -170,7 +169,7 @@ impl Iterator for ReadDir {
170169
}
171170

172171
unsafe {
173-
let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
172+
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
174173
let mut entry_ptr = ptr::null_mut();
175174
loop {
176175
if readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
@@ -204,7 +203,7 @@ impl Drop for Dir {
204203
impl DirEntry {
205204
pub fn path(&self) -> PathBuf {
206205
use crate::sys::vxworks::ext::ffi::OsStrExt;
207-
self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
206+
self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
208207
}
209208

210209
pub fn file_name(&self) -> OsString {

0 commit comments

Comments
 (0)