math_ops/
vector.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
65
66
67
//! Defines the `Vector<T>` struct that wraps `Vec<T>` and provides conversion traits.

use std::ops::{Deref, DerefMut};

/// A wrapper around `Vec<T>` to enable trait implementations.
#[derive(Debug, Clone, PartialEq)]
pub struct Vector<T>(pub Vec<T>);

impl<T> Vector<T> {
  /// Creates a new `Vector<T>` from a `Vec<T>`.
  pub fn new(data: Vec<T>) -> Self {
    Vector(data)
  }

  /// Consumes the `Vector<T>` and returns the inner `Vec<T>`.
  pub fn into_vec(self) -> Vec<T> {
    self.0
  }
}

impl<T> From<Vec<T>> for Vector<T> {
  fn from(vec: Vec<T>) -> Self {
    Vector(vec)
  }
}

impl<T> Into<Vec<T>> for Vector<T> {
  fn into(self) -> Vec<T> {
    self.0
  }
}

impl<T> Deref for Vector<T> {
  type Target = Vec<T>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<T> DerefMut for Vector<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

/// Trait to convert `Vec<T>` into `Vector<T>`.
pub trait WrapAsVector<T> {
  fn wrap_as_vector(self) -> Vector<T>;
}

/// Trait to convert `Vector<T>` back into `Vec<T>`.
pub trait UnwrapToVec<T> {
  fn unwrap_to_vec(self) -> Vec<T>;
}

impl<T> WrapAsVector<T> for Vec<T> {
  fn wrap_as_vector(self) -> Vector<T> {
    Vector::new(self)
  }
}

impl<T> UnwrapToVec<T> for Vector<T> {
  fn unwrap_to_vec(self) -> Vec<T> {
    self.into_vec()
  }
}