Skip to content

Make state setting easier through builder pattern #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

NicEastvillage
Copy link
Contributor

@NicEastvillage NicEastvillage commented Jun 22, 2025

I noticed that state setting is a pain in Rust due to all the Options. This API makes it easy to set the fields that you want to change without thinking about all the optionals.

Example:

let mut dgs = DesiredGameState::default();
dgs.mod_car(0, |c| {
    c.set_location(Vector3::default());
    c.set_boost(100.);
});
dgs.mod_balls(0..5, |_, b| {
    b.set_location_z(0.);
    b.set_velocity_z(0.);
});

Copy link
Member

@swz-git swz-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General questions:

  • How are the ergonomics of passing functions instead of DesiredXState directly? Is it worth not being able to pass already built DesiredXState?
  • Maybe all of the builders should take a mutable reference in ::new()? Or maybe none of them should? Is the inconsistency worth it? (see below)

Currently the ergonomics seem to be adapted for use-cases like your example in the docs. But what about if you want to set a Vec of already existing DesiredCarStates? See comment on all_cars.

Comment on lines 58 to 71
/// Modify all desired balls.
pub fn all_balls(
mut self,
range: Range<usize>,
build: impl Fn(usize, DesiredBallBuilder) -> DesiredBallBuilder,
) -> Self {
while self.state.ball_states.len() < range.end {
self.state.ball_states.push(Default::default());
}
for (i, ball) in self.state.ball_states[range].iter_mut().enumerate() {
build(i, DesiredBallBuilder::new(ball));
}
self
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a better solution would look like this? What if you want to state-set only balls 0 and 2 for example. This applies to all_cars too.

Suggested change
/// Modify all desired balls.
pub fn all_balls(
mut self,
range: Range<usize>,
build: impl Fn(usize, DesiredBallBuilder) -> DesiredBallBuilder,
) -> Self {
while self.state.ball_states.len() < range.end {
self.state.ball_states.push(Default::default());
}
for (i, ball) in self.state.ball_states[range].iter_mut().enumerate() {
build(i, DesiredBallBuilder::new(ball));
}
self
}
/// Modify all desired balls.
pub fn all_balls(mut self, build: impl IntoIterator<Item = (usize, DesiredBallState)>) -> Self {
for (i, ball) in build.into_iter() {
if self.state.ball_states.len() <= i {
self.state.ball_states.resize(i + 1, Default::default());
}
self.state.ball_states[i] = ball;
}
self
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only state-set ball 0 and 2, then you should probably use the ball method twice instead. Changing range to impl IntoIterator<Item = usize> is a good idea though.

Regarding passing in DesiredXState instead of functions - If you already have an iterator of DesiredXState, then the normal API is not hard to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stuff changed in 7f7835e

@NicEastvillage NicEastvillage requested a review from swz-git June 25, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants