devela/num/geom/linear/vector/
mod.rs

1// devela::num::geom::linear::vector
2//
3//! Linear algebra vectors.
4//!
5//! Vectors represent the difference between two positions.
6//!
7//! They are characterized by their *direction* and *magnitude*, and
8//! their direction can be decomposed into *orientation* and *sense*.
9//
10
11use crate::Num;
12#[cfg(feature = "alloc")]
13use crate::{Box, NumError, NumResult as Result, Vec};
14
15mod array;
16#[cfg(feature = "alloc")]
17mod vec;
18
19/* types */
20
21/// A static `D`-dimensional vector, backed by a primitive [`array`][prim@array].
22#[repr(transparent)]
23pub struct Vector<T, const D: usize> {
24    /// The vector coordinates in some basis.
25    pub coords: [T; D],
26}
27
28/// A static 2-dimensional vector.
29pub type Vector2d<T> = Vector<T, 2>;
30
31/// A static 3-dimensional vector.
32pub type Vector3d<T> = Vector<T, 3>;
33
34/// A dynamic vector, backed by a primitive [`Vec`].
35#[repr(transparent)]
36#[cfg(feature = "alloc")]
37#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
38pub struct VecVector<T> {
39    /// The vector coordinates in some basis.
40    pub coords: Vec<T>,
41}
42
43/* trait */
44
45/// A common trait for all vectors.
46// THINK: not const D, to support Vec, but could be enough for arrays performance-wise?
47// I'd need
48pub trait NumVector: Num {
49    // RETHINK
50    /// The associated scalar type.
51    type Scalar;
52
53    // Either Collection, Data, Sequence, Coords...
54    // type Coords; // array, vec, etc
55
56    // TODO
57    // fn dot() -> Self::Scalar;
58    // fn cross() -> Self::Scalar;
59}
60
61#[cfg(feature = "alloc")]
62#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
63impl<T: Num + 'static, const D: usize>
64    TryInto<Box<dyn NumVector<Scalar = T, Rhs = Self, Inner = [T; D], Out = Self>>>
65    for Vector<T, D>
66{
67    type Error = NumError;
68
69    fn try_into(
70        self,
71    ) -> Result<Box<dyn NumVector<Scalar = T, Rhs = Self, Inner = [T; D], Out = Self>>> {
72        Ok(Box::new(self))
73    }
74}