minlin/traits/
two_component.rs1use std::{ops, range::Range};
2
3use crate::Vec2;
4
5pub trait TwoComponent {
7 type Val;
8
9 fn from_components(c1: Self::Val, c2: Self::Val) -> Self;
11
12 fn to_components(self) -> (Self::Val, Self::Val);
14
15 fn comp1(&self) -> &Self::Val;
17
18 fn comp2(&self) -> &Self::Val;
20}
21
22impl<T> TwoComponent for Vec2<T> {
23 type Val = T;
24
25 fn from_components(c1: Self::Val, c2: Self::Val) -> Self {
26 Vec2::new(c1, c2)
27 }
28
29 fn to_components(self) -> (Self::Val, Self::Val) {
30 self.into()
31 }
32
33 fn comp1(&self) -> &Self::Val {
34 &self.x
35 }
36
37 fn comp2(&self) -> &Self::Val {
38 &self.y
39 }
40}
41
42impl<T> TwoComponent for (T, T) {
43 type Val = T;
44
45 fn from_components(c1: Self::Val, c2: Self::Val) -> Self {
46 (c1, c2)
47 }
48
49 fn to_components(self) -> (Self::Val, Self::Val) {
50 self
51 }
52
53 fn comp1(&self) -> &Self::Val {
54 &self.0
55 }
56
57 fn comp2(&self) -> &Self::Val {
58 &self.1
59 }
60}
61
62impl<T> TwoComponent for ops::Range<T> {
63 type Val = T;
64
65 fn from_components(c1: Self::Val, c2: Self::Val) -> Self {
66 c1..c2
67 }
68
69 fn to_components(self) -> (Self::Val, Self::Val) {
70 (self.start, self.end)
71 }
72
73 fn comp1(&self) -> &Self::Val {
74 &self.start
75 }
76
77 fn comp2(&self) -> &Self::Val {
78 &self.end
79 }
80}
81
82impl<T> TwoComponent for Range<T> {
83 type Val = T;
84
85 fn from_components(c1: Self::Val, c2: Self::Val) -> Self {
86 Self { start: c1, end: c2 }
87 }
88
89 fn to_components(self) -> (Self::Val, Self::Val) {
90 (self.start, self.end)
91 }
92
93 fn comp1(&self) -> &Self::Val {
94 &self.start
95 }
96
97 fn comp2(&self) -> &Self::Val {
98 &self.end
99 }
100}