implement try from

This commit is contained in:
Corwin 2022-10-08 19:54:30 +01:00
parent d83f0ea710
commit 5e8a50159e

View file

@ -1,5 +1,5 @@
use core::{ use core::{
convert::TryInto, convert::{TryFrom, TryInto},
ops::{Mul, MulAssign}, ops::{Mul, MulAssign},
}; };
@ -69,13 +69,12 @@ impl AffineMatrix {
(self.x, self.y).into() (self.x, self.y).into()
} }
#[must_use] pub fn try_to_background(&self) -> Result<AffineMatrixBackground, OverflowError> {
pub fn try_to_background(&self) -> Option<AffineMatrixBackground> { Ok(AffineMatrixBackground {
Some(AffineMatrixBackground { a: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
a: self.a.to_raw().try_into().ok()?, b: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
b: self.a.to_raw().try_into().ok()?, c: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
c: self.a.to_raw().try_into().ok()?, d: self.a.to_raw().try_into().map_err(|_| OverflowError(()))?,
d: self.a.to_raw().try_into().ok()?,
x: self.a.to_raw(), x: self.a.to_raw(),
y: self.a.to_raw(), y: self.a.to_raw(),
}) })
@ -107,6 +106,14 @@ pub struct AffineMatrixBackground {
y: i32, y: i32,
} }
impl TryFrom<AffineMatrix> for AffineMatrixBackground {
type Error = OverflowError;
fn try_from(value: AffineMatrix) -> Result<Self, Self::Error> {
value.try_to_background()
}
}
impl AffineMatrixBackground { impl AffineMatrixBackground {
#[must_use] #[must_use]
pub fn to_affine_matrix(&self) -> AffineMatrix { pub fn to_affine_matrix(&self) -> AffineMatrix {