Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ and its version numbers follow [Semantic Versioning](https://semver.org/).
- `RuntimeHandle` now supports forwarding arbitrary control messages, including
`RuntimeControlMessage::Custom(...)`, via `control(...)` and `custom(...)`
helpers (`#20`).
- `RuntimeControlMessage::Custom(...)` is now clone-safe and no longer panics
when cloned (`#27`).

### Removed
- Unused aliases and imports producing compiler warnings.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl RuntimeGuard {
where
T: std::any::Any + Send + Sync + 'static,
{
self.control(RuntimeControlMessage::Custom(Box::new(message)))
self.control(RuntimeControlMessage::Custom(Arc::new(message)))
}

/// **Busy-wait** helper for tests and demos.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl RuntimeHandle {
where
T: std::any::Any + Send + Sync + 'static,
{
self.control(RuntimeControlMessage::Custom(Box::new(message)))
self.control(RuntimeControlMessage::Custom(Arc::new(message)))
}
}

Expand Down
18 changes: 2 additions & 16 deletions src/runtime_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,14 @@ pub enum ProcessOperation<T> {
/// wildcard arm (`_`) when pattern-matching so that new variants introduced in
/// future releases do not break compilation.
#[non_exhaustive]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum RuntimeControlMessage {
/// Trigger a *hot reload*.
Reload,
/// Request a *graceful shutdown*.
Shutdown,
/// User-defined message for custom extensions.
Custom(Box<dyn std::any::Any + Send + Sync>),
}

impl Clone for RuntimeControlMessage {
/// Manual implementation is required because the enum is
/// `#[non_exhaustive]`; remember to update this when adding new variants.
fn clone(&self) -> Self {
match self {
RuntimeControlMessage::Reload => RuntimeControlMessage::Reload,
RuntimeControlMessage::Shutdown => RuntimeControlMessage::Shutdown,
RuntimeControlMessage::Custom(_) => {
panic!("Cloning `Custom` control messages is not supported")
}
}
}
Custom(Arc<dyn std::any::Any + Send + Sync>),
}

impl<R> Runnable for Arc<R>
Expand Down
16 changes: 16 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ async fn test_runtime_handle_custom_control_message_is_delivered() {
}
}

#[test]
fn test_runtime_control_message_custom_clone_is_safe() {
let msg = RuntimeControlMessage::Custom(Arc::new(7_u8));
let cloned = msg.clone();

match cloned {
RuntimeControlMessage::Custom(payload) => {
let value = payload
.downcast::<u8>()
.expect("expected u8 custom payload");
assert_eq!(*value, 7_u8);
}
_ => panic!("expected custom control message"),
}
}

#[tokio::test]
async fn test_reload_dispatch_is_parallel() {
let mut manager = ProcessManager::new();
Expand Down
Loading