yoshuawuyts-patch-1
Stjepan Glavina 5 years ago
parent 5c1e0522b7
commit aa13ba758b

@ -0,0 +1,39 @@
use std::iter::FusedIterator;
use crate::path::Path;
/// An iterator over [`Path`] and its ancestors.
///
/// This `struct` is created by the [`ancestors`] method on [`Path`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// use async_std::path::Path;
///
/// let path = Path::new("/foo/bar");
///
/// for ancestor in path.ancestors() {
/// println!("{}", ancestor.display());
/// }
/// ```
///
/// [`ancestors`]: struct.Path.html#method.ancestors
/// [`Path`]: struct.Path.html
#[derive(Copy, Clone, Debug)]
pub struct Ancestors<'a> {
pub(crate) next: Option<&'a Path>,
}
impl<'a> Iterator for Ancestors<'a> {
type Item = &'a Path;
fn next(&mut self) -> Option<Self::Item> {
let next = self.next;
self.next = next.and_then(Path::parent);
next
}
}
impl FusedIterator for Ancestors<'_> {}

@ -4,6 +4,7 @@
//!
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html
mod ancestors;
mod path;
mod pathbuf;
@ -23,5 +24,6 @@ pub use std::path::MAIN_SEPARATOR;
#[doc(inline)]
pub use std::path::is_separator;
pub use path::{Ancestors, Path};
use ancestors::Ancestors;
pub use path::Path;
pub use pathbuf::PathBuf;

File diff suppressed because it is too large Load Diff

@ -11,6 +11,19 @@ pub struct PathBuf {
}
impl PathBuf {
/// Allocates an empty `PathBuf`.
///
/// # Examples
///
/// ```
/// use async_std::path::PathBuf;
///
/// let path = PathBuf::new();
/// ```
pub fn new() -> PathBuf {
std::path::PathBuf::new().into()
}
/// Coerces to a [`Path`] slice.
///
/// [`Path`]: struct.Path.html
@ -27,42 +40,39 @@ impl PathBuf {
self.inner.as_path().into()
}
/// Converts this `PathBuf` into a [boxed][`Box`] [`Path`].
/// Extends `self` with `path`.
///
/// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
/// [`Path`]: struct.Path.html
pub fn into_boxed_path(self) -> Box<Path> {
let rw = Box::into_raw(self.inner.into_boxed_path()) as *mut Path;
unsafe { Box::from_raw(rw) }
}
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
/// If `path` is absolute, it replaces the current path.
///
/// [`OsString`]: https://doc.rust-lang.org/std/ffi/struct.OsString.html
/// On Windows:
///
/// * if `path` has a root but no prefix (e.g., `\windows`), it
/// replaces everything except for the prefix (if any) of `self`.
/// * if `path` has a prefix but no root, it replaces `self`.
///
/// # Examples
///
/// Pushing a relative path extends the existing path:
///
/// ```
/// use async_std::path::PathBuf;
///
/// let p = PathBuf::from("/the/head");
/// let os_str = p.into_os_string();
/// let mut path = PathBuf::from("/tmp");
/// path.push("file.bk");
/// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
/// ```
pub fn into_os_string(self) -> OsString {
self.inner.into_os_string()
}
/// Allocates an empty `PathBuf`.
///
/// # Examples
/// Pushing an absolute path replaces the existing path:
///
/// ```
/// use async_std::path::PathBuf;
///
/// let path = PathBuf::new();
/// let mut path = PathBuf::from("/tmp");
/// path.push("/etc");
/// assert_eq!(path, PathBuf::from("/etc"));
/// ```
pub fn new() -> PathBuf {
std::path::PathBuf::new().into()
pub fn push<P: AsRef<Path>>(&mut self, path: P) {
self.inner.push(path.as_ref())
}
/// Truncates `self` to [`self.parent`].
@ -89,39 +99,34 @@ impl PathBuf {
self.inner.pop()
}
/// Extends `self` with `path`.
/// Updates [`self.file_name`] to `file_name`.
///
/// If `path` is absolute, it replaces the current path.
/// If [`self.file_name`] was [`None`], this is equivalent to pushing
/// `file_name`.
///
/// On Windows:
/// Otherwise it is equivalent to calling [`pop`] and then pushing
/// `file_name`. The new path will be a sibling of the original path.
/// (That is, it will have the same parent.)
///
/// * if `path` has a root but no prefix (e.g., `\windows`), it
/// replaces everything except for the prefix (if any) of `self`.
/// * if `path` has a prefix but no root, it replaces `self`.
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
/// [`pop`]: struct.PathBuf.html#method.pop
///
/// # Examples
///
/// Pushing a relative path extends the existing path:
///
/// ```
/// use async_std::path::PathBuf;
///
/// let mut path = PathBuf::from("/tmp");
/// path.push("file.bk");
/// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
/// ```
///
/// Pushing an absolute path replaces the existing path:
///
/// ```
/// use async_std::path::PathBuf;
///
/// let mut path = PathBuf::from("/tmp");
/// path.push("/etc");
/// assert_eq!(path, PathBuf::from("/etc"));
/// let mut buf = PathBuf::from("/");
/// assert!(buf.file_name() == None);
/// buf.set_file_name("bar");
/// assert!(buf == PathBuf::from("/bar"));
/// assert!(buf.file_name().is_some());
/// buf.set_file_name("baz.txt");
/// assert!(buf == PathBuf::from("/baz.txt"));
/// ```
pub fn push<P: AsRef<std::path::Path>>(&mut self, path: P) {
self.inner.push(path)
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
self.inner.set_file_name(file_name)
}
/// Updates [`self.extension`] to `extension`.
@ -153,34 +158,29 @@ impl PathBuf {
self.inner.set_extension(extension)
}
/// Updates [`self.file_name`] to `file_name`.
///
/// If [`self.file_name`] was [`None`], this is equivalent to pushing
/// `file_name`.
///
/// Otherwise it is equivalent to calling [`pop`] and then pushing
/// `file_name`. The new path will be a sibling of the original path.
/// (That is, it will have the same parent.)
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
///
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
/// [`pop`]: struct.PathBuf.html#method.pop
/// [`OsString`]: https://doc.rust-lang.org/std/ffi/struct.OsString.html
///
/// # Examples
///
/// ```
/// use async_std::path::PathBuf;
///
/// let mut buf = PathBuf::from("/");
/// assert!(buf.file_name() == None);
/// buf.set_file_name("bar");
/// assert!(buf == PathBuf::from("/bar"));
/// assert!(buf.file_name().is_some());
/// buf.set_file_name("baz.txt");
/// assert!(buf == PathBuf::from("/baz.txt"));
/// let p = PathBuf::from("/the/head");
/// let os_str = p.into_os_string();
/// ```
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
self.inner.set_file_name(file_name)
pub fn into_os_string(self) -> OsString {
self.inner.into_os_string()
}
/// Converts this `PathBuf` into a [boxed][`Box`] [`Path`].
///
/// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
/// [`Path`]: struct.Path.html
pub fn into_boxed_path(self) -> Box<Path> {
let rw = Box::into_raw(self.inner.into_boxed_path()) as *mut Path;
unsafe { Box::from_raw(rw) }
}
}

Loading…
Cancel
Save