Skip to content

Commit df9a01f

Browse files
committed
Implemented Path::is_file
1 parent 20f58ea commit df9a01f

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/path/path.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,76 @@ impl Path {
303303
self.inner.is_absolute()
304304
}
305305

306+
/// Returns `true` if the path exists on disk and is pointing at a directory.
307+
///
308+
/// This function will traverse symbolic links to query information about the
309+
/// destination file. In case of broken symbolic links this will return `false`.
310+
///
311+
/// If you cannot access the directory containing the file, e.g., because of a
312+
/// permission error, this will return `false`.
313+
///
314+
/// # Examples
315+
///
316+
/// ```no_run
317+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
318+
/// #
319+
/// use async_std::path::Path;
320+
/// assert_eq!(Path::new("./is_a_directory/").is_dir().await, true);
321+
/// assert_eq!(Path::new("a_file.txt").is_dir().await, false);
322+
/// #
323+
/// # Ok(()) }) }
324+
/// ```
325+
///
326+
/// # See Also
327+
///
328+
/// This is a convenience function that coerces errors to false. If you want to
329+
/// check errors, call [fs::metadata] and handle its Result. Then call
330+
/// [fs::Metadata::is_dir] if it was Ok.
331+
///
332+
/// [fs::metadata]: ../fs/fn.metadata.html
333+
/// [fs::Metadata::is_dir]: ../fs/struct.Metadata.html#method.is_dir
334+
pub async fn is_dir(&self) -> bool {
335+
fs::metadata(self)
336+
.await
337+
.map(|m| m.is_dir())
338+
.unwrap_or(false)
339+
}
340+
341+
/// Returns `true` if the path exists on disk and is pointing at a regular file.
342+
///
343+
/// This function will traverse symbolic links to query information about the
344+
/// destination file. In case of broken symbolic links this will return `false`.
345+
///
346+
/// If you cannot access the directory containing the file, e.g., because of a
347+
/// permission error, this will return `false`.
348+
///
349+
/// # Examples
350+
///
351+
/// ```no_run
352+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
353+
/// #
354+
/// use async_std::path::Path;
355+
/// assert_eq!(Path::new("./is_a_directory/").is_file().await, false);
356+
/// assert_eq!(Path::new("a_file.txt").is_file().await, true);
357+
/// #
358+
/// # Ok(()) }) }
359+
/// ```
360+
///
361+
/// # See Also
362+
///
363+
/// This is a convenience function that coerces errors to false. If you want to
364+
/// check errors, call [fs::metadata] and handle its Result. Then call
365+
/// [fs::Metadata::is_file] if it was Ok.
366+
///
367+
/// [fs::metadata]: ../fs/fn.metadata.html
368+
/// [fs::Metadata::is_file]: ../fs/struct.Metadata.html#method.is_file
369+
pub async fn is_file(&self) -> bool {
370+
fs::metadata(self)
371+
.await
372+
.map(|m| m.is_file())
373+
.unwrap_or(false)
374+
}
375+
306376
/// Queries the file system to get information about a file, directory, etc.
307377
///
308378
/// This function will traverse symbolic links to query information about the

0 commit comments

Comments
 (0)