mirror of
https://github.com/async-rs/async-std.git
synced 2025-05-14 02:51:29 +00:00
Refactor
This commit is contained in:
parent
5c1e0522b7
commit
aa13ba758b
4 changed files with 746 additions and 751 deletions
39
src/path/ancestors.rs
Normal file
39
src/path/ancestors.rs
Normal file
|
@ -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
|
//! [`std::path`]: https://doc.rust-lang.org/std/path/index.html
|
||||||
|
|
||||||
|
mod ancestors;
|
||||||
mod path;
|
mod path;
|
||||||
mod pathbuf;
|
mod pathbuf;
|
||||||
|
|
||||||
|
@ -23,5 +24,6 @@ pub use std::path::MAIN_SEPARATOR;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use std::path::is_separator;
|
pub use std::path::is_separator;
|
||||||
|
|
||||||
pub use path::{Ancestors, Path};
|
use ancestors::Ancestors;
|
||||||
|
pub use path::Path;
|
||||||
pub use pathbuf::PathBuf;
|
pub use pathbuf::PathBuf;
|
||||||
|
|
1262
src/path/path.rs
1262
src/path/path.rs
File diff suppressed because it is too large
Load diff
|
@ -11,6 +11,19 @@ pub struct PathBuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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.
|
/// Coerces to a [`Path`] slice.
|
||||||
///
|
///
|
||||||
/// [`Path`]: struct.Path.html
|
/// [`Path`]: struct.Path.html
|
||||||
|
@ -27,68 +40,6 @@ impl PathBuf {
|
||||||
self.inner.as_path().into()
|
self.inner.as_path().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
|
|
||||||
///
|
|
||||||
/// [`OsString`]: https://doc.rust-lang.org/std/ffi/struct.OsString.html
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use async_std::path::PathBuf;
|
|
||||||
///
|
|
||||||
/// let p = PathBuf::from("/the/head");
|
|
||||||
/// let os_str = p.into_os_string();
|
|
||||||
/// ```
|
|
||||||
pub fn into_os_string(self) -> OsString {
|
|
||||||
self.inner.into_os_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Allocates an empty `PathBuf`.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use async_std::path::PathBuf;
|
|
||||||
///
|
|
||||||
/// let path = PathBuf::new();
|
|
||||||
/// ```
|
|
||||||
pub fn new() -> PathBuf {
|
|
||||||
std::path::PathBuf::new().into()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Truncates `self` to [`self.parent`].
|
|
||||||
///
|
|
||||||
/// Returns `false` and does nothing if [`self.parent`] is [`None`].
|
|
||||||
/// Otherwise, returns `true`.
|
|
||||||
///
|
|
||||||
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
|
|
||||||
/// [`self.parent`]: struct.PathBuf.html#method.parent
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use async_std::path::{Path, PathBuf};
|
|
||||||
///
|
|
||||||
/// let mut p = PathBuf::from("/test/test.rs");
|
|
||||||
///
|
|
||||||
/// p.pop();
|
|
||||||
/// assert_eq!(Path::new("/test"), p.as_ref());
|
|
||||||
/// p.pop();
|
|
||||||
/// assert_eq!(Path::new("/"), p.as_ref());
|
|
||||||
/// ```
|
|
||||||
pub fn pop(&mut self) -> bool {
|
|
||||||
self.inner.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extends `self` with `path`.
|
/// Extends `self` with `path`.
|
||||||
///
|
///
|
||||||
/// If `path` is absolute, it replaces the current path.
|
/// If `path` is absolute, it replaces the current path.
|
||||||
|
@ -120,8 +71,62 @@ impl PathBuf {
|
||||||
/// path.push("/etc");
|
/// path.push("/etc");
|
||||||
/// assert_eq!(path, PathBuf::from("/etc"));
|
/// assert_eq!(path, PathBuf::from("/etc"));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn push<P: AsRef<std::path::Path>>(&mut self, path: P) {
|
pub fn push<P: AsRef<Path>>(&mut self, path: P) {
|
||||||
self.inner.push(path)
|
self.inner.push(path.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Truncates `self` to [`self.parent`].
|
||||||
|
///
|
||||||
|
/// Returns `false` and does nothing if [`self.parent`] is [`None`].
|
||||||
|
/// Otherwise, returns `true`.
|
||||||
|
///
|
||||||
|
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
|
||||||
|
/// [`self.parent`]: struct.PathBuf.html#method.parent
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use async_std::path::{Path, PathBuf};
|
||||||
|
///
|
||||||
|
/// let mut p = PathBuf::from("/test/test.rs");
|
||||||
|
///
|
||||||
|
/// p.pop();
|
||||||
|
/// assert_eq!(Path::new("/test"), p.as_ref());
|
||||||
|
/// p.pop();
|
||||||
|
/// assert_eq!(Path::new("/"), p.as_ref());
|
||||||
|
/// ```
|
||||||
|
pub fn pop(&mut self) -> bool {
|
||||||
|
self.inner.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.)
|
||||||
|
///
|
||||||
|
/// [`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
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// 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"));
|
||||||
|
/// ```
|
||||||
|
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`.
|
/// Updates [`self.extension`] to `extension`.
|
||||||
|
@ -153,34 +158,29 @@ impl PathBuf {
|
||||||
self.inner.set_extension(extension)
|
self.inner.set_extension(extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates [`self.file_name`] to `file_name`.
|
/// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
|
||||||
///
|
///
|
||||||
/// If [`self.file_name`] was [`None`], this is equivalent to pushing
|
/// [`OsString`]: https://doc.rust-lang.org/std/ffi/struct.OsString.html
|
||||||
/// `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.)
|
|
||||||
///
|
|
||||||
/// [`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
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use async_std::path::PathBuf;
|
/// use async_std::path::PathBuf;
|
||||||
///
|
///
|
||||||
/// let mut buf = PathBuf::from("/");
|
/// let p = PathBuf::from("/the/head");
|
||||||
/// assert!(buf.file_name() == None);
|
/// let os_str = p.into_os_string();
|
||||||
/// 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 set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
|
pub fn into_os_string(self) -> OsString {
|
||||||
self.inner.set_file_name(file_name)
|
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…
Reference in a new issue