diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 94b6222..2e067fa 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -218,12 +218,38 @@ static_assert!(1+1==2; "static assertion test"); }; } +/// Run each expression in sequence then return the result of the first one. +/// +/// # Example +/// ``` +/// # use mtfse::prog1; +/// assert_eq!(prog1 { +/// 1 + 2; +/// println!("Done thing"); +/// 4 + 5; +/// }, 3); +/// ``` #[macro_export] macro_rules! prog1 { - ($first:expr, $($rest:expr);+ $(;)?) => { - ($first, $( $rest ),+).0 + ($first:expr; $($rest:expr);* $(;)?) => { + ($first, $( $rest ),*).0 } } +/// Helper to create an `std::io::Error` structure. +/// +/// # Example +/// ``` +/// # use mtfse::io_err; +/// let err = io_err!(Other, "some error"); +/// ``` +#[macro_export] macro_rules! io_err { + ($kind:ident, $msg:expr) => { + ::std::io::Error::new(::std::io::ErrorKind::$kind, $msg) + }; + ($msg:expr) => (io_err!(Other, $msg)); +} + + pub trait UnwrapInfallible { diff --git a/src/main.rs b/src/main.rs index 381d099..52749e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,13 +15,6 @@ use jemallocator::Jemalloc; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -macro_rules! io_err { - ($kind:ident, $msg:expr) => { - ::std::io::Error::new(::std::io::ErrorKind::$kind, $msg) - }; - ($msg:expr) => (io_err!(Other, $msg)); -} - // Utils mod ext; use ext::*; diff --git a/src/service/command.rs b/src/service/command.rs index caa2f95..982f4c4 100644 --- a/src/service/command.rs +++ b/src/service/command.rs @@ -30,6 +30,7 @@ pub struct Command resp: oneshot::Receiver>, } + impl fmt::Display for Command { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result diff --git a/src/service/mod.rs b/src/service/mod.rs index e8bfd4b..f14d569 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -6,11 +6,16 @@ use tokio::sync::{ Mutex, mpsc, + oneshot, }; pub mod command; +use command::{ + CommandKind, + CommandID, +}; -/// Handle to a running service +/// Handle to a running service. Can be used to join it or create `Channel`s. #[derive(Debug)] pub struct Handle { @@ -18,21 +23,38 @@ pub struct Handle } #[derive(Debug)] -struct CommInner +struct ChannelInner +{ + +} + + +/// The side of a `command::Command` that the running service sees. +#[derive(Debug)] +struct Request { + /// ID that corresponds to the returned `command::Command` from the `send()` function of `Channel`. + id: CommandID, + /// The actual command sent by the user. + kind: CommandKind, + /// Optional response sender. + /// + /// Just dropping this is the same as sending `None` as far as the user sees. + resp: oneshot::Sender>, } -/// Communicate with a running service +/// Communicates with a running service #[derive(Debug, Clone)] -pub struct Comm{ - inner: Arc, +pub struct Channel{ + inner: Arc, - tx: mpsc::Sender<()>, + + tx: mpsc::Sender, } -impl Eq for Comm{} -impl PartialEq for Comm { +impl Eq for Channel{} +impl PartialEq for Channel { #[inline] fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.inner, &other.inner)