pixman/
vector.rs

1use crate::{ffi, Fixed};
2
3/// A single vector
4#[derive(Debug, Clone, Copy)]
5#[repr(transparent)]
6pub struct Vector(ffi::pixman_vector_t);
7
8impl Vector {
9    /// Initialize a vector from the provided values
10    #[inline]
11    pub fn new<T: Into<Fixed> + Copy>(vector: [T; 3]) -> Self {
12        Self(ffi::pixman_vector {
13            vector: [
14                vector[0].into().into_raw(),
15                vector[1].into().into_raw(),
16                vector[2].into().into_raw(),
17            ],
18        })
19    }
20
21    /// Access the x component of this vector
22    pub fn x(&self) -> Fixed {
23        Fixed::from_raw(self.0.vector[0])
24    }
25
26    /// Access the y component of this vector
27    pub fn y(&self) -> Fixed {
28        Fixed::from_raw(self.0.vector[1])
29    }
30
31    /// Access the z component of this vector
32    pub fn z(&self) -> Fixed {
33        Fixed::from_raw(self.0.vector[2])
34    }
35
36    #[inline]
37    pub(crate) fn as_mut_ptr(&mut self) -> *mut ffi::pixman_vector_t {
38        &mut self.0 as *mut ffi::pixman_vector_t
39    }
40}
41
42impl<T: Into<Fixed> + Copy> From<[T; 3]> for Vector {
43    #[inline]
44    fn from(value: [T; 3]) -> Self {
45        Self::new(value)
46    }
47}
48
49impl From<ffi::pixman_vector_t> for Vector {
50    #[inline]
51    fn from(value: ffi::pixman_vector_t) -> Self {
52        Self(value)
53    }
54}
55
56impl From<Vector> for ffi::pixman_vector_t {
57    #[inline]
58    fn from(value: Vector) -> Self {
59        value.0
60    }
61}
62
63/// Floating-point vector
64#[derive(Debug, Clone, Copy)]
65#[repr(transparent)]
66pub struct FVector(ffi::pixman_f_vector_t);
67
68impl FVector {
69    /// Initialize the vector from the specified values
70    #[inline]
71    pub fn new(v: [f64; 3]) -> Self {
72        Self(ffi::pixman_f_vector_t {
73            v: [v[0], v[1], v[2]],
74        })
75    }
76
77    /// Access the x component of this vector
78    pub fn x(&self) -> f64 {
79        self.0.v[0]
80    }
81
82    /// Access the y component of this vector
83    pub fn y(&self) -> f64 {
84        self.0.v[1]
85    }
86
87    /// Access the z component of this vector
88    pub fn z(&self) -> f64 {
89        self.0.v[2]
90    }
91
92    #[inline]
93    pub(crate) fn as_mut_ptr(&mut self) -> *mut ffi::pixman_f_vector_t {
94        &mut self.0 as *mut ffi::pixman_f_vector_t
95    }
96}
97
98impl From<[f64; 3]> for FVector {
99    #[inline]
100    fn from(value: [f64; 3]) -> Self {
101        Self::new(value)
102    }
103}
104
105impl From<ffi::pixman_f_vector_t> for FVector {
106    #[inline]
107    fn from(value: ffi::pixman_f_vector_t) -> Self {
108        Self(value)
109    }
110}
111
112impl From<FVector> for ffi::pixman_f_vector_t {
113    #[inline]
114    fn from(value: FVector) -> Self {
115        value.0
116    }
117}