summaryrefslogtreecommitdiffstats
path: root/tokio/src/fs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-11-29 02:09:28 +0100
committerCarl Lerche <me@carllerche.com>2019-11-28 17:09:28 -0800
commit4261ab6627d1da5e5c70a4b88ccdfc329334716b (patch)
treeba8c29e70ae6dcc03230c26725a2015247b1e638 /tokio/src/fs
parentaef434c089754051e4a10c0e618f46003f31d2dc (diff)
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
Diffstat (limited to 'tokio/src/fs')
-rw-r--r--tokio/src/fs/file.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs
index c4558272..bb1084a1 100644
--- a/tokio/src/fs/file.rs
+++ b/tokio/src/fs/file.rs
@@ -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