strflags/
operators.rs

1use crate::Flags;
2use smallvec::SmallVec;
3use std::ops::*;
4
5impl<T: PartialEq, const S: char> Add<T> for Flags<T, S> {
6    type Output = Self;
7    fn add(mut self, rhs: T) -> Self{
8        if !self.0.iter().any(|x| x == &rhs){
9            self.0.push(rhs)
10        }
11        self
12    }
13}
14
15impl<T: PartialEq, const S: char> Sub<T> for Flags<T, S> {
16    type Output = Self;
17    fn sub(mut self, rhs: T) -> Self{
18        if let Some(index) = self.0.iter().position(|x| x == &rhs){
19            self.0.remove(index);
20        }
21        self
22    }
23}
24
25impl<T: PartialEq, const S: char> BitOr<T> for Flags<T, S> {
26    type Output = Self;
27    fn bitor(mut self, rhs: T) -> Self{
28        if !self.0.iter().any(|x| x == &rhs){
29            self.0.push(rhs)
30        }
31        self
32    }
33}
34
35impl<T: PartialEq, const S: char> BitAnd<T> for Flags<T, S> {
36    type Output = Self;
37    fn bitand(self, rhs: T) -> Self{
38        if self.0.iter().any(|x| x == &rhs){
39            Self::new(rhs)
40        } else {
41            Self::EMPTY
42        }
43    }
44}
45
46impl<T: PartialEq, const S: char> BitXor<T> for Flags<T, S> {
47    type Output = Self;
48    fn bitxor(mut self, rhs: T) -> Self{
49        if let Some(index) = self.0.iter().position(|x| x == &rhs){
50            self.0.remove(index);
51        } else {
52            self.0.push(rhs)
53        }
54        self
55    }
56}
57
58impl<T: PartialEq, const S: char> Add<Self> for Flags<T, S> {
59    type Output = Self;
60    fn add(mut self, rhs: Self) -> Self{
61        for i in rhs.into_iter(){
62            if !self.0.iter().any(|x| x == &i){
63                self.0.push(i)
64            }
65        }
66        self
67    }
68}
69
70impl<T: PartialEq, const S: char> Sub<Self> for Flags<T, S> {
71    type Output = Self;
72    fn sub(mut self, rhs: Self) -> Self{
73        for i in rhs.into_iter(){
74            if let Some(index) = self.0.iter().position(|x| x == &i){
75                self.0.remove(index);
76            }
77        }
78        self
79    }
80}
81
82impl<T: PartialEq, const S: char> BitOr<Self> for Flags<T, S> {
83    type Output = Self;
84    fn bitor(mut self, rhs: Self) -> Self{
85        for i in rhs.into_iter(){
86            if !self.0.iter().any(|x| x == &i){
87                self.0.push(i)
88            }
89        }
90        self
91    }
92}
93
94impl<T: PartialEq, const S: char> BitAnd<Self> for Flags<T, S> {
95    type Output = Self;
96    fn bitand(self, rhs: Self) -> Self{
97        Self(self.0.into_iter()
98                .filter(|l| rhs.iter().any(|r| r == l))
99                .collect())
100    }
101}
102
103impl<T: PartialEq, const S: char> BitXor<Self> for Flags<T, S> {
104    type Output = Self;
105    fn bitxor(mut self, rhs: Self) -> Self{
106        for i in rhs.into_iter(){
107            if let Some(pos) = self.0.iter().position(|x| x == &i) {
108                self.0.remove(pos);
109            } else {
110                self.0.push(i);
111            }
112        }
113        self
114    }
115}
116
117impl<T: PartialEq, const S: char> AddAssign<T> for Flags<T, S> {
118    fn add_assign(&mut self, rhs: T) {
119        if !self.0.iter().any(|x| x == &rhs){
120            self.0.push(rhs)
121        }
122    }
123}
124
125impl<T: PartialEq, const S: char> SubAssign<T> for Flags<T, S> {
126    fn sub_assign(&mut self, rhs: T) {
127        if let Some(index) = self.0.iter().position(|x| x == &rhs){
128            self.0.remove(index);
129        }
130    }
131}
132
133impl<T: PartialEq, const S: char> BitOrAssign<T> for Flags<T, S> {
134    fn bitor_assign(&mut self, rhs: T) {
135        if !self.0.iter().any(|x| x == &rhs){
136            self.0.push(rhs)
137        }
138    }
139}
140
141impl<T: PartialEq, const S: char> BitAndAssign<T> for Flags<T, S> {
142    fn bitand_assign(&mut self, rhs: T) {
143        if self.0.iter().any(|x| x == &rhs){
144            *self = Self(SmallVec::new_const());
145            self.0.push(rhs)
146        } else {
147            *self = Self(SmallVec::new_const());
148        }
149    }
150}
151
152impl<T: PartialEq, const S: char> BitXorAssign<T> for Flags<T, S> {
153    fn bitxor_assign(&mut self, rhs: T) {
154        if let Some(index) = self.0.iter().position(|x| x == &rhs){
155            self.0.remove(index);
156        } else {
157            self.0.push(rhs)
158        }
159    }
160}
161
162impl<T: PartialEq, const S: char> AddAssign<Self> for Flags<T, S> {
163    fn add_assign(&mut self, rhs: Self) {
164        for i in rhs.into_iter(){
165            if !self.0.iter().any(|x| x == &i){
166                self.0.push(i)
167            }
168        }
169    }
170}
171
172impl<T: PartialEq, const S: char> SubAssign<Self> for Flags<T, S> {
173    fn sub_assign(&mut self, rhs: Self) {
174        for i in rhs.into_iter(){
175            if let Some(index) = self.0.iter().position(|x| x == &i){
176                self.0.remove(index);
177            }
178        }
179    }
180}
181
182impl<T: PartialEq, const S: char> BitOrAssign<Self> for Flags<T, S> {
183    fn bitor_assign(&mut self, rhs: Self) {
184        for i in rhs.into_iter(){
185            if !self.0.iter().any(|x| x == &i){
186                self.0.push(i)
187            }
188        }
189    }
190}
191
192impl<T: PartialEq, const S: char> BitAndAssign<Self> for Flags<T, S> {
193    fn bitand_assign(&mut self, rhs: Self) {
194        *self = Self(
195            std::mem::take(&mut self.0)
196                .into_iter()
197                .filter(|l| rhs.iter().any(|r| r == l))
198                .collect())
199    }
200}
201
202impl<T: PartialEq, const S: char> BitXorAssign<Self> for Flags<T, S> {
203    fn bitxor_assign(&mut self, rhs: Self) {
204        for i in rhs.into_iter(){
205            if let Some(pos) = self.0.iter().position(|x| x == &i) {
206                self.0.remove(pos);
207            } else {
208                self.0.push(i);
209            }
210        }
211    }
212}