Skip to content

Commit 141954d

Browse files
committed
Implemented Path::parent
1 parent cc57db0 commit 141954d

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

src/path/path.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -462,29 +462,6 @@ impl Path {
462462
fs::metadata(self).await
463463
}
464464

465-
/// Queries the metadata about a file without following symlinks.
466-
///
467-
/// This is an alias to [`fs::symlink_metadata`].
468-
///
469-
/// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
470-
///
471-
/// # Examples
472-
///
473-
/// ```no_run
474-
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
475-
/// #
476-
/// use async_std::path::Path;
477-
///
478-
/// let path = Path::new("/Minas/tirith");
479-
/// let metadata = path.symlink_metadata().await.expect("symlink_metadata call failed");
480-
/// println!("{:?}", metadata.file_type());
481-
/// #
482-
/// # Ok(()) }) }
483-
/// ```
484-
pub async fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
485-
fs::symlink_metadata(self).await
486-
}
487-
488465
/// Directly wraps a string slice as a `Path` slice.
489466
///
490467
/// This is a cost-free conversion.
@@ -511,6 +488,52 @@ impl Path {
511488
unsafe { &*(std::path::Path::new(s) as *const std::path::Path as *const Path) }
512489
}
513490

491+
/// Returns the `Path` without its final component, if there is one.
492+
///
493+
/// Returns [`None`] if the path terminates in a root or prefix.
494+
///
495+
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
496+
///
497+
/// # Examples
498+
///
499+
/// ```
500+
/// use async_std::path::Path;
501+
///
502+
/// let path = Path::new("/foo/bar");
503+
/// let parent = path.parent().unwrap();
504+
/// assert_eq!(parent, Path::new("/foo"));
505+
///
506+
/// let grand_parent = parent.parent().unwrap();
507+
/// assert_eq!(grand_parent, Path::new("/"));
508+
/// assert_eq!(grand_parent.parent(), None);
509+
/// ```
510+
pub fn parent(&self) -> Option<&Path> {
511+
self.inner.parent().map(|p| p.into())
512+
}
513+
514+
/// Queries the metadata about a file without following symlinks.
515+
///
516+
/// This is an alias to [`fs::symlink_metadata`].
517+
///
518+
/// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
519+
///
520+
/// # Examples
521+
///
522+
/// ```no_run
523+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
524+
/// #
525+
/// use async_std::path::Path;
526+
///
527+
/// let path = Path::new("/Minas/tirith");
528+
/// let metadata = path.symlink_metadata().await.expect("symlink_metadata call failed");
529+
/// println!("{:?}", metadata.file_type());
530+
/// #
531+
/// # Ok(()) }) }
532+
/// ```
533+
pub async fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
534+
fs::symlink_metadata(self).await
535+
}
536+
514537
/// Converts a `Path` to an owned [`PathBuf`].
515538
///
516539
/// [`PathBuf`]: struct.PathBuf.html

0 commit comments

Comments
 (0)