mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
fs: add File::into_std and File::try_into_std methods (#1856)
In version 0.1 there was File::into_std method that destructured tokio_fs::File into std::fs:File. That method was lacking in version 0.2. Fixes: #1852
This commit is contained in:
parent
aef434c089
commit
4261ab6627
@ -394,6 +394,55 @@ impl File {
|
||||
Ok(File::from_std(std_file))
|
||||
}
|
||||
|
||||
/// Destructures `File` into a [`std::fs::File`][std]. This function is
|
||||
/// async to allow any in-flight operations to complete.
|
||||
///
|
||||
/// Use `File::try_into_std` to attempt conversion immediately.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use tokio::fs::File;
|
||||
///
|
||||
/// # async fn dox() -> std::io::Result<()> {
|
||||
/// let tokio_file = File::open("foo.txt").await?;
|
||||
/// let std_file = tokio_file.into_std().await;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn into_std(mut self) -> sys::File {
|
||||
self.complete_inflight().await;
|
||||
Arc::try_unwrap(self.std).expect("Arc::try_unwrap failed")
|
||||
}
|
||||
|
||||
/// Tries to immediately destructure `File` into a [`std::fs::File`][std].
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error containing the file if some
|
||||
/// operation is in-flight.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use tokio::fs::File;
|
||||
///
|
||||
/// # async fn dox() -> std::io::Result<()> {
|
||||
/// let tokio_file = File::open("foo.txt").await?;
|
||||
/// let std_file = tokio_file.try_into_std().unwrap();
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn try_into_std(mut self) -> Result<sys::File, Self> {
|
||||
match Arc::try_unwrap(self.std) {
|
||||
Ok(file) => Ok(file),
|
||||
Err(std_file_arc) => {
|
||||
self.std = std_file_arc;
|
||||
Err(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Changes the permissions on the underlying file.
|
||||
///
|
||||
/// # Platform-specific behavior
|
||||
|
Loading…
x
Reference in New Issue
Block a user