Use a trait to make it nicer

This commit is contained in:
Surma
2019-02-15 19:22:46 +01:00
parent a316120b69
commit 1a63387408

View File

@@ -1,20 +1,26 @@
use std::slice::from_raw_parts_mut; use std::slice::from_raw_parts_mut;
// This function is taken from // This function is taken from Zachary Dremann
// https://rustwasm.github.io/book/reference/code-size.html // https://github.com/GoogleChromeLabs/squoosh/pull/462
trait HardUnwrap<T> {
fn unwrap_hard(self) -> T;
}
impl<T> HardUnwrap<T> for Option<T> {
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
#[inline] #[inline]
fn unwrap_abort<T>(o: Option<T>) -> T { fn unwrap_hard(self) -> T {
match o { match self {
Some(t) => t, Some(t) => t,
None => std::process::abort(), None => std::process::abort(),
} }
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
fn unwrap_abort<T>(o: Option<T>) -> T { fn unwrap_hard(self) -> T {
o.unwrap() o.unwrap()
} }
}
#[no_mangle] #[no_mangle]
fn rotate(width: usize, height: usize, _rotate: usize) { fn rotate(width: usize, height: usize, _rotate: usize) {
@@ -32,8 +38,8 @@ fn rotate(width: usize, height: usize, _rotate: usize) {
for x in 0..width { for x in 0..width {
let new_x = (new_width - 1) - y; let new_x = (new_width - 1) - y;
let new_y = x; let new_y = x;
*unwrap_abort(out_b.get_mut(new_y * new_width + new_x)) = *out_b.get_mut(new_y * new_width + new_x).unwrap_hard() =
*unwrap_abort(in_b.get(y * width + x)); *in_b.get(y * width + x).unwrap_hard();
} }
} }
} }