Refactor for clearer and more performant code #466
Merged
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.
Simplified PR #463 to refactor for clearer and more performant code.
Changelog
Logic Refactoring
DeadZoneShape::cross_deadzone_value()
andDeadZoneShape::ellipse_deadzone_value()
to improve clarity of logic.InputStreams::button_pressed()
andInputStreams::input_value()
functions for bothMouseWheel
andMouseMotion
events by using more efficientf64
accumulation, reducing unnecessarymatch
.Deduplication
ActionState::action_data_mut_or_default()
function to remove duplication in the parts checkingpress()
,release()
, andconsume()
.deadzone_axis_value()
to remove duplication in the parts applying deadzone to input values.virtual_axis_button_clash()
for checking clashes ofVirtualAxis
.send_keyboard_input()
,send_mouse_wheel()
,send_mouse_motion()
, andsend_gamepad_button_changed()
forMutableInputStreams
when mocking input.collect_events_cloned()
to remove duplication in the parts collecting the cloned events ofMouseWheel
andMouseMotion
.InputStreams::extract_dual_axis_data()
to checkInputKind::DualAxis
has beenbutton_pressed()
.get_gamepad_value()
closure to reuse the logic getting values fromInputStreams::gamepad_axes
andInputStreams::gamepad_buttons_axes
.InputStreams::extract_single_axis_data()
for computing theinput_value()
ofVirtualAxis
and theinput_axis_pair()
ofVirtualDpad
.RawInputs::merge_input_data()
to remove duplication in the parts mergingUserInput
s into aRawInputs
.Code Quality
FRAC_PI_2
rather than magic numbers.f32::from()
rather than manualif
when just getting af32
frombool
value.matches!
rather than manualmatch
when just getting abool
field value insideOption
.if let Some()
rather than manualmatch
when theNone
arm does nothing.Result::ok()
rather than manualmatch
when castingResult
intoOption
.Option::then()
rather than manualmatch
when just mapping its value.bool::then_some()
rather than manualif
when just mapping it intoOption
.for
loops when the logic can be replaced with simple and descriptive operators.Itertools::join()
rather than manualfor
loop when just converting values into a string.Option::into_iter().chain(Iter)
rather than manualif
for identical behavior, as they result in the same assembly code.HashMap::entry().or_default()
rather than manualHashMap::raw_entry_mut().or_insert_with()
to ensure its value existence.assert_eq!
rather thanassert!
when asserting two values are equal.Typo
DeadZoneShape
.Documentation
UserInput::len()
forVirtualAxis
.