Skip to content

Commit

Permalink
Convert to let-else
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Nov 18, 2022
1 parent 6347710 commit e8a69ac
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 69 deletions.
5 changes: 1 addition & 4 deletions bxt-strafe/src/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,7 @@ impl<S: Step> Step for LeaveGround<S> {
.0
.simulate(tracer, parameters, frame_bulk, state.clone(), input);

let action = match frame_bulk.auto_actions.leave_ground_action {
Some(x) => x,
None => return do_nothing,
};
let Some(action) = frame_bulk.auto_actions.leave_ground_action else { return do_nothing };

if action.times != Times::UnlimitedWithinFrameBulk {
return do_nothing;
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/bxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ fn open_library() -> Option<libloading::Library> {

#[instrument(name = "bxt::find_pointers", skip_all)]
pub unsafe fn find_pointers(marker: MainThreadMarker) {
let library = match open_library() {
Some(library) => library,
None => {
debug!("could not find Bunnymod XT");
return;
}
let Some(library) = open_library() else {
debug!("could not find Bunnymod XT");
return;
};

for pointer in POINTERS {
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/opengl32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ fn open_library() -> Option<libloading::Library> {
/// stale.
#[instrument(name = "opengl32::find_pointers", skip_all)]
pub unsafe fn find_pointers(marker: MainThreadMarker) {
let library = match open_library() {
Some(library) => library,
None => {
warn!("error loading opengl32.dll");
return;
}
let Some(library) = open_library() else {
warn!("error loading opengl32.dll");
return;
};

for pointer in POINTERS {
Expand Down
9 changes: 3 additions & 6 deletions src/hooks/sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ fn open_library() -> Option<libloading::Library> {
/// [`reset_pointers()`] must be called before SDL is unloaded so the pointers don't go stale.
#[instrument(name = "sdl::find_pointers", skip_all)]
pub unsafe fn find_pointers(marker: MainThreadMarker) {
let library = match open_library() {
Some(library) => library,
None => {
warn!("error loading SDL2");
return;
}
let Some(library) = open_library() else {
warn!("error loading SDL2");
return;
};

for pointer in POINTERS {
Expand Down
15 changes: 3 additions & 12 deletions src/modules/capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,7 @@ pub unsafe fn capture_sound(marker: MainThreadMarker, mode: SoundCaptureMode) {

pub unsafe fn on_s_transfer_stereo_16(marker: MainThreadMarker, end: i32) {
let mut state = STATE.borrow_mut(marker);
let recorder = match *state {
State::Recording(ref mut recorder) => recorder,
_ => return,
};
let State::Recording(ref mut recorder) = *state else { return };

let painted_time = *engine::paintedtime.get(marker);
let paint_buffer = &*engine::paintbuffer.get(marker);
Expand All @@ -466,10 +463,7 @@ pub unsafe fn on_s_transfer_stereo_16(marker: MainThreadMarker, end: i32) {

pub unsafe fn on_host_filter_time(marker: MainThreadMarker) -> bool {
let mut state = STATE.borrow_mut(marker);
let recorder = match *state {
State::Recording(ref mut recorder) => recorder,
_ => return false,
};
let State::Recording(ref mut recorder) = *state else { return false };

if (*engine::cls_demos.get(marker)).demoplayback == 0 {
return false;
Expand Down Expand Up @@ -517,10 +511,7 @@ pub fn prevent_toggle_console(marker: MainThreadMarker) -> bool {

pub unsafe fn time_passed(marker: MainThreadMarker) {
let mut state = STATE.borrow_mut(marker);
let recorder = match *state {
State::Recording(ref mut recorder) => recorder,
_ => return,
};
let State::Recording(ref mut recorder) = *state else { return };

// Accumulate time for the last frame.
let time = *engine::host_frametime.get(marker);
Expand Down
7 changes: 1 addition & 6 deletions src/modules/hud_scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ pub unsafe fn with_scaled_projection_matrix<T>(
marker: MainThreadMarker,
f: impl FnOnce() -> T,
) -> T {
let scale = match scale(marker) {
Some(scale) => scale,
None => {
return f();
}
};
let Some(scale) = scale(marker) else { return f() };

{
let (width, height) = engine::get_resolution(marker);
Expand Down
17 changes: 4 additions & 13 deletions src/modules/tas_optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,9 @@ fn optim_init(marker: MainThreadMarker, path: PathBuf, first_frame: usize) {
// TODO: this function must be marked as unsafe. Getting the player data should be safe in a
// console command callback, however by being safe, this function can be called from anywhere
// else in the code.
let player = match unsafe { player_data(marker) } {
Some(x) => x,
None => {
con_print(
marker,
"Cannot enable the TAS optimizer outside of gameplay.\n",
);
return;
}
let Some(player) = (unsafe { player_data(marker) }) else {
con_print(marker, "Cannot enable the TAS optimizer outside of gameplay.\n");
return;
};

// TODO: get current parameters.
Expand Down Expand Up @@ -742,10 +736,7 @@ fn optim_simulation_done(_marker: MainThreadMarker) {
}

pub unsafe fn maybe_receive_messages_from_remote_server(marker: MainThreadMarker) {
let cls = match engine::cls.get_opt(marker) {
Some(x) => x,
None => return,
};
let Some(cls) = engine::cls.get_opt(marker) else { return };

let client_state = (*cls).state;
if client_state != 1 && client_state != 5 {
Expand Down
20 changes: 4 additions & 16 deletions src/modules/tas_optimizer/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,9 @@ pub fn receive_new_hltas_to_simulate() -> Option<HLTAS> {

pub fn on_frame_simulated(get_frame_data: impl FnOnce() -> Frame) {
let mut state = STATE.lock().unwrap();
let server = match state.remote_server() {
Some(x) => x,
None => return,
};
let Some(server) = state.remote_server() else { return };

let pending_frames = match &mut server.simulation_state {
SimulationState::Recording(frames) => frames,
_ => return,
};
let SimulationState::Recording(pending_frames) = &mut server.simulation_state else { return };

let frame = get_frame_data();

Expand All @@ -544,10 +538,7 @@ pub fn on_frame_simulated(get_frame_data: impl FnOnce() -> Frame) {

pub fn send_simulation_result_to_server() {
let mut state = STATE.lock().unwrap();
let server = match state.remote_server() {
Some(x) => x,
None => return,
};
let Some(server) = state.remote_server() else { return };

// Send empty frames if needed to avoid softlocks in unforeseen situations.
let pending_frames = server.simulation_state.take_frames().unwrap_or_default();
Expand All @@ -560,10 +551,7 @@ pub fn send_simulation_result_to_server() {

pub fn start_recording_frames() {
let mut state = STATE.lock().unwrap();
let server = match state.remote_server() {
Some(x) => x,
None => return,
};
let Some(server) = state.remote_server() else { return };

if server.simulation_state.is_waiting_to_start() {
server.simulation_state = SimulationState::Recording(Vec::new());
Expand Down

0 comments on commit e8a69ac

Please sign in to comment.