Repository: tokio-rs/tokio
File: tokio/src/fs/read_dir.rs (Rust)

Code before:
                    let (mut buf, mut std) = data.take().unwrap();

                    self.0 = State::Pending(spawn_blocking(move || {
                        buf.extend(ReadDir::next_chunk(&mut std));
                        (buf, std)
                    }));
                }
                State::Pending(ref mut rx) => {
                    let (mut buf, std) = ready!(Pin::new(rx).poll(cx))?;

                    let ret = match buf.pop_front() {
                        Some(Ok(x)) => Ok(Some(x)),
                        Some(Err(e)) => Err(e),
                        None => Ok(None),
                    };

                    self.0 = State::Idle(Some((buf, std)));

                    return Poll::Ready(ret);
                }
            }
        }
    }

    fn next_chunk(std: &mut std::fs::ReadDir) -> Vec<io::Result<DirEntry>> {
        let mut buf = Vec::with_capacity(CHUNK_SIZE);

        for ret in std.by_ref().take(CHUNK_SIZE) {
            let success = ret.is_ok();

            buf.push(ret.map(|std| DirEntry {
                #[cfg(not(any(
                    target_os = "solaris",
                    target_os = "illumos",
                    target_os = "haiku",
                    target_os = "vxworks"
                )))]
                file_type: std.file_type().ok(),
                std: Arc::new(std),
            }));

            if !success {
                break;
            }
        }

        buf
    }
}

feature! {

Review comment (suggestion):
Creating a new `Vec` each time invalidates the point behind reusing the `VecDeque`. You should have `next_chunk` take a mutable reference to the `VecDeque` and append directly to it instead.

Code after:

                    let (mut buf, mut std) = data.take().unwrap();

                    self.0 = State::Pending(spawn_blocking(move || {
                        ReadDir::next_chunk(&mut buf, &mut std);
                        (buf, std)
                    }));
                }
                State::Pending(ref mut rx) => {
                    let (mut buf, std) = ready!(Pin::new(rx).poll(cx))?;

                    let ret = match buf.pop_front() {
                        Some(Ok(x)) => Ok(Some(x)),
                        Some(Err(e)) => Err(e),
                        None => Ok(None),
                    };

                    self.0 = State::Idle(Some((buf, std)));

                    return Poll::Ready(ret);
                }
            }
        }
    }

    fn next_chunk(buf: &mut VecDeque<io::Result<DirEntry>>, std: &mut std::fs::ReadDir) {
        for ret in std.by_ref().take(CHUNK_SIZE) {
            let success = ret.is_ok();

            buf.push_back(ret.map(|std| DirEntry {
                #[cfg(not(any(
                    target_os = "solaris",
                    target_os = "illumos",
                    target_os = "haiku",
                    target_os = "vxworks"
                )))]
                file_type: std.file_type().ok(),
                std: Arc::new(std),
            }));

            if !success {
                break;
            }
        }
    }
}

feature! {
    #![unix]

    use std::os::unix::fs::DirEntryExt;