Skip to content
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

12660 progress and small optimization #13244

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 8 additions & 4 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ impl VariableCurve {
// An Ok(keyframe_index) result means an exact result was found by binary search
// An Err result means the keyframe was not found, and the index is the keyframe
// PERF: finding the current keyframe can be optimised
let search_result = self
.keyframe_timestamps
.binary_search_by(|probe| probe.partial_cmp(&seek_time).unwrap());
let search_result = self.keyframe_timestamps.binary_search_by(|probe| {
probe
.partial_cmp(&seek_time)
.expect("provided floats can't be compared")
Copy link
Member

Choose a reason for hiding this comment

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

what are those floats? why are they compared? which is an input provided by the user when calling this function?

});

// Subtract one for zero indexing!
let last_keyframe = self.keyframe_timestamps.len() - 1;
Expand Down Expand Up @@ -1187,7 +1189,9 @@ impl AnimationTargetId {
for name in names {
blake3.update(name.as_bytes());
}
let hash = blake3.finalize().as_bytes()[0..16].try_into().unwrap();
let hash = blake3.finalize().as_bytes()[0..16]
.try_into()
.expect("failed to convert name into hash");
Self(*uuid::Builder::from_sha1_bytes(hash).as_uuid())
}

Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_app/src/plugin_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ impl PluginGroupBuilder {
for plugin_id in order {
self.upsert_plugin_entry_state(
plugin_id,
plugins.remove(&plugin_id).unwrap(),
plugins
.remove(&plugin_id)
.expect("plugin with provided id not found in group"),
self.order.len(),
);

Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_app/src/sub_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ impl SubApp {
schedules.insert(Schedule::new(label));
}

let schedule = schedules.get_mut(label).unwrap();
let schedule = schedules
.get_mut(label)
.expect("could not get mutable reference to the schedule");
f(schedule);

self
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ impl<A: Asset> Assets<A> {
pub fn insert(&mut self, id: impl Into<AssetId<A>>, asset: A) {
match id.into() {
AssetId::Index { index, .. } => {
self.insert_with_index(index, asset).unwrap();
self.insert_with_index(index, asset)
.expect("invalid generation error when inserting asset by id");
}
AssetId::Uuid { uuid } => {
self.insert_with_uuid(uuid, asset);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_winit/src/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct WinitActionHandler(pub Arc<Mutex<VecDeque<ActionRequest>>>);

impl ActionHandler for WinitActionHandler {
fn do_action(&mut self, request: ActionRequest) {
let mut requests = self.0.lock().unwrap();
let mut requests = self.0.lock().expect("ActionHandler lock is poisoned");
requests.push_back(request);
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ fn poll_receivers(
mut actions: EventWriter<ActionRequestWrapper>,
) {
for (_id, handler) in handlers.iter() {
let mut handler = handler.lock().unwrap();
let mut handler = handler.lock().expect("ActionHandlers lock is poisoned");
moonlightaria marked this conversation as resolved.
Show resolved Hide resolved
while let Some(event) = handler.pop_front() {
actions.send(ActionRequestWrapper(event));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub fn winit_runner(mut app: App) -> AppExit {
let event_loop = app
.world_mut()
.remove_non_send_resource::<EventLoop<UserEvent>>()
.unwrap();
.expect("value found that was not previously present");
moonlightaria marked this conversation as resolved.
Show resolved Hide resolved

app.world_mut()
.insert_non_send_resource(event_loop.create_proxy());
Expand Down
78 changes: 37 additions & 41 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ impl WinitWindows {
winit_window_builder = winit_window_builder.with_append(true);
}

let winit_window = winit_window_builder.build(event_loop).unwrap();
let winit_window = winit_window_builder
.build(event_loop)
.expect("unable to create winit window");
let name = window.title.clone();
prepare_accessibility_for_window(
&winit_window,
Expand Down Expand Up @@ -281,52 +283,46 @@ pub fn get_fitting_videomode(
width: u32,
height: u32,
) -> winit::monitor::VideoMode {
let mut modes = monitor.video_modes().collect::<Vec<_>>();

fn abs_diff(a: u32, b: u32) -> u32 {
if a > b {
return a - b;
}
b - a
}

modes.sort_by(|a, b| {
use std::cmp::Ordering::*;
match abs_diff(a.size().width, width).cmp(&abs_diff(b.size().width, width)) {
Equal => {
match abs_diff(a.size().height, height).cmp(&abs_diff(b.size().height, height)) {
Equal => b
.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz()),
default => default,
}
}
default => default,
}
});

modes.first().unwrap().clone()
monitor
.video_modes()
.min_by(|a, b| {
a.size()
.width
.abs_diff(width)
.cmp(&b.size().width.abs_diff(width))
.then_with(|| {
a.size()
.height
.abs_diff(height)
.cmp(&b.size().height.abs_diff(height))
})
.then_with(|| {
b.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz())
})
})
.expect("no video modes found for monitor")
.clone()
}

/// Gets the "best" videomode from a monitor.
///
/// The heuristic for "best" prioritizes width, height, and refresh rate in that order.
pub fn get_best_videomode(monitor: &MonitorHandle) -> winit::monitor::VideoMode {
let mut modes = monitor.video_modes().collect::<Vec<_>>();
modes.sort_by(|a, b| {
use std::cmp::Ordering::*;
match b.size().width.cmp(&a.size().width) {
Equal => match b.size().height.cmp(&a.size().height) {
Equal => b
.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz()),
default => default,
},
default => default,
}
});

modes.first().unwrap().clone()
monitor
.video_modes()
.min_by(|a, b| {
b.size()
.width
.cmp(&a.size().width)
.then_with(|| b.size().height.cmp(&a.size().height))
.then_with(|| {
b.refresh_rate_millihertz()
.cmp(&a.refresh_rate_millihertz())
})
})
.expect("no video modes found for monitor")
.clone()
}

pub(crate) fn attempt_grab(winit_window: &winit::window::Window, grab_mode: CursorGrabMode) {
Expand Down