diff --git a/overwatch-rs/src/services/handle.rs b/overwatch-rs/src/services/handle.rs index 77f110f..fc796d7 100644 --- a/overwatch-rs/src/services/handle.rs +++ b/overwatch-rs/src/services/handle.rs @@ -21,7 +21,7 @@ pub struct ServiceHandle { /// Handle to overwatch overwatch_handle: OverwatchHandle, settings: SettingsUpdater, - initial_state: S::State, + pub initial_state: S::State, } /// Service core resources @@ -42,6 +42,7 @@ pub struct ServiceRunner { service_state: ServiceStateHandle, state_handle: StateHandle, lifecycle_handle: LifecycleHandle, + initial_state: S::State, } impl ServiceHandle { @@ -91,7 +92,7 @@ impl ServiceHandle { // add relay channel to handle self.outbound_relay = Some(outbound_relay); let settings = self.settings.notifier().get_updated_settings(); - let operator = S::StateOperator::from_settings::(settings); + let operator = S::StateOperator::from_settings(settings); let (state_handle, state_updater) = StateHandle::::new(self.initial_state.clone(), operator); @@ -109,6 +110,7 @@ impl ServiceHandle { service_state, state_handle, lifecycle_handle, + initial_state: self.initial_state.clone(), } } } @@ -132,10 +134,11 @@ where service_state, state_handle, lifecycle_handle, + initial_state, } = self; let runtime = service_state.overwatch_handle.runtime().clone(); - let service = S::init(service_state)?; + let service = S::init(service_state, initial_state)?; runtime.spawn(service.run()); runtime.spawn(state_handle.run()); diff --git a/overwatch-rs/src/services/mod.rs b/overwatch-rs/src/services/mod.rs index 5b21843..f50adf0 100644 --- a/overwatch-rs/src/services/mod.rs +++ b/overwatch-rs/src/services/mod.rs @@ -43,7 +43,10 @@ pub trait ServiceData { #[async_trait] pub trait ServiceCore: Sized + ServiceData { /// Initialize the service with the given state - fn init(service_state: ServiceStateHandle) -> Result; + fn init( + service_state: ServiceStateHandle, + initial_state: Self::State, + ) -> Result; /// Service main loop async fn run(mut self) -> Result<(), super::DynError>; diff --git a/overwatch-rs/src/services/state.rs b/overwatch-rs/src/services/state.rs index 828e265..ae11291 100644 --- a/overwatch-rs/src/services/state.rs +++ b/overwatch-rs/src/services/state.rs @@ -31,7 +31,7 @@ pub trait StateOperator { /// The type of state that the operator can handle type StateInput: ServiceState; /// Operator initialization method. Can be implemented over some subset of settings - fn from_settings(settings: Settings) -> Self; + fn from_settings(settings: ::Settings) -> Self; /// Asynchronously perform an operation for a given state async fn run(&mut self, state: Self::StateInput); } @@ -57,7 +57,7 @@ impl Clone for NoOperator { impl StateOperator for NoOperator { type StateInput = StateInput; - fn from_settings(_settings: Settings) -> Self { + fn from_settings(_settings: ::Settings) -> Self { NoOperator(PhantomData) } @@ -230,7 +230,7 @@ mod test { impl StateOperator for PanicOnGreaterThanTen { type StateInput = UsizeCounter; - fn from_settings(_settings: Settings) -> Self { + fn from_settings(_settings: ::Settings) -> Self { Self } diff --git a/overwatch-rs/tests/cancelable_service.rs b/overwatch-rs/tests/cancelable_service.rs index e0ab96f..bc95ad1 100644 --- a/overwatch-rs/tests/cancelable_service.rs +++ b/overwatch-rs/tests/cancelable_service.rs @@ -25,7 +25,10 @@ impl ServiceData for CancellableService { #[async_trait::async_trait] impl ServiceCore for CancellableService { - fn init(service_state: ServiceStateHandle) -> Result { + fn init( + service_state: ServiceStateHandle, + _initial_state: Self::State, + ) -> Result { Ok(Self { service_state }) } diff --git a/overwatch-rs/tests/generics.rs b/overwatch-rs/tests/generics.rs index 9a88406..4363395 100644 --- a/overwatch-rs/tests/generics.rs +++ b/overwatch-rs/tests/generics.rs @@ -39,7 +39,10 @@ impl ServiceCore for GenericService where T: Debug + 'static + Sync, { - fn init(state: ServiceStateHandle) -> Result { + fn init( + state: ServiceStateHandle, + _initial_state: Self::State, + ) -> Result { Ok(Self { state, _phantom: std::marker::PhantomData, diff --git a/overwatch-rs/tests/print_service.rs b/overwatch-rs/tests/print_service.rs index b206a98..dc37538 100644 --- a/overwatch-rs/tests/print_service.rs +++ b/overwatch-rs/tests/print_service.rs @@ -28,7 +28,10 @@ impl ServiceData for PrintService { #[async_trait] impl ServiceCore for PrintService { - fn init(state: ServiceStateHandle) -> Result { + fn init( + state: ServiceStateHandle, + _initial_state: Self::State, + ) -> Result { Ok(Self { state }) } diff --git a/overwatch-rs/tests/settings_update.rs b/overwatch-rs/tests/settings_update.rs index ebe0c46..7ddaca5 100644 --- a/overwatch-rs/tests/settings_update.rs +++ b/overwatch-rs/tests/settings_update.rs @@ -29,7 +29,10 @@ impl ServiceData for SettingsService { #[async_trait] impl ServiceCore for SettingsService { - fn init(state: ServiceStateHandle) -> Result { + fn init( + state: ServiceStateHandle, + _initial_state: Self::State, + ) -> Result { Ok(Self { state }) } diff --git a/overwatch-rs/tests/state_handling.rs b/overwatch-rs/tests/state_handling.rs index 5bc30c2..fd58431 100644 --- a/overwatch-rs/tests/state_handling.rs +++ b/overwatch-rs/tests/state_handling.rs @@ -50,7 +50,7 @@ pub struct CounterStateOperator; impl StateOperator for CounterStateOperator { type StateInput = CounterState; - fn from_settings(_settings: Settings) -> Self { + fn from_settings(_settings: ::Settings) -> Self { CounterStateOperator } @@ -75,7 +75,10 @@ impl ServiceData for UpdateStateService { #[async_trait] impl ServiceCore for UpdateStateService { - fn init(state: ServiceStateHandle) -> Result { + fn init( + state: ServiceStateHandle, + _initial_state: Self::State, + ) -> Result { Ok(Self { state }) }