Skip to content

Commit c1297ec

Browse files
committed
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.
1 parent 53a4c3b commit c1297ec

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

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

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

186-
#[derive(Clone)]
187186
pub struct ReadDir {
188187
inner: Arc<InnerReadDir>,
189188
end_of_stream: bool,
@@ -196,7 +195,7 @@ unsafe impl Sync for Dir {}
196195

197196
pub struct DirEntry {
198197
entry: dirent64,
199-
dir: ReadDir,
198+
dir: Arc<InnerReadDir>,
200199
// We need to store an owned copy of the entry name
201200
// on Solaris and Fuchsia because a) it uses a zero-length
202201
// array to store the name, b) its lifetime between readdir
@@ -443,7 +442,7 @@ impl Iterator for ReadDir {
443442
name: slice::from_raw_parts(name as *const u8, namelen as usize)
444443
.to_owned()
445444
.into_boxed_slice(),
446-
dir: self.clone(),
445+
dir: Arc::clone(&self.inner),
447446
};
448447
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
449448
return Some(Ok(ret));
@@ -464,7 +463,7 @@ impl Iterator for ReadDir {
464463
}
465464

466465
unsafe {
467-
let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
466+
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
468467
let mut entry_ptr = ptr::null_mut();
469468
loop {
470469
if readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
@@ -497,7 +496,7 @@ impl Drop for Dir {
497496

498497
impl DirEntry {
499498
pub fn path(&self) -> PathBuf {
500-
self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
499+
self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
501500
}
502501

503502
pub fn file_name(&self) -> OsString {
@@ -506,7 +505,7 @@ impl DirEntry {
506505

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

512511
cfg_has_statx! {

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)