Skip to content

Commit d2bab1d

Browse files
authored
Merge pull request #70 from frame-lang/unified-runtime
Refactor and unification of runtime interface
2 parents 6029bff + da23199 commit d2bab1d

24 files changed

+1336
-1229
lines changed

frame_runtime/src/callback.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! This module defines wrappers for callbacks that can be registered with a state machine's
2+
//! [EventMonitor](crate::event::EventMonitor). Use the [Callback] wrapper if the state machine was
3+
//! generated with the Framec feature `thread_safe` set to `false` . Use the [CallbackSend] wrapper
4+
//! if the state machine was generated with `thread_safe=true`.
5+
6+
use std::sync::{Arc, Mutex};
7+
8+
/// Trait for wrappers around callback functions that have a name and accept a reference to `Arg`
9+
/// as an argument.
10+
pub trait IsCallback<Arg> {
11+
/// A name/ID associated with this callback to enable removing it later.
12+
fn name(&self) -> &str;
13+
14+
/// Apply the wrapped function.
15+
fn apply(&mut self, arg: &Arg);
16+
}
17+
18+
/// A named callback function that accepts a reference to `Arg` as an argument. Use this struct to
19+
/// wrap callbacks if the state machine was generated with `thread_safe=false`.
20+
pub struct Callback<Arg> {
21+
name: String,
22+
closure: Box<dyn FnMut(&Arg) + 'static>,
23+
}
24+
25+
impl<Arg> Callback<Arg> {
26+
/// Create a new callback from the given closure.
27+
pub fn new(name: &str, f: impl FnMut(&Arg) + 'static) -> Self {
28+
Callback {
29+
closure: Box::new(f),
30+
name: name.to_string(),
31+
}
32+
}
33+
}
34+
35+
impl<Arg> IsCallback<Arg> for Callback<Arg> {
36+
fn name(&self) -> &str {
37+
&self.name
38+
}
39+
fn apply(&mut self, arg: &Arg) {
40+
(*self.closure)(arg)
41+
}
42+
}
43+
44+
/// A named callback function that accepts a reference to `Arg` as an argument and implements the
45+
/// [Send] trait. Use this struct to wrap callbacks if the state machine was generated with
46+
/// `thread_safe=true`.
47+
pub struct CallbackSend<Arg> {
48+
name: String,
49+
closure: Arc<Mutex<dyn FnMut(&Arg) + Send + 'static>>,
50+
}
51+
52+
impl<Arg> CallbackSend<Arg> {
53+
/// Create a new callback from the given closure.
54+
pub fn new(name: &str, f: impl FnMut(&Arg) + Send + 'static) -> Self {
55+
CallbackSend {
56+
closure: Arc::new(Mutex::new(f)),
57+
name: name.to_string(),
58+
}
59+
}
60+
}
61+
62+
impl<Arg> IsCallback<Arg> for CallbackSend<Arg> {
63+
fn name(&self) -> &str {
64+
&self.name
65+
}
66+
fn apply(&mut self, arg: &Arg) {
67+
(*self.closure.lock().unwrap())(arg)
68+
}
69+
}

frame_runtime/src/env.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,6 @@ impl<T: Environment + Clone> Environment for Mutex<T> {
8080
}
8181
}
8282

83-
/// Definitions specific to the synchronized/thread-safe interface.
84-
pub mod sync {
85-
pub use super::*;
86-
use std::sync::Arc;
87-
88-
/// A reference-counted pointer to an environment.
89-
pub type EnvironmentPtr = Arc<dyn super::Environment>;
90-
}
91-
92-
/// Definitions specific to the unsynchronized interface.
93-
pub mod unsync {
94-
pub use super::*;
95-
use std::rc::Rc;
96-
97-
/// A reference-counted pointer to an environment.
98-
pub type EnvironmentPtr = Rc<dyn super::Environment>;
99-
}
100-
10183
#[allow(clippy::approx_constant)]
10284
#[cfg(test)]
10385
mod tests {

0 commit comments

Comments
 (0)