|
| 1 | +use std::{io, thread::spawn}; |
| 2 | + |
| 3 | +use crate::node::LocalNode; |
| 4 | + |
| 5 | +/// A generic task builder. |
| 6 | +/// |
| 7 | +pub trait TaskBuilder { |
| 8 | + /// An error type. |
| 9 | + type Error; |
| 10 | + |
| 11 | + /// A target task. |
| 12 | + type Task: Task<Error = Self::Error>; |
| 13 | + |
| 14 | + /// Create a new task starting with the given node. |
| 15 | + #[must_use] |
| 16 | + fn open(&self, sink: LocalNode) -> Self::Task; |
| 17 | +} |
| 18 | + |
| 19 | +/// A generic task trait. |
| 20 | +/// |
| 21 | +pub trait Task { |
| 22 | + /// An error type. |
| 23 | + type Error; |
| 24 | + |
| 25 | + /// A target node. |
| 26 | + type Node; |
| 27 | + |
| 28 | + /// Register a typed node into the task. |
| 29 | + #[must_use] |
| 30 | + fn map(&mut self, sink: Self::Node, src: Self::Node) -> &mut Self; |
| 31 | + |
| 32 | + /// Complete building a task. |
| 33 | + /// Note that this task cannot be reused. |
| 34 | + #[must_use] |
| 35 | + fn finish( |
| 36 | + &mut self, |
| 37 | + src: Self::Node, |
| 38 | + ) -> Result<TaskContext<Self::Node, Self::Error>, Self::Error>; |
| 39 | +} |
| 40 | + |
| 41 | +/// An additional task trait that derives automatically. |
| 42 | +/// |
| 43 | +pub trait TaskExt: Task { |
| 44 | + /// Register a node into the task. |
| 45 | + #[inline] |
| 46 | + fn push<Tx, Rx>(&mut self, sink: Tx, src: Rx) -> Result<(), Self::Error> |
| 47 | + where |
| 48 | + Tx: TryInto<Self::Node>, |
| 49 | + Rx: TryInto<Self::Node>, |
| 50 | + Self::Error: From<Tx::Error> + From<Rx::Error>, |
| 51 | + { |
| 52 | + let _ = self.map(sink.try_into()?, src.try_into()?); |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | + |
| 56 | + /// Spawn the task into a new thread and return the last node. |
| 57 | + #[inline] |
| 58 | + fn to_node(&mut self, src: Self::Node) -> Result<Self::Node, Self::Error> |
| 59 | + where |
| 60 | + Self::Error: 'static + Send + From<io::Error>, |
| 61 | + { |
| 62 | + self.finish(src)?.into_node() |
| 63 | + } |
| 64 | + |
| 65 | + /// Spawn the task into the current thread and wait until finished. |
| 66 | + #[inline] |
| 67 | + fn wait(&mut self, src: Self::Node) -> Result<(), Self::Error> |
| 68 | + where |
| 69 | + Self::Error: 'static + Send, |
| 70 | + { |
| 71 | + self.finish(src)?.wait() |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<T> TaskExt for T where Self: Task {} |
| 76 | + |
| 77 | +/// A lazy task that contains actual task information. |
| 78 | +/// |
| 79 | +pub struct TaskContext<N, E> { |
| 80 | + bound: TaskBound<E>, |
| 81 | + node: N, |
| 82 | +} |
| 83 | + |
| 84 | +impl<N, E> TaskContext<N, E> |
| 85 | +where |
| 86 | + E: 'static + Send, |
| 87 | +{ |
| 88 | + /// Create a new task context. |
| 89 | + pub fn new(bound: TaskBound<E>, node: N) -> Self { |
| 90 | + Self { bound, node } |
| 91 | + } |
| 92 | + |
| 93 | + /// Spawn the task into a new thread and return the last node. |
| 94 | + pub fn into_node(self) -> Result<N, E> |
| 95 | + where |
| 96 | + E: From<io::Error>, |
| 97 | + { |
| 98 | + let Self { bound, node } = self; |
| 99 | + match bound { |
| 100 | + #[cfg(feature = "local")] |
| 101 | + TaskBound::LocalThread(_) => { |
| 102 | + return Err(io::Error::new( |
| 103 | + io::ErrorKind::InvalidInput, |
| 104 | + "the task is not thread-safe", |
| 105 | + ) |
| 106 | + .into()); |
| 107 | + } |
| 108 | + #[cfg(feature = "local")] |
| 109 | + TaskBound::LocalProcess(f) => { |
| 110 | + spawn(f); |
| 111 | + Ok(node) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /// Spawn the task into the current thread and wait until finished. |
| 117 | + #[cfg(feature = "local")] |
| 118 | + pub fn wait(self) -> Result<(), E> { |
| 119 | + let Self { bound, node } = self; |
| 120 | + let result = match bound { |
| 121 | + #[cfg(feature = "local")] |
| 122 | + TaskBound::LocalThread(f) => f(), |
| 123 | + #[cfg(feature = "local")] |
| 124 | + TaskBound::LocalProcess(f) => f(), |
| 125 | + }; |
| 126 | + drop(node); |
| 127 | + result |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/// A generic task context. |
| 132 | +/// |
| 133 | +pub enum TaskBound<E> { |
| 134 | + /// A thread-unsafe local task. |
| 135 | + #[cfg(feature = "local")] |
| 136 | + LocalThread(Box<dyn FnOnce() -> Result<(), E>>), |
| 137 | + /// A thread-safe local task. |
| 138 | + #[cfg(feature = "local")] |
| 139 | + LocalProcess(Box<dyn Send + FnOnce() -> Result<(), E>>), |
| 140 | +} |
0 commit comments