From 4efc467d8407e699676e7dde163cc18dbd31d95b Mon Sep 17 00:00:00 2001 From: Giacomo Pasini Date: Tue, 17 Jan 2023 14:45:22 +0100 Subject: [PATCH] Relax type bounds (#17) * relax type bounds * remove Send bound on NoOperator * fix tests * remove bound on service message * re-add type bound on Relay * remove duplicate PhantomBound --- overwatch-rs/src/overwatch/handle.rs | 6 +++--- overwatch-rs/src/services/handle.rs | 12 ++++++------ overwatch-rs/src/services/mod.rs | 2 +- overwatch-rs/src/services/relay.rs | 27 +++++++++++++++------------ overwatch-rs/src/services/state.rs | 15 ++++++++++++--- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/overwatch-rs/src/overwatch/handle.rs b/overwatch-rs/src/overwatch/handle.rs index 1b52d82..fe64876 100644 --- a/overwatch-rs/src/overwatch/handle.rs +++ b/overwatch-rs/src/overwatch/handle.rs @@ -3,13 +3,13 @@ // crates use crate::overwatch::commands::{OverwatchCommand, OverwatchLifeCycleCommand, SettingsCommand}; use crate::overwatch::Services; +use crate::services::ServiceData; use tokio::runtime::Handle; use tokio::sync::mpsc::Sender; use tracing::{error, info, instrument}; // internal use crate::services::relay::Relay; -use crate::services::ServiceCore; /// Handler object over the main Overwatch runner /// It handles communications to the main Overwatch runner. @@ -28,8 +28,8 @@ impl OverwatchHandle { } } - /// Request for a relay to an specific service by type - pub fn relay(&self) -> Relay { + /// Request for a relay + pub fn relay(&self) -> Relay { Relay::new(self.clone()) } diff --git a/overwatch-rs/src/services/handle.rs b/overwatch-rs/src/services/handle.rs index af392ca..a5085dc 100644 --- a/overwatch-rs/src/services/handle.rs +++ b/overwatch-rs/src/services/handle.rs @@ -6,14 +6,14 @@ use crate::overwatch::handle::OverwatchHandle; use crate::services::relay::{relay, InboundRelay, OutboundRelay}; use crate::services::settings::{SettingsNotifier, SettingsUpdater}; use crate::services::state::{StateHandle, StateOperator, StateUpdater}; -use crate::services::{ServiceCore, ServiceId, ServiceState}; +use crate::services::{ServiceCore, ServiceData, ServiceId, ServiceState}; // TODO: Abstract handle over state, to diferentiate when the service is running and when it is not // that way we can expose a better API depending on what is happenning. Would get rid of the probably // unnecessary Option and cloning. /// Service handle /// This is used to access different parts of the service -pub struct ServiceHandle { +pub struct ServiceHandle { /// Message channel relay /// Would be None if service is not running /// Will contain the channel if service is running @@ -26,7 +26,7 @@ pub struct ServiceHandle { /// Service core resources /// It contains whatever is necessary to start a new service runner -pub struct ServiceStateHandle { +pub struct ServiceStateHandle { /// Relay channel to communicate with the service runner pub inbound_relay: InboundRelay, /// Overwatch handle @@ -38,12 +38,12 @@ pub struct ServiceStateHandle { /// Main service executor /// It is the object that hold the necessary information for the service to run -pub struct ServiceRunner { +pub struct ServiceRunner { service_state: ServiceStateHandle, state_handle: StateHandle, } -impl ServiceHandle { +impl ServiceHandle { pub fn new( settings: S::Settings, overwatch_handle: OverwatchHandle, @@ -109,7 +109,7 @@ impl ServiceHandle { } } -impl ServiceStateHandle { +impl ServiceStateHandle { pub fn id(&self) -> ServiceId { S::SERVICE_ID } diff --git a/overwatch-rs/src/services/mod.rs b/overwatch-rs/src/services/mod.rs index e107b38..5b21843 100644 --- a/overwatch-rs/src/services/mod.rs +++ b/overwatch-rs/src/services/mod.rs @@ -36,7 +36,7 @@ pub trait ServiceData { /// State operator type StateOperator: StateOperator + Clone; /// Service messages that the service itself understands and can react to - type Message: RelayMessage + Debug + Send + Sync; + type Message: RelayMessage + Debug; } /// Main trait for Services initialization and main loop hook diff --git a/overwatch-rs/src/services/relay.rs b/overwatch-rs/src/services/relay.rs index 9f17fb4..f250472 100644 --- a/overwatch-rs/src/services/relay.rs +++ b/overwatch-rs/src/services/relay.rs @@ -14,7 +14,7 @@ use tracing::{error, instrument}; // internal use crate::overwatch::commands::{OverwatchCommand, RelayCommand, ReplyChannel}; use crate::overwatch::handle::OverwatchHandle; -use crate::services::{ServiceCore, ServiceId}; +use crate::services::{ServiceData, ServiceId}; #[derive(Error, Debug)] pub enum RelayError { @@ -66,20 +66,21 @@ pub struct OutboundRelay { } #[derive(Debug)] -pub struct Relay { - _marker: PhantomData, +pub struct Relay { overwatch_handle: OverwatchHandle, + _bound: PhantomBound, } -impl Clone for Relay { - fn clone(&self) -> Self { - Self { - _marker: PhantomData, - overwatch_handle: self.overwatch_handle.clone(), - } - } +// Like PhantomData but without +// ownership of T +#[derive(Debug)] +struct PhantomBound { + _inner: PhantomData<*const T>, } +unsafe impl Send for PhantomBound {} +unsafe impl Sync for PhantomBound {} + impl Clone for OutboundRelay { fn clone(&self) -> Self { Self { @@ -142,11 +143,13 @@ impl OutboundRelay { } } -impl Relay { +impl Relay { pub fn new(overwatch_handle: OverwatchHandle) -> Self { Self { overwatch_handle, - _marker: PhantomData, + _bound: PhantomBound { + _inner: PhantomData, + }, } } diff --git a/overwatch-rs/src/services/state.rs b/overwatch-rs/src/services/state.rs index 24ceefa..f591f61 100644 --- a/overwatch-rs/src/services/state.rs +++ b/overwatch-rs/src/services/state.rs @@ -1,7 +1,7 @@ // std use std::marker::PhantomData; +use std::pin::Pin; use std::sync::Arc; - // crates use async_trait::async_trait; use futures::StreamExt; @@ -54,14 +54,23 @@ impl Clone for NoOperator { } #[async_trait] -impl StateOperator for NoOperator { +impl StateOperator for NoOperator { type StateInput = StateInput; fn from_settings(_settings: Settings) -> Self { NoOperator(PhantomData::default()) } - async fn run(&mut self, _state: Self::StateInput) {} + fn run<'borrow, 'fut>( + &'borrow mut self, + _state: Self::StateInput, + ) -> Pin + Send + 'fut>> + where + 'borrow: 'fut, + Self: 'fut, + { + Box::pin(async {}) + } } /// Empty state