[emath] Pos2: add conversions to/from (f32, f32) and [f32; 2]

Useful for places where we have `impl Into<Pos2>` as an argument
This commit is contained in:
Emil Ernerfeldt 2021-01-25 21:11:19 +01:00
parent 63e4ff4382
commit 38ca36724a
1 changed files with 44 additions and 0 deletions

View File

@ -22,6 +22,9 @@ pub const fn pos2(x: f32, y: f32) -> Pos2 {
Pos2 { x, y }
}
// ----------------------------------------------------------------------------
// Compatibility and convenience conversions to and from [f32; 2]:
impl From<[f32; 2]> for Pos2 {
fn from(v: [f32; 2]) -> Self {
Self { x: v[0], y: v[1] }
@ -34,6 +37,47 @@ impl From<&[f32; 2]> for Pos2 {
}
}
impl From<Pos2> for [f32; 2] {
fn from(v: Pos2) -> Self {
[v.x, v.y]
}
}
impl From<&Pos2> for [f32; 2] {
fn from(v: &Pos2) -> Self {
[v.x, v.y]
}
}
// ----------------------------------------------------------------------------
// Compatibility and convenience conversions to and from (f32, f32):
impl From<(f32, f32)> for Pos2 {
fn from(v: (f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<&(f32, f32)> for Pos2 {
fn from(v: &(f32, f32)) -> Self {
Self { x: v.0, y: v.1 }
}
}
impl From<Pos2> for (f32, f32) {
fn from(v: Pos2) -> Self {
(v.x, v.y)
}
}
impl From<&Pos2> for (f32, f32) {
fn from(v: &Pos2) -> Self {
(v.x, v.y)
}
}
// ----------------------------------------------------------------------------
impl Pos2 {
/// The zero position, the origin.
/// The top left corner in a GUI.