Skip to content

Commit c496ec3

Browse files
author
bors-servo
authored
Auto merge of #200 - nical:from-into, r=kvark
Implement some missing convenience traits. Implement `From<[T; N]>` and `Into<[T; N]>` for points and vectors, Implement Hash for rects, and add some helper methods to convert transforms from and to arrays. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/euclid/200) <!-- Reviewable:end -->
2 parents 95d41dc + 0e9bf89 commit c496ec3

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

src/point.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,19 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<Self> for TypedPoint2D<T, U> {
327327
}
328328
}
329329

330+
impl<T: Copy, U> Into<[T; 2]> for TypedPoint2D<T, U> {
331+
fn into(self) -> [T; 2] {
332+
self.to_array()
333+
}
334+
}
335+
336+
impl<T: Copy, U> From<[T; 2]> for TypedPoint2D<T, U> {
337+
fn from(array: [T; 2]) -> Self {
338+
point2(array[0], array[1])
339+
}
340+
}
341+
342+
330343
define_matrix! {
331344
/// A 3d Point tagged with a unit.
332345
pub struct TypedPoint3D<T, U> {
@@ -598,6 +611,18 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<Self> for TypedPoint3D<T, U> {
598611
}
599612
}
600613

614+
impl<T: Copy, U> Into<[T; 3]> for TypedPoint3D<T, U> {
615+
fn into(self) -> [T; 3] {
616+
self.to_array()
617+
}
618+
}
619+
620+
impl<T: Copy, U> From<[T; 3]> for TypedPoint3D<T, U> {
621+
fn from(array: [T; 3]) -> Self {
622+
point3(array[0], array[1], array[2])
623+
}
624+
}
625+
601626

602627
pub fn point2<T: Copy, U>(x: T, y: T) -> TypedPoint2D<T, U> {
603628
TypedPoint2D::new(x, y)

src/rect.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use num_traits::NumCast;
2020
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2121
use std::cmp::PartialOrd;
2222
use std::fmt;
23+
use std::hash::{Hash, Hasher};
2324
use std::ops::{Add, Sub, Mul, Div};
2425

2526
/// A 2d Rectangle optionally tagged with a unit.
@@ -54,6 +55,14 @@ impl<T: Serialize, U> Serialize for TypedRect<T, U> {
5455
}
5556
}
5657

58+
impl<T: Hash, U> Hash for TypedRect<T, U>
59+
{
60+
fn hash<H: Hasher>(&self, h: &mut H) {
61+
self.origin.hash(h);
62+
self.size.hash(h);
63+
}
64+
}
65+
5766
impl<T: Copy, U> Copy for TypedRect<T, U> {}
5867

5968
impl<T: Copy, U> Clone for TypedRect<T, U> {

src/transform2d.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ impl<T: Copy, Src, Dst> TypedTransform2D<T, Src, Dst> {
7979
]
8080
}
8181

82+
/// Returns an array containing this transform's 3 rows in (in row-major order)
83+
/// as arrays.
84+
///
85+
/// This is a convenience method to interface with other libraries like glium.
86+
pub fn to_row_arrays(&self) -> [[T; 2]; 3] {
87+
[
88+
[self.m11, self.m12],
89+
[self.m21, self.m22],
90+
[self.m31, self.m32],
91+
]
92+
}
93+
94+
/// Creates a transform from an array of 6 elements in row-major order.
95+
pub fn from_row_major_array(array: [T; 6]) -> Self {
96+
Self::row_major(
97+
array[0], array[1],
98+
array[2], array[3],
99+
array[4], array[5],
100+
)
101+
}
102+
103+
/// Creates a transform from 3 rows of 2 elements (row-major order).
104+
pub fn from_row_arrays(array: [[T; 2]; 3]) -> Self {
105+
Self::row_major(
106+
array[0][0], array[0][1],
107+
array[1][0], array[1][1],
108+
array[2][0], array[2][1],
109+
)
110+
}
111+
82112
/// Drop the units, preserving only the numeric value.
83113
pub fn to_untyped(&self) -> Transform2D<T> {
84114
Transform2D::row_major(

src/transform3d.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<T: Copy, Src, Dst> TypedTransform3D<T, Src, Dst> {
597597
/// as arrays.
598598
///
599599
/// This is a convenience method to interface with other libraries like glium.
600-
pub fn to_row_arrays(&self) -> [[T; 4];4] {
600+
pub fn to_row_arrays(&self) -> [[T; 4]; 4] {
601601
[
602602
[self.m11, self.m12, self.m13, self.m14],
603603
[self.m21, self.m22, self.m23, self.m24],
@@ -618,6 +618,26 @@ impl<T: Copy, Src, Dst> TypedTransform3D<T, Src, Dst> {
618618
[self.m14, self.m24, self.m34, self.m44]
619619
]
620620
}
621+
622+
/// Creates a transform from an array of 16 elements in row-major order.
623+
pub fn from_array(array: [T; 16]) -> Self {
624+
Self::row_major(
625+
array[0], array[1], array[2], array[3],
626+
array[4], array[5], array[6], array[7],
627+
array[8], array[9], array[10], array[11],
628+
array[12], array[13], array[14], array[15],
629+
)
630+
}
631+
632+
/// Creates a transform from 4 rows of 4 elements (row-major order).
633+
pub fn from_row_arrays(array: [[T; 4]; 4]) -> Self {
634+
Self::row_major(
635+
array[0][0], array[0][1], array[0][2], array[0][3],
636+
array[1][0], array[1][1], array[1][2], array[1][3],
637+
array[2][0], array[2][1], array[2][2], array[2][3],
638+
array[3][0], array[3][1], array[3][2], array[3][3],
639+
)
640+
}
621641
}
622642

623643
impl<T, Src, Dst> fmt::Debug for TypedTransform3D<T, Src, Dst>

src/vector.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedVector2D<T, U>> for TypedVector2D<T,
348348
}
349349
}
350350

351+
impl<T: Copy, U> Into<[T; 2]> for TypedVector2D<T, U> {
352+
fn into(self) -> [T; 2] {
353+
self.to_array()
354+
}
355+
}
356+
357+
impl<T: Copy, U> From<[T; 2]> for TypedVector2D<T, U> {
358+
fn from(array: [T; 2]) -> Self {
359+
vec2(array[0], array[1])
360+
}
361+
}
362+
351363
define_matrix! {
352364
/// A 3d Vector tagged with a unit.
353365
pub struct TypedVector3D<T, U> {
@@ -675,6 +687,19 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedVector3D<T, U>> for TypedVector3D<T,
675687
}
676688
}
677689

690+
impl<T: Copy, U> Into<[T; 3]> for TypedVector3D<T, U> {
691+
fn into(self) -> [T; 3] {
692+
self.to_array()
693+
}
694+
}
695+
696+
impl<T: Copy, U> From<[T; 3]> for TypedVector3D<T, U> {
697+
fn from(array: [T; 3]) -> Self {
698+
vec3(array[0], array[1], array[2])
699+
}
700+
}
701+
702+
678703
/// Convenience constructor.
679704
#[inline]
680705
pub fn vec2<T: Copy, U>(x: T, y: T) -> TypedVector2D<T, U> {

0 commit comments

Comments
 (0)