Repository: starship/starship
File: src/context.rs (Rust)

Code before:
        follow_symlinks: bool,
    ) -> Result<Self, std::io::Error> {
        let _ = fs::read_dir(base)?; // Early return if invalid base

        let start = Instant::now();
        let mut remaining_time = timeout;

        let mut folders: HashSet<PathBuf> = HashSet::new();
        let mut files: HashSet<PathBuf> = HashSet::new();
        let mut file_names: HashSet<String> = HashSet::new();
        let mut extensions: HashSet<String> = HashSet::new();

        let base_path = base;
        let base = Arc::from(base);
        let (tx, rx) = mpsc::channel();

        {
            let worker = move || {
                let timeout = &timeout;
                let enumerated_dir = fs::read_dir(base).unwrap().enumerate();
                for entry in enumerated_dir
                    .take_while(|(n, _)| {
                        cfg!(test) // ignore timeout during tests
                        || n & 0xFF != 0 // only check timeout once every 2^8 entries
                        || start.elapsed() < *timeout
                    })
                    .filter_map(|(_, entry)| entry.ok())
                {
                    match tx.send(entry) {
                        Ok(_) => continue,
                        Err(e) => {
                            log::warn!("Error occurred while sending across mpsc: {e}");
                            break;
                        }
                    }
                }
            };

            let _ = thread::Builder::new()
                .name("from_path_with_timeout worker".into())
                .spawn(worker)?;
        }

        loop {
            match rx.recv_timeout(remaining_time) {
                Ok(entry) => {
                    let path = PathBuf::from(entry.path().strip_prefix(base_path).unwrap());

                    let is_dir = match follow_symlinks {
                        true => entry.path().is_dir(),
                        false => fs::symlink_metadata(entry.path())

Review comment (suggestion):
Lets just end on `tx.send` error instead to avoid superfluous checks (thus please also remove the `log`).

Code after:
        follow_symlinks: bool,
    ) -> Result<Self, std::io::Error> {
        let _ = fs::read_dir(base)?; // Early return if invalid base

        let start = Instant::now();
        let mut remaining_time = timeout;

        let mut folders: HashSet<PathBuf> = HashSet::new();
        let mut files: HashSet<PathBuf> = HashSet::new();
        let mut file_names: HashSet<String> = HashSet::new();
        let mut extensions: HashSet<String> = HashSet::new();

        let base_path = base;
        let base = Arc::from(base);
        let (tx, rx) = mpsc::channel();

        {
            let worker = move || {
                let enumerated_dir = fs::read_dir(base).unwrap().enumerate();
                let _ = enumerated_dir
                    .filter_map(|(_, entry)| entry.ok())
                    .try_for_each(|entry| tx.send(entry));
            };

            let _ = thread::Builder::new()
                .name("from_path_with_timeout worker".into())
                .spawn(worker)?;
        }

        loop {
            // TODO: use `recv_deadline` instead once stable
            let msg = if cfg!(test) {
                // recv() errors out only when the corresponding sender closes.
                // See mpsc::RecvError.
                rx.recv().map_err(|_| mpsc::RecvTimeoutError::Disconnected)
            } else {
                rx.recv_timeout(remaining_time)
            };
            match msg {
                Ok(entry) => {
                    let path = PathBuf::from(entry.path().strip_prefix(base_path).unwrap());

                    let is_dir = match follow_symlinks {
                        true => entry.path().is_dir(),
                        false => fs::symlink_metadata(entry.path())
                            .map(|m| m.is_dir())
                            .unwrap_or(false),
                    };

                    if is_dir {
                        folders.insert(path);