diff --git a/src/job.rs b/src/job.rs index c03171a..10011bc 100644 --- a/src/job.rs +++ b/src/job.rs @@ -10,6 +10,81 @@ use std::io::{ }; 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 +{ + /// 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 Status +{ + /// 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 + { + 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 Default for Status +{ + #[inline] + fn default() -> Self + { + Self::Pending + } +} + + #[derive(Debug)] pub struct Prelude { @@ -52,8 +127,6 @@ pub struct Job offset: usize, } -//TODO: job's work :^) - impl Job { pub fn state(&self) -> &state::State diff --git a/src/main.rs b/src/main.rs index c8dded1..a80d626 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,19 @@ use std::convert::TryFrom; }; } +macro_rules! vec_default { + ($num:expr) => { + { + let num = $num; + + let mut v= Vec::with_capacity(num); + v.resize_with(num, Default::default); + v + } + } +} + + mod state; mod pool; mod map; @@ -54,6 +67,7 @@ fn work(arg::Operation{output, inputs}: arg::Operation) -> Result<(), Box> = vec_default!(jobs.len()); // when we receive completion from the stream (which yeilds `(usize, usize)`), the first item is the *index* in this vector to set, and the second is the value let state: state::State = {todo!()}; // - spawn the task thread pool diff --git a/src/map.rs b/src/map.rs index b35e2ca..70995ff 100644 --- a/src/map.rs +++ b/src/map.rs @@ -41,7 +41,7 @@ impl MemoryMapMut /// Unmap this file and return the fd pub fn unmap(self) -> fs::File { - use std::{mem, ptr}; + use std::mem; let mut this = mem::ManuallyDrop::new(self); unsafe { let map = (&mut this.map as *mut MmapMut).read();