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

Commit

Permalink
SimpleSlotWorker: Do not implement SlotWorker for all types impleme…
Browse files Browse the repository at this point in the history
…nting `SimpleSlotWorker` (#10934)

Because Rust currently doesn't support specialization, it prevents users from implementing
`SlotWorker` for their own types. This pr solves this by removing the generic implementation of
`SlotWorker` for `SimpleSlotWorker` and providing some wrapper type for that.
  • Loading branch information
bkchr authored Mar 2, 2022
1 parent a7a4c68 commit f4fb93f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ where
L: sc_consensus::JustificationSyncLink<B>,
BS: BackoffAuthoringBlocksStrategy<NumberFor<B>> + Send + Sync + 'static,
{
AuraWorker {
sc_consensus_slots::SimpleSlotWorkerToSlotWorker(AuraWorker {
client,
block_import,
env: proposer_factory,
Expand All @@ -292,7 +292,7 @@ where
block_proposal_slot_portion,
max_block_proposal_slot_portion,
_key_type: PhantomData::<P>,
}
})
}

struct AuraWorker<C, E, I, P, SO, L, BS> {
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ where
let slot_worker = sc_consensus_slots::start_slot_worker(
babe_link.config.slot_duration(),
select_chain,
worker,
sc_consensus_slots::SimpleSlotWorkerToSlotWorker(worker),
sync_oracle,
create_inherent_data_providers,
can_author_with,
Expand Down
13 changes: 10 additions & 3 deletions client/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,22 @@ pub trait SimpleSlotWorker<B: BlockT> {
}
}

/// A type that implements [`SlotWorker`] for a type that implements [`SimpleSlotWorker`].
///
/// This is basically a workaround for Rust not supporting specialization. Otherwise we could
/// implement [`SlotWorker`] for any `T` that implements [`SimpleSlotWorker`], but currently
/// that would prevent downstream users to implement [`SlotWorker`] for their own types.
pub struct SimpleSlotWorkerToSlotWorker<T>(pub T);

#[async_trait::async_trait]
impl<B: BlockT, T: SimpleSlotWorker<B> + Send + Sync>
SlotWorker<B, <T::Proposer as Proposer<B>>::Proof> for T
impl<T: SimpleSlotWorker<B> + Send + Sync, B: BlockT>
SlotWorker<B, <T::Proposer as Proposer<B>>::Proof> for SimpleSlotWorkerToSlotWorker<T>
{
async fn on_slot(
&mut self,
slot_info: SlotInfo<B>,
) -> Option<SlotResult<B, <T::Proposer as Proposer<B>>::Proof>> {
SimpleSlotWorker::on_slot(self, slot_info).await
self.0.on_slot(slot_info).await
}
}

Expand Down

0 comments on commit f4fb93f

Please sign in to comment.