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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// devela::num::alg::linear::vector
//
//! Linear algebra vectors.
//!
//! Vectors represent the difference between two positions.
//!
//! They are characterized by their *direction* and *magnitude*, and
//! their direction can be decomposed into *orientation* and *sense*.
//

use crate::Num;
#[cfg(feature = "alloc")]
use crate::{Box, NumError, NumResult as Result, Vec};

mod array;
#[cfg(feature = "alloc")]
mod vec;

/* types */

/// A static `D`-dimensional vector, backed by a primitive [`array`][prim@array].
#[repr(transparent)]
pub struct Vector<T, const D: usize> {
    /// The vector coordinates in some basis.
    pub coords: [T; D],
}

/// A static 2-dimensional vector.
pub type Vector2d<T> = Vector<T, 2>;

/// A static 3-dimensional vector.
pub type Vector3d<T> = Vector<T, 3>;

/// A dynamic vector, backed by a primitive [`Vec`].
#[repr(transparent)]
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
pub struct VecVector<T> {
    /// The vector coordinates in some basis.
    pub coords: Vec<T>,
}

/* trait */

/// A common trait for all vectors.
pub trait NumVector: Num {
    /// The associated scalar type.
    type Scalar;
}

#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
impl<T: Num + 'static, const D: usize>
    TryInto<Box<dyn NumVector<Scalar = T, Rhs = Self, Inner = [T; D], Out = Self>>>
    for Vector<T, D>
{
    type Error = NumError;

    fn try_into(
        self,
    ) -> Result<Box<dyn NumVector<Scalar = T, Rhs = Self, Inner = [T; D], Out = Self>>> {
        Ok(Box::new(self))
    }
}