@@ -330,6 +330,37 @@ macro_rules! impl_vector3x_consts {
330330 } ;
331331}
332332
333+ macro_rules! shared_vector_docs {
334+ ( ) => {
335+ "Conversions are provided via various `from_*` and `to_*` functions, not via the `From` trait. This encourages `new()` as the main \
336+ way to construct vectors, is explicit about the conversion taking place, needs no type inference, and works in `const` contexts."
337+ } ;
338+ }
339+
340+ macro_rules! tuple_type {
341+ ( $Scalar: ty; $x: ident, $y: ident) => {
342+ ( $Scalar, $Scalar)
343+ } ;
344+ ( $Scalar: ty; $x: ident, $y: ident, $z: ident) => {
345+ ( $Scalar, $Scalar, $Scalar)
346+ } ;
347+ ( $Scalar: ty; $x: ident, $y: ident, $z: ident, $w: ident) => {
348+ ( $Scalar, $Scalar, $Scalar, $Scalar)
349+ } ;
350+ }
351+
352+ macro_rules! array_type {
353+ ( $Scalar: ty; $x: ident, $y: ident) => {
354+ [ $Scalar; 2 ]
355+ } ;
356+ ( $Scalar: ty; $x: ident, $y: ident, $z: ident) => {
357+ [ $Scalar; 3 ]
358+ } ;
359+ ( $Scalar: ty; $x: ident, $y: ident, $z: ident, $w: ident) => {
360+ [ $Scalar; 4 ]
361+ } ;
362+ }
363+
333364/// Implements functions that are present on floating-point and integer vectors.
334365macro_rules! impl_vector_fns {
335366 (
@@ -345,20 +376,48 @@ macro_rules! impl_vector_fns {
345376 /// # Constructors and general vector functions
346377 /// The following associated functions and methods are available on all vectors (2D, 3D, 4D; float and int).
347378 impl $Vector {
348- /// Returns a vector with the given components.
379+ /// Creates a vector with the given components.
380+ #[ inline]
349381 pub const fn new( $( $comp: $Scalar) ,* ) -> Self {
350382 Self {
351383 $( $comp ) ,*
352384 }
353385 }
354386
355- /// Returns a new vector with all components set to `v`.
387+ /// Creates a vector with all components set to `v`.
388+ #[ inline]
356389 pub const fn splat( v: $Scalar) -> Self {
357390 Self {
358391 $( $comp: v ) ,*
359392 }
360393 }
361394
395+ /// Creates a vector from the given tuple.
396+ #[ inline]
397+ pub const fn from_tuple( tuple: tuple_type!( $Scalar; $( $comp) ,* ) ) -> Self {
398+ let ( $( $comp, ) * ) = tuple;
399+ Self :: new( $( $comp) ,* )
400+ }
401+
402+ /// Creates a vector from the given array.
403+ #[ inline]
404+ pub const fn from_array( array: array_type!( $Scalar; $( $comp) ,* ) ) -> Self {
405+ let [ $( $comp, ) * ] = array;
406+ Self :: new( $( $comp) ,* )
407+ }
408+
409+ /// Returns a tuple with the components of the vector.
410+ #[ inline]
411+ pub const fn to_tuple( & self ) -> tuple_type!( $Scalar; $( $comp) ,* ) {
412+ ( $( self . $comp, ) * )
413+ }
414+
415+ /// Returns an array with the components of the vector.
416+ #[ inline]
417+ pub const fn to_array( & self ) -> array_type!( $Scalar; $( $comp) ,* ) {
418+ [ $( self . $comp, ) * ]
419+ }
420+
362421 /// Converts the corresponding `glam` type to `Self`.
363422 fn from_glam( v: $GlamVector) -> Self {
364423 Self :: new(
0 commit comments