Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 17650ce

Browse files
authored
Subsystem::start takes self by-value (#1325)
* Subsystem::start takes self by-value * fix doc-test compilation
1 parent 7a7d41c commit 17650ce

File tree

5 files changed

+38
-46
lines changed

5 files changed

+38
-46
lines changed

node/network/bridge/src/lib.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,15 @@ impl Network for Arc<sc_network::NetworkService<Block, Hash>> {
164164
}
165165

166166
/// The network bridge subsystem.
167-
pub struct NetworkBridge<N>(Option<N>);
167+
pub struct NetworkBridge<N>(N);
168168

169169
impl<N> NetworkBridge<N> {
170170
/// Create a new network bridge subsystem with underlying network service.
171171
///
172172
/// This assumes that the network service has had the notifications protocol for the network
173173
/// bridge already registered. See [`notifications_protocol_info`](notifications_protocol_info).
174174
pub fn new(net_service: N) -> Self {
175-
NetworkBridge(Some(net_service))
175+
NetworkBridge(net_service)
176176
}
177177
}
178178

@@ -181,18 +181,10 @@ impl<Net, Context> Subsystem<Context> for NetworkBridge<Net>
181181
Net: Network,
182182
Context: SubsystemContext<Message=NetworkBridgeMessage>,
183183
{
184-
fn start(&mut self, mut ctx: Context) -> SpawnedSubsystem {
185-
SpawnedSubsystem(match self.0.take() {
186-
None => async move { for _ in ctx.recv().await { } }.boxed(),
187-
Some(net) => {
188-
// Swallow error because failure is fatal to the node and we log with more precision
189-
// within `run_network`.
190-
run_network(net, ctx).map(|_| ()).boxed()
191-
}
192-
})
193-
194-
195-
184+
fn start(self, ctx: Context) -> SpawnedSubsystem {
185+
// Swallow error because failure is fatal to the node and we log with more precision
186+
// within `run_network`.
187+
SpawnedSubsystem(run_network(self.0, ctx).map(|_| ()).boxed())
196188
}
197189
}
198190

node/overseer/examples/minimal-example.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Subsystem1 {
7474
impl<C> Subsystem<C> for Subsystem1
7575
where C: SubsystemContext<Message=CandidateBackingMessage>
7676
{
77-
fn start(&mut self, ctx: C) -> SpawnedSubsystem {
77+
fn start(self, ctx: C) -> SpawnedSubsystem {
7878
SpawnedSubsystem(Box::pin(async move {
7979
Self::run(ctx).await;
8080
}))
@@ -111,7 +111,7 @@ impl Subsystem2 {
111111
impl<C> Subsystem<C> for Subsystem2
112112
where C: SubsystemContext<Message=CandidateValidationMessage>
113113
{
114-
fn start(&mut self, ctx: C) -> SpawnedSubsystem {
114+
fn start(self, ctx: C) -> SpawnedSubsystem {
115115
SpawnedSubsystem(Box::pin(async move {
116116
Self::run(ctx).await;
117117
}))
@@ -129,8 +129,8 @@ fn main() {
129129

130130
let (overseer, _handler) = Overseer::new(
131131
vec![],
132-
Box::new(Subsystem2),
133-
Box::new(Subsystem1),
132+
Subsystem2,
133+
Subsystem1,
134134
spawner,
135135
).unwrap();
136136
let overseer_fut = overseer.run().fuse();

node/overseer/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ pub type CompatibleSubsystem<M> = Box<dyn Subsystem<OverseerSubsystemContext<M>>
314314
/// [`Subsystem`]: trait.Subsystem.html
315315
#[allow(dead_code)]
316316
struct OverseenSubsystem<M> {
317-
subsystem: CompatibleSubsystem<M>,
318317
instance: Option<SubsystemInstance<M>>,
319318
}
320319

@@ -407,7 +406,7 @@ where
407406
/// where C: SubsystemContext<Message=CandidateValidationMessage>
408407
/// {
409408
/// fn start(
410-
/// &mut self,
409+
/// self,
411410
/// mut ctx: C,
412411
/// ) -> SpawnedSubsystem {
413412
/// SpawnedSubsystem(Box::pin(async move {
@@ -423,7 +422,7 @@ where
423422
/// where C: SubsystemContext<Message=CandidateBackingMessage>
424423
/// {
425424
/// fn start(
426-
/// &mut self,
425+
/// self,
427426
/// mut ctx: C,
428427
/// ) -> SpawnedSubsystem {
429428
/// SpawnedSubsystem(Box::pin(async move {
@@ -438,8 +437,8 @@ where
438437
/// let spawner = executor::ThreadPool::new().unwrap();
439438
/// let (overseer, _handler) = Overseer::new(
440439
/// vec![],
441-
/// Box::new(ValidationSubsystem),
442-
/// Box::new(CandidateBackingSubsystem),
440+
/// ValidationSubsystem,
441+
/// CandidateBackingSubsystem,
443442
/// spawner,
444443
/// ).unwrap();
445444
///
@@ -458,8 +457,8 @@ where
458457
/// ```
459458
pub fn new(
460459
leaves: impl IntoIterator<Item = BlockInfo>,
461-
validation: CompatibleSubsystem<CandidateValidationMessage>,
462-
candidate_backing: CompatibleSubsystem<CandidateBackingMessage>,
460+
validation: impl Subsystem<OverseerSubsystemContext<CandidateValidationMessage>> + Send,
461+
candidate_backing: impl Subsystem<OverseerSubsystemContext<CandidateBackingMessage>> + Send,
463462
mut s: S,
464463
) -> SubsystemResult<(Self, OverseerHandler)> {
465464
let (events_tx, events_rx) = mpsc::channel(CHANNEL_CAPACITY);
@@ -658,7 +657,7 @@ fn spawn<S: Spawn, M: Send + 'static>(
658657
spawner: &mut S,
659658
futures: &mut FuturesUnordered<RemoteHandle<()>>,
660659
streams: &mut StreamUnordered<mpsc::Receiver<ToOverseer>>,
661-
mut s: CompatibleSubsystem<M>,
660+
s: impl Subsystem<OverseerSubsystemContext<M>>,
662661
) -> SubsystemResult<OverseenSubsystem<M>> {
663662
let (to_tx, to_rx) = mpsc::channel(CHANNEL_CAPACITY);
664663
let (from_tx, from_rx) = mpsc::channel(CHANNEL_CAPACITY);
@@ -675,7 +674,6 @@ fn spawn<S: Spawn, M: Send + 'static>(
675674
});
676675

677676
Ok(OverseenSubsystem {
678-
subsystem: s,
679677
instance,
680678
})
681679
}
@@ -692,8 +690,8 @@ mod tests {
692690
impl<C> Subsystem<C> for TestSubsystem1
693691
where C: SubsystemContext<Message=CandidateValidationMessage>
694692
{
695-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
696-
let mut sender = self.0.clone();
693+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
694+
let mut sender = self.0;
697695
SpawnedSubsystem(Box::pin(async move {
698696
let mut i = 0;
699697
loop {
@@ -717,8 +715,10 @@ mod tests {
717715
impl<C> Subsystem<C> for TestSubsystem2
718716
where C: SubsystemContext<Message=CandidateBackingMessage>
719717
{
720-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
718+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
719+
let sender = self.0.clone();
721720
SpawnedSubsystem(Box::pin(async move {
721+
let _sender = sender;
722722
let mut c: usize = 0;
723723
loop {
724724
if c < 10 {
@@ -759,7 +759,7 @@ mod tests {
759759
impl<C> Subsystem<C> for TestSubsystem4
760760
where C: SubsystemContext<Message=CandidateBackingMessage>
761761
{
762-
fn start(&mut self, mut _ctx: C) -> SpawnedSubsystem {
762+
fn start(self, mut _ctx: C) -> SpawnedSubsystem {
763763
SpawnedSubsystem(Box::pin(async move {
764764
// Do nothing and exit.
765765
}))
@@ -777,8 +777,8 @@ mod tests {
777777

778778
let (overseer, mut handler) = Overseer::new(
779779
vec![],
780-
Box::new(TestSubsystem1(s1_tx)),
781-
Box::new(TestSubsystem2(s2_tx)),
780+
TestSubsystem1(s1_tx),
781+
TestSubsystem2(s2_tx),
782782
spawner,
783783
).unwrap();
784784
let overseer_fut = overseer.run().fuse();
@@ -827,8 +827,8 @@ mod tests {
827827
let (s1_tx, _) = mpsc::channel(64);
828828
let (overseer, _handle) = Overseer::new(
829829
vec![],
830-
Box::new(TestSubsystem1(s1_tx)),
831-
Box::new(TestSubsystem4),
830+
TestSubsystem1(s1_tx),
831+
TestSubsystem4,
832832
spawner,
833833
).unwrap();
834834
let overseer_fut = overseer.run().fuse();
@@ -846,7 +846,7 @@ mod tests {
846846
impl<C> Subsystem<C> for TestSubsystem5
847847
where C: SubsystemContext<Message=CandidateValidationMessage>
848848
{
849-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
849+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
850850
let mut sender = self.0.clone();
851851

852852
SpawnedSubsystem(Box::pin(async move {
@@ -872,7 +872,7 @@ mod tests {
872872
impl<C> Subsystem<C> for TestSubsystem6
873873
where C: SubsystemContext<Message=CandidateBackingMessage>
874874
{
875-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
875+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
876876
let mut sender = self.0.clone();
877877

878878
SpawnedSubsystem(Box::pin(async move {
@@ -925,8 +925,8 @@ mod tests {
925925

926926
let (overseer, mut handler) = Overseer::new(
927927
vec![first_block],
928-
Box::new(TestSubsystem5(tx_5)),
929-
Box::new(TestSubsystem6(tx_6)),
928+
TestSubsystem5(tx_5),
929+
TestSubsystem6(tx_6),
930930
spawner,
931931
).unwrap();
932932

@@ -1010,8 +1010,8 @@ mod tests {
10101010
// start with two forks of different height.
10111011
let (overseer, mut handler) = Overseer::new(
10121012
vec![first_block, second_block],
1013-
Box::new(TestSubsystem5(tx_5)),
1014-
Box::new(TestSubsystem6(tx_6)),
1013+
TestSubsystem5(tx_5),
1014+
TestSubsystem6(tx_6),
10151015
spawner,
10161016
).unwrap();
10171017

node/service/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ struct CandidateValidationSubsystem;
272272
impl<C> Subsystem<C> for CandidateValidationSubsystem
273273
where C: SubsystemContext<Message = CandidateValidationMessage>
274274
{
275-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
275+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
276276
SpawnedSubsystem(Box::pin(async move {
277277
while let Ok(_) = ctx.recv().await {}
278278
}))
@@ -284,7 +284,7 @@ struct CandidateBackingSubsystem;
284284
impl<C> Subsystem<C> for CandidateBackingSubsystem
285285
where C: SubsystemContext<Message = CandidateBackingMessage>
286286
{
287-
fn start(&mut self, mut ctx: C) -> SpawnedSubsystem {
287+
fn start(self, mut ctx: C) -> SpawnedSubsystem {
288288
SpawnedSubsystem(Box::pin(async move {
289289
while let Ok(_) = ctx.recv().await {}
290290
}))
@@ -295,8 +295,8 @@ fn real_overseer<S: futures::task::Spawn>(
295295
leaves: impl IntoIterator<Item = BlockInfo>,
296296
s: S,
297297
) -> Result<(Overseer<S>, OverseerHandler), ServiceError> {
298-
let validation = Box::new(CandidateValidationSubsystem);
299-
let candidate_backing = Box::new(CandidateBackingSubsystem);
298+
let validation = CandidateValidationSubsystem;
299+
let candidate_backing = CandidateBackingSubsystem;
300300
Overseer::new(leaves, validation, candidate_backing, s)
301301
.map_err(|e| ServiceError::Other(format!("Failed to create an Overseer: {:?}", e)))
302302
}

node/subsystem/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,5 @@ pub trait SubsystemContext: Send + 'static {
146146
/// [`Subsystem`]: trait.Subsystem.html
147147
pub trait Subsystem<C: SubsystemContext> {
148148
/// Start this `Subsystem` and return `SpawnedSubsystem`.
149-
fn start(&mut self, ctx: C) -> SpawnedSubsystem;
149+
fn start(self, ctx: C) -> SpawnedSubsystem;
150150
}

0 commit comments

Comments
 (0)