Skip to content

Commit

Permalink
Add conversion functions to Size and Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Hlusička committed Oct 28, 2020
1 parent d3b04bf commit a1d1770
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/size.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::f32;
use super::vector::Vector;

/// An amount of space in 2 dimensions.
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -57,6 +58,12 @@ impl From<[u16; 2]> for Size {
}
}

impl From<Vector<f32>> for Size {
fn from(vector: Vector<f32>) -> Self {
Size { width: vector.x, height: vector.y }
}
}

impl From<Size> for [f32; 2] {
fn from(size: Size) -> [f32; 2] {
[size.width, size.height]
Expand Down
20 changes: 20 additions & 0 deletions core/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::size::Size;

/// A 2D vector.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vector<T = f32> {
Expand Down Expand Up @@ -65,3 +67,21 @@ where
}
}
}

impl<T> From<[T; 2]> for Vector<T> {
fn from([x, y]: [T; 2]) -> Self {
Self::new(x, y)
}
}

impl<T> From<Vector<T>> for [T; 2] where T: Copy {
fn from(other: Vector<T>) -> Self {
[other.x, other.y]
}
}

impl From<Size> for Vector<f32> {
fn from(size: Size) -> Self {
Vector::new(size.width, size.height)
}
}

0 comments on commit a1d1770

Please sign in to comment.