math_ops/
vector.rs

1//! Defines the `Vector<T>` struct that wraps `Vec<T>` and provides conversion traits.
2
3use std::ops::{Deref, DerefMut};
4
5/// A wrapper around `Vec<T>` to enable trait implementations.
6#[derive(Debug, Clone, PartialEq)]
7pub struct Vector<T>(pub Vec<T>);
8
9impl<T> Vector<T> {
10  /// Creates a new `Vector<T>` from a `Vec<T>`.
11  pub fn new(data: Vec<T>) -> Self {
12    Vector(data)
13  }
14
15  /// Consumes the `Vector<T>` and returns the inner `Vec<T>`.
16  pub fn into_vec(self) -> Vec<T> {
17    self.0
18  }
19}
20
21impl<T> From<Vec<T>> for Vector<T> {
22  fn from(vec: Vec<T>) -> Self {
23    Vector(vec)
24  }
25}
26
27impl<T> Into<Vec<T>> for Vector<T> {
28  fn into(self) -> Vec<T> {
29    self.0
30  }
31}
32
33impl<T> Deref for Vector<T> {
34  type Target = Vec<T>;
35
36  fn deref(&self) -> &Self::Target {
37    &self.0
38  }
39}
40
41impl<T> DerefMut for Vector<T> {
42  fn deref_mut(&mut self) -> &mut Self::Target {
43    &mut self.0
44  }
45}
46
47/// Trait to convert `Vec<T>` into `Vector<T>`.
48pub trait IntoVector<T> {
49  fn into_vector(self) -> Vector<T>;
50}
51
52impl<T> IntoVector<T> for Vec<T> {
53  fn into_vector(self) -> Vector<T> {
54    Vector::new(self)
55  }
56}
57