-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor of runtime interface (CI will fail)
This is a major refactor of the runtime interface which makes it *much* easier to program against in a generic way. The most obvious changes are (1) the interface is no longer split into sync/unsync variants, (2) the runtime interface no longer requires a lifetime annotation on the machine type. The type annotations required on callback arguments have gotten a little bit harrier... I really wish Rust was better at inferring the types of closure arguments--in this case all the info is obviously there to do it. The runtime interface and its demos and tests have been updated and are working, but the code generator and integration tests have not been updated yet, so CI is expected to fail.
- Loading branch information
Showing
11 changed files
with
798 additions
and
900 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! This module defines callbacks that can be registered with a state machine's `EventMonitor`. | ||
use std::sync::{Arc, Mutex}; | ||
|
||
/// Trait for wrappers around callback functions that have a name and accept a reference to `Arg` | ||
/// as an argument. | ||
pub trait IsCallback<Arg> { | ||
/// A name/ID associated with this callback to enable removing it later. | ||
fn name(&self) -> &str; | ||
|
||
/// Apply the wrapped function. | ||
fn apply(&mut self, arg: &Arg); | ||
} | ||
|
||
/// A named callback function that accepts a reference to `Arg` as an argument. | ||
pub struct Callback<Arg> { | ||
name: String, | ||
closure: Box<dyn FnMut(&Arg) + 'static>, | ||
} | ||
|
||
impl<Arg> Callback<Arg> { | ||
/// Create a new callback from the given closure. | ||
pub fn new(name: &str, f: impl FnMut(&Arg) + 'static) -> Self { | ||
Callback { | ||
closure: Box::new(f), | ||
name: name.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl<Arg> IsCallback<Arg> for Callback<Arg> { | ||
fn name(&self) -> &str { | ||
&self.name | ||
} | ||
fn apply(&mut self, arg: &Arg) { | ||
(*self.closure)(arg) | ||
} | ||
} | ||
|
||
/// A named callback function that accepts a reference to `Arg` as an argument and implements the | ||
/// `Send` trait. | ||
pub struct CallbackSend<Arg> { | ||
name: String, | ||
closure: Arc<Mutex<dyn FnMut(&Arg) + Send + 'static>>, | ||
} | ||
|
||
impl<Arg> CallbackSend<Arg> { | ||
/// Create a new callback from the given closure. | ||
pub fn new(name: &str, f: impl FnMut(&Arg) + Send + 'static) -> Self { | ||
CallbackSend { | ||
closure: Arc::new(Mutex::new(f)), | ||
name: name.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl<Arg> IsCallback<Arg> for CallbackSend<Arg> { | ||
fn name(&self) -> &str { | ||
&self.name | ||
} | ||
fn apply(&mut self, arg: &Arg) { | ||
(*self.closure.lock().unwrap())(arg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.