diff --git a/src/fs/raw_dir.rs b/src/fs/raw_dir.rs index 7d866abdf..6bd93da95 100644 --- a/src/fs/raw_dir.rs +++ b/src/fs/raw_dir.rs @@ -45,7 +45,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> { /// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap(); /// /// let mut buf = Vec::with_capacity(8192); - /// for entry in RawDir::new(fd, buf.spare_capacity_mut()) { + /// let mut iter = RawDir::new(fd, buf.spare_capacity_mut()); + /// while let Some(entry) = iter.next() { /// let entry = entry.unwrap(); /// dbg!(&entry); /// } @@ -60,7 +61,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> { /// let fd = openat(cwd(), ".", OFlags::RDONLY | OFlags::DIRECTORY, Mode::empty()).unwrap(); /// /// let mut buf = [MaybeUninit::uninit(); 2048]; - /// for entry in RawDir::new(fd, &mut buf) { + /// let mut iter = RawDir::new(fd, &mut buf); + /// while let Some(entry) = iter.next() { /// let entry = entry.unwrap(); /// dbg!(&entry); /// } @@ -82,7 +84,8 @@ impl<'buf, Fd: AsFd> RawDir<'buf, Fd> { /// let mut buf = Vec::with_capacity(8192); /// 'read: loop { /// 'resize: { - /// for entry in RawDir::new(&fd, buf.spare_capacity_mut()) { + /// let mut iter = RawDir::new(&fd, buf.spare_capacity_mut()); + /// while let Some(entry) = iter.next() { /// let entry = match entry { /// Err(Errno::INVAL) => break 'resize, /// r => r.unwrap(), @@ -162,11 +165,13 @@ impl<'a> RawDirEntry<'a> { } } -impl<'buf, Fd: AsFd> Iterator for RawDir<'buf, Fd> { - type Item = io::Result>; - +impl<'buf, Fd: AsFd> RawDir<'buf, Fd> { + /// Identical to [Iterator::next] except that [Iterator::Item] borrows from self. + /// + /// Note: this interface will be broken to implement a stdlib iterator API with + /// GAT support once one becomes available. #[allow(unsafe_code)] - fn next(&mut self) -> Option { + pub fn next(&mut self) -> Option> { loop { if self.offset < self.initialized { let dirent_ptr = self.buf[self.offset..].as_ptr(); diff --git a/tests/fs/readdir.rs b/tests/fs/readdir.rs index 0105ba4b9..97578a3c5 100644 --- a/tests/fs/readdir.rs +++ b/tests/fs/readdir.rs @@ -56,16 +56,15 @@ fn read_entries(dir: &mut Dir) -> HashMap { #[cfg(any(target_os = "android", target_os = "linux"))] fn test_raw_dir(buf: &mut [MaybeUninit]) { + use std::collections::HashSet; use std::io::{Seek, SeekFrom}; use rustix::fd::AsFd; - use rustix::fs::{RawDir, RawDirEntry}; + use rustix::fs::RawDir; - fn read_raw_entries<'a, Fd: AsFd>( - dir: &'a mut RawDir<'_, Fd>, - ) -> HashMap> { - let mut out = HashMap::new(); - for entry in dir { + fn read_raw_entries(dir: &mut RawDir) -> HashSet { + let mut out = HashSet::new(); + while let Some(entry) = dir.next() { let entry = entry.expect("non-error entry"); let name = entry .file_name() @@ -73,7 +72,7 @@ fn test_raw_dir(buf: &mut [MaybeUninit]) { .expect("utf8 filename") .to_owned(); if name != "." && name != ".." { - out.insert(name, entry); + out.insert(name); } } out