Closed
Description
Feature gate: #![feature(path_try_exists)]
This is a tracking issue for try_exists()
method on std::path::Path
.
This method is similar to exists()
method except it does not silently ignore errors that made it impossible to find out if the path actually exists (e.g. lack of permission on parent dir). Thus it naturally returns io::Result<bool>
instead of just bool
.
Public API
mod fs {
pub fn try_exists<P: AsRef<Path>>(path: P) -> Result<bool>;
}
impl Path {
#[stable(since = "1.63.0")]
pub fn try_exists(&self) -> io::Result<bool>;
}
Steps / History
- Added
try_exists()
method tostd::path::Path
#81822 - Windows implementation of feature
path_try_exists
#85060 - Stabilize
Path::try_exists()
and improve doc #97912
Unresolved Questions
- None yet.
Activity
try_exists()
method tostd::path::Path
#81822ChrisDenton commentedon Mar 17, 2021
Could
try_exists
be added as a free function tostd::fs
and then thestd::path::Path
method forwards to that? This would be similar to themetadata
function.My motivation for this is to make it easier for platforms to override the
metadata
based implementation.fs::exists
function tokio-rs/tokio#3375path.exists()
instead offs::metadata(path).is_ok()
#85044path_try_exists
#85060Rollup merge of rust-lang#85060 - ChrisDenton:win-file-exists, r=yaahc
Auto merge of rust-lang#85060 - ChrisDenton:win-file-exists, r=yaahc
Kixunil commentedon Aug 21, 2021
In the internals discussion @matklad pointed out that
is_file()
andis_dir()
have same issue. I believe it may be even worse in their case. I wonder would it be reasonable to add those method within the scope of this feature or as a separate change?is_symlink()
forMetadata
andPath
#89677roblabla commentedon Feb 1, 2022
Since #85060, there's now
std::fs::try_exists
but nostd::fs::exists
(the method only exists onPath
). This is somewhat confusing, especially since the documentation forstd::fs::try_exists
says:Without linking the appropriate exists method.
I think if we're going to have an
std::fs::try_exists
, we'll want to make anstd::fs::exists
method as well for symmetry.ChrisDenton commentedon Feb 1, 2022
I think there are a number of options here:
std::fs::exists
as you suggest.std::fs::try_exists
. I'd still want there to be some kind offs::try_exists
so that different platforms can specialize the implementation. But that can be purely internal to the standard library.std::fs::exists
is thatPath::exists
is just a convenience wrapper aroundstd::fs::metadata(path).is_ok()
. A similar rationale could be applied whenPath::exists
is implemented asstd::fs::try_exists(path).unwrap_or(false)
.Kixunil commentedon Feb 1, 2022
@roblabla clearly the doc was copied from
Path::try_exist
. Would be nice to fix.62 remaining items