mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-06 20:55:33 +00:00
Add ToOwned and Borrow impls
This commit is contained in:
parent
ba87048db5
commit
0adcb50f58
7 changed files with 19 additions and 5 deletions
|
@ -32,7 +32,7 @@ use crate::task::blocking;
|
|||
/// # Ok(()) }) }
|
||||
/// ```
|
||||
pub async fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
|
||||
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
|
||||
let path: PathBuf = path.as_ref().to_owned();
|
||||
Ok(blocking::spawn(async move { std::fs::canonicalize(&path) })
|
||||
.await?
|
||||
.into())
|
||||
|
|
|
@ -35,7 +35,7 @@ use crate::task::blocking;
|
|||
/// # Ok(()) }) }
|
||||
/// ```
|
||||
pub async fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
|
||||
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
|
||||
let path = path.as_ref().to_owned();
|
||||
blocking::spawn(async move { std::fs::metadata(path) }).await
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll};
|
|||
/// # Ok(()) }) }
|
||||
/// ```
|
||||
pub async fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
|
||||
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
|
||||
let path = path.as_ref().to_owned();
|
||||
blocking::spawn(async move { std::fs::read_dir(path) })
|
||||
.await
|
||||
.map(ReadDir::new)
|
||||
|
|
|
@ -27,7 +27,7 @@ use crate::task::blocking;
|
|||
/// # Ok(()) }) }
|
||||
/// ```
|
||||
pub async fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
|
||||
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
|
||||
let path = path.as_ref().to_owned();
|
||||
Ok(blocking::spawn(async move { std::fs::read_link(path) })
|
||||
.await?
|
||||
.into())
|
||||
|
|
|
@ -33,6 +33,6 @@ use crate::task::blocking;
|
|||
/// # Ok(()) }) }
|
||||
/// ```
|
||||
pub async fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
|
||||
let path: std::path::PathBuf = path.as_ref().to_path_buf().into();
|
||||
let path = path.as_ref().to_owned();
|
||||
blocking::spawn(async move { std::fs::symlink_metadata(path) }).await
|
||||
}
|
||||
|
|
|
@ -830,3 +830,11 @@ impl AsRef<Path> for String {
|
|||
Path::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::borrow::ToOwned for Path {
|
||||
type Owned = PathBuf;
|
||||
|
||||
fn to_owned(&self) -> PathBuf {
|
||||
self.to_path_buf()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -192,6 +192,12 @@ impl std::ops::Deref for PathBuf {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::borrow::Borrow<Path> for PathBuf {
|
||||
fn borrow(&self) -> &Path {
|
||||
&**self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::path::PathBuf> for PathBuf {
|
||||
fn from(path: std::path::PathBuf) -> PathBuf {
|
||||
PathBuf { inner: path }
|
||||
|
|
Loading…
Reference in a new issue