-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 DesiredCarState
s? See comment on all_cars
.
rlbot/src/state_builder.rs
Outdated
/// 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 | ||
} |
There was a problem hiding this comment.
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.
/// 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 | |
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stuff changed in 7f7835e
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: