@ -8,12 +8,15 @@ use std::io::{
self ,
self ,
Read ,
Read ,
} ;
} ;
use std ::{ fmt , error } ;
#[ derive(Debug) ]
#[ derive(Debug) ]
pub struct Prelude
pub struct Prelude
{
{
file : File ,
file : File ,
stat : Metadata ,
stat : Metadata ,
file_num : usize ,
offset : usize ,
offset : usize ,
}
}
@ -30,6 +33,7 @@ impl Prelude{
fd : self . file ,
fd : self . file ,
stat : self . stat ,
stat : self . stat ,
offset : self . offset ,
offset : self . offset ,
file_num : self . file_num ,
state ,
state ,
}
}
}
}
@ -41,6 +45,7 @@ pub struct Job
fd : File ,
fd : File ,
stat : Metadata ,
stat : Metadata ,
file_num : usize ,
/// We grab the slice of memory we write to from here
/// We grab the slice of memory we write to from here
state : state ::State ,
state : state ::State ,
/// From this offset
/// From this offset
@ -83,6 +88,12 @@ impl Job
self . state . slice ( self . start ( ) .. self . end ( ) )
self . state . slice ( self . start ( ) .. self . end ( ) )
}
}
}
}
/// Complete this job
pub fn complete ( self , size : usize ) -> Result < ( ) , CompletionError >
{
self . state . send_complete ( self . file_num , size ) . map_err ( | _ | CompletionError )
}
}
}
impl Read for Job
impl Read for Job
@ -97,7 +108,7 @@ impl Read for Job
///
///
/// `sz` is the offset of the *end* of the last job. (or 0 for the first).
/// `sz` is the offset of the *end* of the last job. (or 0 for the first).
/// `sz` is then updated with this file's size for this method to be used again on the next file.
/// `sz` is then updated with this file's size for this method to be used again on the next file.
pub fn create_from_file ( file : impl AsRef < Path > , sz : & mut usize ) -> io ::Result < Prelude >
pub fn create_from_file ( file _num: usize , file : impl AsRef < Path > , sz : & mut usize ) -> io ::Result < Prelude >
{
{
let file = OpenOptions ::new ( )
let file = OpenOptions ::new ( )
. read ( true )
. read ( true )
@ -107,9 +118,24 @@ pub fn create_from_file(file: impl AsRef<Path>, sz: &mut usize) -> io::Result<Pr
let offset = * sz ;
let offset = * sz ;
let prelude = Prelude {
let prelude = Prelude {
file , stat , offset ,
file , stat , offset , file_num
} ;
} ;
* sz + = prelude . len ( ) ;
* sz + = prelude . len ( ) ;
Ok ( prelude )
Ok ( prelude )
}
}
/// Error returned when main thread's completion receiver was dropped. This should be fatal.
#[ derive(Debug) ]
pub struct CompletionError ;
impl error ::Error for CompletionError { }
impl fmt ::Display for CompletionError
{
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result
{
write! ( f , "unable to send completion signal because main thread's completion receiver was dropped." )
}
}