refactor!: improve simulation builder API #45
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This changes the simulation spawner API to be generic over a
Ctxtrait that combines the existingSetupDatawith a newConfig, to allow for dynamic simulation config down the road. It also adds the spawn method to theNodetrait, to make things easier and less convoluted.(The following API change summary is AI generated but checked by me to be accurate)
Ctxtrait to model simulation context (config + setup) and round labeling.Node<C>trait and removed the oldSpawn<D>trait.SpawnContextandRoundContextgeneric over the newCtxand added access to config.Node::node_label(&SpawnContext<C>)for per-node labels in traces.Ctx, with newBuilder::with_config.Added
trait Ctxwith:type Config: SetupData + Defaulttype Setup: SetupDataasync fn setup(config: &Self::Config) -> Result<Self::Setup>fn round_label(config: &Self::Config, round: u32) -> Option<String>SpawnContext<'_, C>::config()andRoundContext<'_, C>::config()accessors.Node::node_label(&SpawnContext<C>) -> Option<String>to label nodes.Builder::with_config(config: C::Config)alongsideBuilder::new()(usesDefaultconfig).Breaking Changes
Spawn<D>; node spawning is now defined onNode<C>:impl Spawn<D> for MyNode { async fn spawn(&mut SpawnContext<'_, D>) { ... } }impl Node<MyCtx> for MyNode { async fn spawn(&mut SpawnContext<'_, MyCtx>) { ... } }Ctxinstead of a freeSetupDatatype:Builder<D>→Builder<C>whereC: Ctx.SpawnContext<'_, D>/RoundContext<'_, D>→SpawnContext<'_, C>/RoundContext<'_, C>.Ctx::setupinstead.NodeBuilder<N, D>→NodeBuilder<N, C>with boundN: Node<C>.BoxNodeandDynNodeare now generic overC: Ctx.Spawn::round_label(&D, round)toCtx::round_label(&C::Config, round).run_sim_fnnow returns/consumesBuilder<C>(whereC: Ctx).Migration Example
Below is a minimal example showing how to convert an existing simulation.
Before (old API with
Spawn<D>and builder-provided setup):After (new API with
CtxandNode<C>):Notes:
context.setup_data()orctx.setup_data()in spawn/round functions; access config viacontext.config().fn node_label(&self, ctx: &SpawnContext<C>) -> Option<String>in yourNode<C>impl.Change checklist