@ -10,6 +10,81 @@ use std::io::{
} ;
} ;
use std ::{ fmt , error } ;
use std ::{ fmt , error } ;
/// A job's completion status
///
/// If the job has not completed, it will be the default value `Pending`.
/// Once the job completes, the result of the job can be inserted to set to `Complete`.
/// Afterwards, you can extract the value and it will have the state `Taken`; which means "job has completed, but the value has been consumed".
///
/// # Notes
/// This is not coupled to anything, it's the user's responsibility to update this status when a job completes.
#[ derive(Debug, Clone, PartialEq, Eq, Hash) ]
pub enum Status < T >
{
/// For running operations
Pending ,
/// An operation that has completed with its value
Complete ( T ) ,
/// An operation who's completed value has been extracted and processed
Taken ,
}
impl < T > Status < T >
{
/// Assign a completed value to this instance.
///
/// If there was one already set, it is ignored.
#[ inline ] pub fn complete ( & mut self , value : T )
{
* self = Self ::Complete ( value ) ;
}
/// Has the value not yet been assigned or taken?
pub fn is_pending ( & self ) -> bool
{
match self {
Self ::Pending = > true ,
_ = > false ,
}
}
/// Take the completed value from this instance
pub fn take ( & mut self ) -> Option < T >
{
match std ::mem ::replace ( self , Self ::Taken ) {
Self ::Complete ( t ) = > Some ( t ) ,
_ = > None
}
}
/// Get a mutable reference to the completed value if there is one.
pub fn peek_mut ( & mut self ) -> Option < & mut T >
{
match self {
Self ::Complete ( ref mut t ) = > Some ( t ) ,
_ = > None ,
}
}
/// Get a reference to the completed value if there is one.
pub fn peek ( & self ) -> Option < & T >
{
match self {
Self ::Complete ( ref t ) = > Some ( t ) ,
_ = > None ,
}
}
}
impl < T > Default for Status < T >
{
#[ inline ]
fn default ( ) -> Self
{
Self ::Pending
}
}
#[ derive(Debug) ]
#[ derive(Debug) ]
pub struct Prelude
pub struct Prelude
{
{
@ -52,8 +127,6 @@ pub struct Job
offset : usize ,
offset : usize ,
}
}
//TODO: job's work :^)
impl Job
impl Job
{
{
pub fn state ( & self ) -> & state ::State
pub fn state ( & self ) -> & state ::State