File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ use crate::utils::Context as _;
5858/// #
5959/// # Ok(()) }) }
6060/// ```
61+ #[ derive( Clone ) ]
6162pub 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>>);
470471unsafe impl < T : Send > Send for Lock < T > { }
471472unsafe 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.
474482struct 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}
You can’t perform that action at this time.
0 commit comments