Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6278fdc

Browse files
Merge pull request #937 from sunfishcode/main
Implement Clone for File.
2 parents b210ee3 + 09e9984 commit 6278fdc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎src/fs/file.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use crate::utils::Context as _;
5858
/// #
5959
/// # Ok(()) }) }
6060
/// ```
61+
#[derive(Clone)]
6162
pub struct File {
6263
/// A reference to the inner file.
6364
file: Arc<std::fs::File>,
@@ -470,6 +471,13 @@ struct Lock<T>(Arc<LockState<T>>);
470471
unsafe impl<T: Send> Send for Lock<T> {}
471472
unsafe impl<T: Send> Sync for Lock<T> {}
472473

474+
impl<T> Clone for Lock<T> {
475+
#[inline]
476+
fn clone(&self) -> Self {
477+
Self(Arc::clone(&self.0))
478+
}
479+
}
480+
473481
/// The state of a lock.
474482
struct LockState<T> {
475483
/// Set to `true` when locked.
@@ -878,4 +886,21 @@ mod tests {
878886
File::open(file!()).await.unwrap();
879887
});
880888
}
889+
890+
#[test]
891+
fn async_file_clone() {
892+
crate::task::block_on(async move {
893+
let file = File::open(file!()).await.unwrap();
894+
let mut clone = file.clone();
895+
let len = crate::task::spawn_blocking(move || {
896+
let mut buf = Vec::new();
897+
crate::task::block_on(async move {
898+
clone.read_to_end(&mut buf).await.unwrap();
899+
drop(clone);
900+
buf.len()
901+
})
902+
}).await;
903+
assert_eq!(len as u64, file.metadata().await.unwrap().len());
904+
});
905+
}
881906
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /