The std::path::Path struct has some blocking methods like read_dir which should be asynchronous in our library.
That means we'll need to have our own async_std::path::Path that is convertible to/from std::path::Path. Moreover, since PathBuf derefs into Path, we'll also need an async version of PathBuf.
Right now, we're using std::path::Path throughout async-std, which might cause users to accidentally call blocking functions without realizing. For example:
let mut dir = fs::read_dir(".").await?;
while let Some(entry) = dir.next().await {
let path = entry?.path();
// Ooops, the `path.canonicalize()` call here is blocking!
//
// We should use an async version of `PathBuf` here and
// do `path.canonicalize().await` instead.
println!("{:?}", path.canonicalize()?);
}
path::PathBuf::as_pathpath::PathBuf::capacitypath::PathBuf::clearpath::PathBuf::into_boxed_pathpath::PathBuf::into_os_stringpath::PathBuf::newpath::PathBuf::poppath::PathBuf::pushpath::PathBuf::reservepath::PathBuf::reserve_exactpath::PathBuf::set_extensionpath::PathBuf::set_file_namepath::PathBuf::shrink_topath::PathBuf::shrink_to_fitpath::PathBuf::with_capacityimpl<P: AsRef<Path>> FromStream<P> for PathBufpath::Path::ancestorspath::Path::as_os_strpath::Path::canonicalizepath::Path::componentspath::Path::displaypath::Path::ends_withpath::Path::existspath::Path::extensionpath::Path::file_namepath::Path::file_stempath::Path::has_rootpath::Path::into_path_bufpath::Path::is_absolutepath::Path::is_dirpath::Path::is_filepath::Path::is_relativepath::Path::iterpath::Path::joinpath::Path::metadatapath::Path::newpath::Path::parentpath::Path::read_dirpath::Path::read_linkpath::Path::starts_withpath::Path::strip_prefixpath::Path::symlink_metadatapath::Path::to_path_bufpath::Path::to_strpath::Path::to_string_lossypath::Path::with_extensionpath::Path::with_file_nameThough it's not super related to the issue description, I highly suggest that as part of this you add the FromStream impl for PathBuf.
Here's the FromIterator impl:
impl<P: AsRef<Path>> FromIterator<P> for PathBuf
@sunjay that's an excellent suggestion; thanks so much!
I'm interested in implementing this feature. It's basically invoking blocking::spawn on filesystem operations, right?
@pandaman64 yes, that's exactly it! Additionally some methods such as Path::canonicalize can be forwarded to existing implementations such as fs::canonicalize. All in all this should be relatively straight forward!
Most helpful comment
I'm interested in implementing this feature. It's basically invoking
blocking::spawnon filesystem operations, right?