rspace_core/point/impl_point.rs
1/*
2 Appellation: impl_point <module>
3 Created At: 2025.12.29:14:06:01
4 Contrib: @FL03
5*/
6use super::Point;
7
8impl<X, Y> Point<X, Y> {
9 /// Creates a new `Point`.
10 pub const fn new(x: X, y: Y) -> Self {
11 Point { x, y }
12 }
13 /// create a new instance from the given 2-tuple
14 pub fn from_tuple((x, y): (X, Y)) -> Self {
15 Point { x, y }
16 }
17 /// returns a reference to the x coordinate.
18 pub const fn x(&self) -> &X {
19 &self.x
20 }
21 /// returns a mutable reference to the x coordinate.
22 pub const fn x_mut(&mut self) -> &mut X {
23 &mut self.x
24 }
25 /// returns a reference to the y coordinate.
26 pub const fn y(&self) -> &Y {
27 &self.y
28 }
29 /// returns a mutable reference to the y coordinate.
30 pub const fn y_mut(&mut self) -> &mut Y {
31 &mut self.y
32 }
33 /// [`replace`](core::mem::replace) the x value, returning the old value.
34 pub const fn replace_x(&mut self, x: X) -> X {
35 core::mem::replace(self.x_mut(), x)
36 }
37 /// [`replace`](core::mem::replace) the y value, returning the old value.
38 pub const fn replace_y(&mut self, y: Y) -> Y {
39 core::mem::replace(self.y_mut(), y)
40 }
41 #[inline]
42 /// set the x value
43 pub fn set_x(&mut self, x: X) {
44 self.x = x;
45 }
46 #[inline]
47 /// set the y value
48 pub fn set_y(&mut self, y: Y) {
49 self.y = y;
50 }
51 #[inline]
52 /// consumes the current instance to create another with the given `x` value.
53 pub fn with_x<X2>(self, x: X2) -> Point<X2, Y> {
54 Point { x, y: self.y }
55 }
56 #[inline]
57 /// consumes the current instance to create another with the given `y` value.
58 pub fn with_y<Y2>(self, y: Y2) -> Point<X, Y2> {
59 Point { x: self.x, y }
60 }
61 /// returns an owned view of the point
62 pub const fn as_view(&self) -> Point<&X, &Y> {
63 Point {
64 x: self.x(),
65 y: self.y(),
66 }
67 }
68 /// returns a view of the [`Point`] containing mutable references to the inner values.
69 pub const fn as_mut_view(&mut self) -> Point<&mut X, &mut Y> {
70 Point {
71 x: &mut self.x,
72 y: &mut self.y,
73 }
74 }
75 /// returns a 2-tuple containing references to the inner values.
76 pub const fn as_tuple(&self) -> (&X, &Y) {
77 (self.x(), self.y())
78 }
79 #[inline]
80 /// consumes the caller to convert the object into a 2-tuple.
81 pub fn into_tuple(self) -> (X, Y) {
82 (self.x, self.y)
83 }
84}