cgmath/
conv.rs

1//! Constrained conversion functions for assisting in situations where type
2//! inference is difficult.
3//!
4//! For example, when declaring `glium` uniforms, we need to convert to fixed
5//! length arrays. We can use the `Into` trait directly, but it is rather ugly!
6//!
7//! --- Doc-test disabled because glium causes problems with nightly-2019-01-01 needed for "simd"
8//! ` ` `rust
9//! #[macro_use]
10//! extern crate glium;
11//! extern crate cgmath;
12//!
13//! use cgmath::{Matrix4, Point2};
14//! use cgmath::prelude::*;
15//!
16//! # fn main() {
17//! let point = Point2::new(1, 2);
18//! let matrix = Matrix4::from_scale(2.0);
19//!
20//! let uniforms = uniform! {
21//!     point: Into::<[_; 2]>::into(point),
22//!     matrix: Into::<[[_; 4]; 4]>::into(matrix),
23//!     // Yuck!! (ノಥ益ಥ)ノ ┻━┻
24//! };
25//! # }
26//! ` ` `
27//!
28//! Instead, we can use the conversion functions from the `conv` module:
29//!
30//! --- Doc-test disabled because glium causes problems nightly-2019-01-01 needed for "simd"
31//! ` ` `rust
32//! #[macro_use]
33//! extern crate glium;
34//! extern crate cgmath;
35//!
36//! use cgmath::{Matrix4, Point2};
37//! use cgmath::prelude::*;
38//! use cgmath::conv::*;
39//!
40//! # fn main() {
41//! let point = Point2::new(1, 2);
42//! let matrix = Matrix4::from_scale(2.0);
43//!
44//! let uniforms = uniform! {
45//!     point: array2(point),
46//!     matrix: array4x4(matrix),
47//!     // ┬─┬ノ( º _ ºノ)
48//! };
49//! # }
50//! ` ` `
51
52/// Force a conversion into a 2-element array.
53#[inline]
54pub fn array2<T, A: Into<[T; 2]>>(value: A) -> [T; 2] {
55    value.into()
56}
57
58/// Force a conversion into a 3-element array.
59#[inline]
60pub fn array3<T, A: Into<[T; 3]>>(value: A) -> [T; 3] {
61    value.into()
62}
63
64/// Force a conversion into a 4-element array.
65#[inline]
66pub fn array4<T, A: Into<[T; 4]>>(value: A) -> [T; 4] {
67    value.into()
68}
69
70/// Force a conversion into a 2x2-element array.
71#[inline]
72pub fn array2x2<T, A: Into<[[T; 2]; 2]>>(value: A) -> [[T; 2]; 2] {
73    value.into()
74}
75
76/// Force a conversion into a 3x3-element array.
77#[inline]
78pub fn array3x3<T, A: Into<[[T; 3]; 3]>>(value: A) -> [[T; 3]; 3] {
79    value.into()
80}
81
82/// Force a conversion into a 4x4-element array.
83#[inline]
84pub fn array4x4<T, A: Into<[[T; 4]; 4]>>(value: A) -> [[T; 4]; 4] {
85    value.into()
86}