char_collection/
operators.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Implementations of standard operators for [CharCollection].
//!
//! `+` and `|` are equivalent. `+` is easier to use with `-`, as they have the same operator
//! precedence.

use crate::{CharCollection, MultiCharRange};
use std::ops;

impl<V: MultiCharRange> ops::BitOr<V> for CharCollection {
    type Output = CharCollection;

    fn bitor(self, rhs: V) -> Self::Output {
        let result: CharCollection = self.into();
        result.union(&rhs)
    }
}

impl<V: MultiCharRange> ops::Add<V> for CharCollection {
    type Output = CharCollection;

    fn add(self, rhs: V) -> Self::Output {
        let result: CharCollection = self.into();
        result.union(&rhs)
    }
}

impl<V: MultiCharRange> ops::BitOrAssign<V> for CharCollection {
    fn bitor_assign(&mut self, rhs: V) {
        self.insert(&rhs);
    }
}

impl<V: MultiCharRange> ops::AddAssign<V> for CharCollection {
    fn add_assign(&mut self, rhs: V) {
        self.insert(&rhs);
    }
}

impl<V: MultiCharRange> ops::Sub<V> for CharCollection {
    type Output = CharCollection;

    fn sub(self, rhs: V) -> Self::Output {
        self.difference(&rhs)
    }
}

impl<V: MultiCharRange> ops::SubAssign<V> for CharCollection {
    fn sub_assign(&mut self, rhs: V) {
        self.remove(&rhs);
    }
}

impl<V: MultiCharRange> ops::BitAnd<V> for CharCollection {
    type Output = CharCollection;

    fn bitand(self, rhs: V) -> Self::Output {
        self.intersection(&rhs)
    }
}

impl<V: MultiCharRange> ops::BitAndAssign<V> for CharCollection {
    fn bitand_assign(&mut self, rhs: V) {
        *self = self.intersection(&rhs);
    }
}

impl ops::Not for CharCollection {
    type Output = CharCollection;

    fn not(self) -> Self::Output {
        self.complement()
    }
}