File tree 1 file changed +25
-0
lines changed
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 _;
58
58
/// #
59
59
/// # Ok(()) }) }
60
60
/// ```
61
+ #[ derive( Clone ) ]
61
62
pub struct File {
62
63
/// A reference to the inner file.
63
64
file : Arc < std:: fs:: File > ,
@@ -470,6 +471,13 @@ struct Lock<T>(Arc<LockState<T>>);
470
471
unsafe impl < T : Send > Send for Lock < T > { }
471
472
unsafe impl < T : Send > Sync for Lock < T > { }
472
473
474
+ impl < T > Clone for Lock < T > {
475
+ #[ inline]
476
+ fn clone ( & self ) -> Self {
477
+ Self ( Arc :: clone ( & self . 0 ) )
478
+ }
479
+ }
480
+
473
481
/// The state of a lock.
474
482
struct LockState < T > {
475
483
/// Set to `true` when locked.
@@ -878,4 +886,21 @@ mod tests {
878
886
File :: open ( file ! ( ) ) . await . unwrap ( ) ;
879
887
} ) ;
880
888
}
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
+ }
881
906
}
You can’t perform that action at this time.
0 commit comments