Create a more rigid overseer builder pattern that fails at compile time (#4753)

Introduces `Missing<Field>` and `Init<Field>` states, that are used in place of builder generics, and make this possible.
This commit is contained in:
Vsevolod Stakhov
2022-02-09 16:01:16 +00:00
committed by GitHub
parent 227e39bff6
commit 84f55cc8d5
20 changed files with 891 additions and 267 deletions
@@ -1,5 +1,5 @@
error[E0119]: conflicting implementations of trait `std::convert::From<MsgStrukt>` for type `AllMessages`
--> $DIR/err-01-duplicate-consumer.rs:19:1
--> tests/ui/err-01-duplicate-consumer.rs:19:1
|
19 | #[overlord(signal=SigSigSig, event=Event, gen=AllMessages, error=OverseerError)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -10,7 +10,7 @@ error[E0119]: conflicting implementations of trait `std::convert::From<MsgStrukt
= note: this error originates in the attribute macro `overlord` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0119]: conflicting implementations of trait `polkadot_overseer_gen::SubsystemSender<MsgStrukt>` for type `OverseerSubsystemSender`
--> $DIR/err-01-duplicate-consumer.rs:19:1
--> tests/ui/err-01-duplicate-consumer.rs:19:1
|
19 | #[overlord(signal=SigSigSig, event=Event, gen=AllMessages, error=OverseerError)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -0,0 +1,61 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSys;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgStrukt>, OverseerError> for AwesomeSubSys {
fn start(self, _ctx: OverseerSubsystemContext<MsgStrukt>) -> SpawnedSubsystem<OverseerError> {
unimplemented!("starting yay!")
}
}
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgStrukt(u8);
#[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
struct Overseer {
#[subsystem(no_dispatch, MsgStrukt)]
sub0: AwesomeSubSys,
i_like_pie: f64,
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
struct DummyCtx;
fn main() {
let _ = Overseer::builder()
.sub0(AwesomeSubSys::default())
//.i_like_pie(std::f64::consts::PI) // The filed is not initialised
.spawner(DummySpawner)
.build()
.unwrap();
}
@@ -0,0 +1,15 @@
error[E0599]: no method named `build` found for struct `OverseerBuilder<Init<DummySpawner>, Init<AwesomeSubSys>, Missing<f64>>` in the current scope
--> tests/ui/err-05-missing-field.rs:59:4
|
22 | #[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
| -------------------------------------------------------------------------------- method `build` not found for this
...
59 | .build()
| ^^^^^ method not found in `OverseerBuilder<Init<DummySpawner>, Init<AwesomeSubSys>, Missing<f64>>`
|
= note: the method was found for
- `OverseerBuilder<Init<S>, Init<AwesomeSubSys>, Init<f64>>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `build`, perhaps you need to implement one of them:
candidate #1: `frame_support::traits::hooks::GenesisBuild`
candidate #2: `prometheus::vec::MetricVecBuilder`
@@ -0,0 +1,61 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSys;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgStrukt>, OverseerError> for AwesomeSubSys {
fn start(self, _ctx: OverseerSubsystemContext<MsgStrukt>) -> SpawnedSubsystem<OverseerError> {
unimplemented!("starting yay!")
}
}
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgStrukt(u8);
#[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
struct Overseer {
#[subsystem(no_dispatch, MsgStrukt)]
sub0: AwesomeSubSys,
i_like_pie: f64,
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
struct DummyCtx;
fn main() {
let _ = Overseer::builder()
//.sub0(AwesomeSubSys::default()) // Subsystem is uninitialized
.i_like_pie(std::f64::consts::PI)
.spawner(DummySpawner)
.build()
.unwrap();
}
@@ -0,0 +1,15 @@
error[E0599]: no method named `build` found for struct `OverseerBuilder<Init<DummySpawner>, Missing<_>, Init<f64>>` in the current scope
--> tests/ui/err-06-missing-subsystem.rs:59:4
|
22 | #[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
| -------------------------------------------------------------------------------- method `build` not found for this
...
59 | .build()
| ^^^^^ method not found in `OverseerBuilder<Init<DummySpawner>, Missing<_>, Init<f64>>`
|
= note: the method was found for
- `OverseerBuilder<Init<S>, Init<AwesomeSubSys>, Init<f64>>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `build`, perhaps you need to implement one of them:
candidate #1: `frame_support::traits::hooks::GenesisBuild`
candidate #2: `prometheus::vec::MetricVecBuilder`
@@ -0,0 +1,61 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSys;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgStrukt>, OverseerError> for AwesomeSubSys {
fn start(self, _ctx: OverseerSubsystemContext<MsgStrukt>) -> SpawnedSubsystem<OverseerError> {
unimplemented!("starting yay!")
}
}
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgStrukt(u8);
#[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
struct Overseer {
#[subsystem(no_dispatch, MsgStrukt)]
sub0: AwesomeSubSys,
i_like_pie: f64,
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
struct DummyCtx;
fn main() {
let _ = Overseer::builder()
.sub0(AwesomeSubSys::default())
.i_like_pie(std::f64::consts::PI)
//.spawner(DummySpawner) // Spawner is missing
.build()
.unwrap();
}
@@ -0,0 +1,15 @@
error[E0599]: no method named `build` found for struct `OverseerBuilder<Missing<_>, Init<AwesomeSubSys>, Init<f64>>` in the current scope
--> tests/ui/err-07-missing-spawner.rs:59:4
|
22 | #[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
| -------------------------------------------------------------------------------- method `build` not found for this
...
59 | .build()
| ^^^^^ method not found in `OverseerBuilder<Missing<_>, Init<AwesomeSubSys>, Init<f64>>`
|
= note: the method was found for
- `OverseerBuilder<Init<S>, Init<AwesomeSubSys>, Init<f64>>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `build`, perhaps you need to implement one of them:
candidate #1: `frame_support::traits::hooks::GenesisBuild`
candidate #2: `prometheus::vec::MetricVecBuilder`
@@ -0,0 +1,62 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSys;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgStrukt>, OverseerError> for AwesomeSubSys {
fn start(self, _ctx: OverseerSubsystemContext<MsgStrukt>) -> SpawnedSubsystem<OverseerError> {
unimplemented!("starting yay!")
}
}
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgStrukt(u8);
#[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
struct Overseer {
#[subsystem(no_dispatch, MsgStrukt)]
sub0: AwesomeSubSys,
i_like_pie: f64,
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
struct DummyCtx;
fn main() {
let _ = Overseer::builder()
.sub0(AwesomeSubSys::default())
.sub0(AwesomeSubSys::default()) // Duplicate subsystem
.i_like_pie(std::f64::consts::PI)
.spawner(DummySpawner)
.build()
.unwrap();
}
@@ -0,0 +1,10 @@
error[E0599]: no method named `sub0` found for struct `OverseerBuilder<Missing<_>, Init<AwesomeSubSys>, Missing<f64>>` in the current scope
--> tests/ui/err-08-duplicate-subsystem.rs:57:4
|
22 | #[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
| -------------------------------------------------------------------------------- method `sub0` not found for this
...
57 | .sub0(AwesomeSubSys::default()) // Duplicate subsystem
| ^^^^-------------------------- help: remove the arguments
| |
| field, not a method
@@ -0,0 +1,61 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSys;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgStrukt>, OverseerError> for AwesomeSubSys {
fn start(self, _ctx: OverseerSubsystemContext<MsgStrukt>) -> SpawnedSubsystem<OverseerError> {
unimplemented!("starting yay!")
}
}
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgStrukt(u8);
#[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
struct Overseer<T> {
#[subsystem(no_dispatch, MsgStrukt)]
sub0: AwesomeSubSys,
i_like_pie: T,
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
unimplemented!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
struct DummyCtx;
fn main() {
let (_, _): (Overseer<_, f64>, _) = Overseer::builder()
.sub0(AwesomeSubSys::default())
//.i_like_pie(std::f64::consts::PI) // The filed is not initialised
.spawner(DummySpawner)
.build()
.unwrap();
}
@@ -0,0 +1,15 @@
error[E0599]: no method named `build` found for struct `OverseerBuilder<Init<DummySpawner>, Init<AwesomeSubSys>, Missing<_>>` in the current scope
--> tests/ui/err-09-uninit_generic_baggage.rs:59:4
|
22 | #[overlord(signal=SigSigSig, error=OverseerError, event=Event, gen=AllMessages)]
| -------------------------------------------------------------------------------- method `build` not found for this
...
59 | .build()
| ^^^^^ method not found in `OverseerBuilder<Init<DummySpawner>, Init<AwesomeSubSys>, Missing<_>>`
|
= note: the method was found for
- `OverseerBuilder<Init<S>, Init<AwesomeSubSys>, Init<T>>`
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following traits define an item `build`, perhaps you need to implement one of them:
candidate #1: `frame_support::traits::hooks::GenesisBuild`
candidate #2: `prometheus::vec::MetricVecBuilder`
@@ -0,0 +1,75 @@
#![allow(dead_code)]
use polkadot_overseer_gen::*;
#[derive(Default)]
struct AwesomeSubSysA;
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgA>, OverseerError> for AwesomeSubSysA {
fn start(self, _ctx: OverseerSubsystemContext<MsgA>) -> SpawnedSubsystem<OverseerError> {
SpawnedSubsystem { name: "sub A", future: Box::pin(async move { Ok(()) }) }
}
}
impl ::polkadot_overseer_gen::Subsystem<OverseerSubsystemContext<MsgB>, OverseerError> for AwesomeSubSysB {
fn start(self, _ctx: OverseerSubsystemContext<MsgB>) -> SpawnedSubsystem<OverseerError> {
SpawnedSubsystem { name: "sub B", future: Box::pin(async move { Ok(()) }) }
}
}
#[derive(Debug, Clone)]
pub struct DummySpawner;
impl SpawnNamed for DummySpawner {
fn spawn_blocking(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
println!("spawn blocking {} {}", task_name, subsystem_name.unwrap_or("default"))
}
fn spawn(
&self,
task_name: &'static str,
subsystem_name: Option<&'static str>,
_future: futures::future::BoxFuture<'static, ()>,
) {
println!("spawn {} {}", task_name, subsystem_name.unwrap_or("default"))
}
}
#[derive(Default)]
struct AwesomeSubSysB;
#[derive(Clone, Debug)]
pub struct SigSigSig;
pub struct Event;
#[derive(Clone, Debug)]
pub struct MsgA(u8);
#[derive(Clone, Debug)]
pub struct MsgB(u8);
#[overlord(signal=SigSigSig, event=Event, gen=AllMessages, error=OverseerError)]
pub struct Overseer {
#[subsystem(MsgA)]
sub_a: AwesomeSubSysA,
#[subsystem(wip, MsgB)]
sub_b: AwesomeSubSysB,
}
pub struct DummyCtx;
fn main() {
let _overseer_builder = Overseer::builder()
.sub_a(AwesomeSubSysA::default())
// b is tagged as `wip`
// .sub_b(AwesomeSubSysB::default())
.spawner(DummySpawner)
.build();
}