char_collection/
operators.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Implementations of standard operators for [CharCollection].
6//!
7//! `+` and `|` are equivalent. `+` is easier to use with `-`, as they have the same operator
8//! precedence.
9
10use crate::{CharCollection, MultiCharRange};
11use std::ops;
12
13impl<V: MultiCharRange> ops::BitOr<V> for CharCollection {
14    type Output = CharCollection;
15
16    fn bitor(self, rhs: V) -> Self::Output {
17        let result: CharCollection = self.into();
18        result.union(&rhs)
19    }
20}
21
22impl<V: MultiCharRange> ops::Add<V> for CharCollection {
23    type Output = CharCollection;
24
25    fn add(self, rhs: V) -> Self::Output {
26        let result: CharCollection = self.into();
27        result.union(&rhs)
28    }
29}
30
31impl<V: MultiCharRange> ops::BitOrAssign<V> for CharCollection {
32    fn bitor_assign(&mut self, rhs: V) {
33        self.insert(&rhs);
34    }
35}
36
37impl<V: MultiCharRange> ops::AddAssign<V> for CharCollection {
38    fn add_assign(&mut self, rhs: V) {
39        self.insert(&rhs);
40    }
41}
42
43impl<V: MultiCharRange> ops::Sub<V> for CharCollection {
44    type Output = CharCollection;
45
46    fn sub(self, rhs: V) -> Self::Output {
47        self.difference(&rhs)
48    }
49}
50
51impl<V: MultiCharRange> ops::SubAssign<V> for CharCollection {
52    fn sub_assign(&mut self, rhs: V) {
53        self.remove(&rhs);
54    }
55}
56
57impl<V: MultiCharRange> ops::BitAnd<V> for CharCollection {
58    type Output = CharCollection;
59
60    fn bitand(self, rhs: V) -> Self::Output {
61        self.intersection(&rhs)
62    }
63}
64
65impl<V: MultiCharRange> ops::BitAndAssign<V> for CharCollection {
66    fn bitand_assign(&mut self, rhs: V) {
67        *self = self.intersection(&rhs);
68    }
69}
70
71impl ops::Not for CharCollection {
72    type Output = CharCollection;
73
74    fn not(self) -> Self::Output {
75        self.complement()
76    }
77}