diff --git a/node/network/bridge/src/lib.rs b/node/network/bridge/src/lib.rs index 725389589ead..7c844146aa8f 100644 --- a/node/network/bridge/src/lib.rs +++ b/node/network/bridge/src/lib.rs @@ -161,7 +161,7 @@ impl Network for Arc> { } /// The network bridge subsystem. -pub struct NetworkBridge(Option); +pub struct NetworkBridge(N); impl NetworkBridge { /// Create a new network bridge subsystem with underlying network service. @@ -169,25 +169,17 @@ impl NetworkBridge { /// This assumes that the network service has had the notifications protocol for the network /// bridge already registered. See [`notifications_protocol_info`](notifications_protocol_info). pub fn new(net_service: N) -> Self { - NetworkBridge(Some(net_service)) + NetworkBridge(net_service) } } impl Subsystem for NetworkBridge where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { - SpawnedSubsystem(match self.0.take() { - None => async move { for _ in ctx.recv().await { } }.boxed(), - Some(net) => { - // Swallow error because failure is fatal to the node and we log with more precision - // within `run_network`. - run_network(net, ctx).map(|_| ()).boxed() - } - }) - - - + fn start(self, ctx: C) -> SpawnedSubsystem { + // Swallow error because failure is fatal to the node and we log with more precision + // within `run_network`. + SpawnedSubsystem(run_network(self.0, ctx).map(|_| ()).boxed()) } } diff --git a/node/overseer/examples/minimal-example.rs b/node/overseer/examples/minimal-example.rs index 0edc87a6b8db..cdef0340d04f 100644 --- a/node/overseer/examples/minimal-example.rs +++ b/node/overseer/examples/minimal-example.rs @@ -74,7 +74,7 @@ impl Subsystem1 { impl Subsystem for Subsystem1 where C: SubsystemContext { - fn start(&mut self, ctx: C) -> SpawnedSubsystem { + fn start(self, ctx: C) -> SpawnedSubsystem { SpawnedSubsystem(Box::pin(async move { Self::run(ctx).await; })) @@ -111,7 +111,7 @@ impl Subsystem2 { impl Subsystem for Subsystem2 where C: SubsystemContext { - fn start(&mut self, ctx: C) -> SpawnedSubsystem { + fn start(self, ctx: C) -> SpawnedSubsystem { SpawnedSubsystem(Box::pin(async move { Self::run(ctx).await; })) @@ -129,8 +129,8 @@ fn main() { let (overseer, _handler) = Overseer::new( vec![], - Box::new(Subsystem2), - Box::new(Subsystem1), + Subsystem2, + Subsystem1, spawner, ).unwrap(); let overseer_fut = overseer.run().fuse(); diff --git a/node/overseer/src/lib.rs b/node/overseer/src/lib.rs index 8fb8706be429..8f7edb2d45ae 100644 --- a/node/overseer/src/lib.rs +++ b/node/overseer/src/lib.rs @@ -314,7 +314,6 @@ pub type CompatibleSubsystem = Box> /// [`Subsystem`]: trait.Subsystem.html #[allow(dead_code)] struct OverseenSubsystem { - subsystem: CompatibleSubsystem, instance: Option>, } @@ -438,8 +437,8 @@ where /// let spawner = executor::ThreadPool::new().unwrap(); /// let (overseer, _handler) = Overseer::new( /// vec![], - /// Box::new(ValidationSubsystem), - /// Box::new(CandidateBackingSubsystem), + /// ValidationSubsystem, + /// CandidateBackingSubsystem, /// spawner, /// ).unwrap(); /// @@ -458,8 +457,8 @@ where /// ``` pub fn new( leaves: impl IntoIterator, - validation: CompatibleSubsystem, - candidate_backing: CompatibleSubsystem, + validation: impl Subsystem> + Send, + candidate_backing: impl Subsystem> + Send, mut s: S, ) -> SubsystemResult<(Self, OverseerHandler)> { let (events_tx, events_rx) = mpsc::channel(CHANNEL_CAPACITY); @@ -658,7 +657,7 @@ fn spawn( spawner: &mut S, futures: &mut FuturesUnordered>, streams: &mut StreamUnordered>, - mut s: CompatibleSubsystem, + s: impl Subsystem>, ) -> SubsystemResult> { let (to_tx, to_rx) = mpsc::channel(CHANNEL_CAPACITY); let (from_tx, from_rx) = mpsc::channel(CHANNEL_CAPACITY); @@ -675,7 +674,6 @@ fn spawn( }); Ok(OverseenSubsystem { - subsystem: s, instance, }) } @@ -692,8 +690,8 @@ mod tests { impl Subsystem for TestSubsystem1 where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { - let mut sender = self.0.clone(); + fn start(self, mut ctx: C) -> SpawnedSubsystem { + let mut sender = self.0; SpawnedSubsystem(Box::pin(async move { let mut i = 0; loop { @@ -717,8 +715,10 @@ mod tests { impl Subsystem for TestSubsystem2 where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { + fn start(self, mut ctx: C) -> SpawnedSubsystem { + let sender = self.0.clone(); SpawnedSubsystem(Box::pin(async move { + let _sender = sender; let mut c: usize = 0; loop { if c < 10 { @@ -759,7 +759,7 @@ mod tests { impl Subsystem for TestSubsystem4 where C: SubsystemContext { - fn start(&mut self, mut _ctx: C) -> SpawnedSubsystem { + fn start(self, mut _ctx: C) -> SpawnedSubsystem { SpawnedSubsystem(Box::pin(async move { // Do nothing and exit. })) @@ -777,8 +777,8 @@ mod tests { let (overseer, mut handler) = Overseer::new( vec![], - Box::new(TestSubsystem1(s1_tx)), - Box::new(TestSubsystem2(s2_tx)), + TestSubsystem1(s1_tx), + TestSubsystem2(s2_tx), spawner, ).unwrap(); let overseer_fut = overseer.run().fuse(); @@ -827,8 +827,8 @@ mod tests { let (s1_tx, _) = mpsc::channel(64); let (overseer, _handle) = Overseer::new( vec![], - Box::new(TestSubsystem1(s1_tx)), - Box::new(TestSubsystem4), + TestSubsystem1(s1_tx), + TestSubsystem4, spawner, ).unwrap(); let overseer_fut = overseer.run().fuse(); @@ -846,7 +846,7 @@ mod tests { impl Subsystem for TestSubsystem5 where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { + fn start(self, mut ctx: C) -> SpawnedSubsystem { let mut sender = self.0.clone(); SpawnedSubsystem(Box::pin(async move { @@ -872,7 +872,7 @@ mod tests { impl Subsystem for TestSubsystem6 where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { + fn start(self, mut ctx: C) -> SpawnedSubsystem { let mut sender = self.0.clone(); SpawnedSubsystem(Box::pin(async move { @@ -925,8 +925,8 @@ mod tests { let (overseer, mut handler) = Overseer::new( vec![first_block], - Box::new(TestSubsystem5(tx_5)), - Box::new(TestSubsystem6(tx_6)), + TestSubsystem5(tx_5), + TestSubsystem6(tx_6), spawner, ).unwrap(); @@ -1010,8 +1010,8 @@ mod tests { // start with two forks of different height. let (overseer, mut handler) = Overseer::new( vec![first_block, second_block], - Box::new(TestSubsystem5(tx_5)), - Box::new(TestSubsystem6(tx_6)), + TestSubsystem5(tx_5), + TestSubsystem6(tx_6), spawner, ).unwrap(); diff --git a/node/service/src/lib.rs b/node/service/src/lib.rs index 9b917aba1b7a..88c5c81c1e0c 100644 --- a/node/service/src/lib.rs +++ b/node/service/src/lib.rs @@ -272,7 +272,7 @@ struct CandidateValidationSubsystem; impl Subsystem for CandidateValidationSubsystem where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { + fn start(self, mut ctx: C) -> SpawnedSubsystem { SpawnedSubsystem(Box::pin(async move { while let Ok(_) = ctx.recv().await {} })) @@ -284,7 +284,7 @@ struct CandidateBackingSubsystem; impl Subsystem for CandidateBackingSubsystem where C: SubsystemContext { - fn start(&mut self, mut ctx: C) -> SpawnedSubsystem { + fn start(self, mut ctx: C) -> SpawnedSubsystem { SpawnedSubsystem(Box::pin(async move { while let Ok(_) = ctx.recv().await {} })) @@ -295,8 +295,8 @@ fn real_overseer( leaves: impl IntoIterator, s: S, ) -> Result<(Overseer, OverseerHandler), ServiceError> { - let validation = Box::new(CandidateValidationSubsystem); - let candidate_backing = Box::new(CandidateBackingSubsystem); + let validation = CandidateValidationSubsystem; + let candidate_backing = CandidateBackingSubsystem; Overseer::new(leaves, validation, candidate_backing, s) .map_err(|e| ServiceError::Other(format!("Failed to create an Overseer: {:?}", e))) } diff --git a/node/subsystem/src/lib.rs b/node/subsystem/src/lib.rs index 31d094907f51..fd32d7cfdbc9 100644 --- a/node/subsystem/src/lib.rs +++ b/node/subsystem/src/lib.rs @@ -146,5 +146,5 @@ pub trait SubsystemContext: Send + 'static { /// [`Subsystem`]: trait.Subsystem.html pub trait Subsystem { /// Start this `Subsystem` and return `SpawnedSubsystem`. - fn start(&mut self, ctx: C) -> SpawnedSubsystem; + fn start(self, ctx: C) -> SpawnedSubsystem; }