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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use core::ops::{BitAnd, Not, BitAndAssign, BitXor, BitXorAssign, BitOr, BitOrAssign};
use super::*;
#[allow(unused_macros)]
macro_rules! add_bitwise_ops {
($wrap_ty:ty) => {
impl BitAnd for $wrap_ty {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self{_v: self._v & rhs._v}
}
}
impl BitAndAssign for $wrap_ty {
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs
}
}
impl BitXor for $wrap_ty {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self{_v: self._v ^ rhs._v}
}
}
impl BitXorAssign for $wrap_ty {
fn bitxor_assign(&mut self, rhs: Self) {
*self = *self ^ rhs
}
}
impl BitOr for $wrap_ty {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self{_v: self._v | rhs._v}
}
}
impl BitOrAssign for $wrap_ty {
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl Not for $wrap_ty {
type Output = Self;
fn not(self) -> Self::Output {
Self{_v: !self._v}
}
}
}
}
#[cfg(feature = "byte_impls")]
mod bitwise_byte_ops {
use super::*;
#[cfg(feature = "big_endian")]
mod be {
use super::*;
add_bitwise_ops!(BigEndian<bool>);
add_bitwise_ops!(BigEndian<u8>);
add_bitwise_ops!(BigEndian<i8>);
}
#[cfg(feature = "little_endian")]
mod le {
use super::*;
add_bitwise_ops!(LittleEndian<bool>);
add_bitwise_ops!(LittleEndian<u8>);
add_bitwise_ops!(LittleEndian<i8>);
}
}
#[cfg(feature = "integer_impls")]
mod bitwise_integer_ops {
use super::*;
#[cfg(feature = "big_endian")]
mod be {
use super::*;
add_bitwise_ops!(BigEndian<u16>);
add_bitwise_ops!(BigEndian<i16>);
add_bitwise_ops!(BigEndian<u32>);
add_bitwise_ops!(BigEndian<i32>);
add_bitwise_ops!(BigEndian<u64>);
add_bitwise_ops!(BigEndian<i64>);
add_bitwise_ops!(BigEndian<u128>);
add_bitwise_ops!(BigEndian<i128>);
add_bitwise_ops!(BigEndian<usize>);
add_bitwise_ops!(BigEndian<isize>);
}
#[cfg(feature = "little_endian")]
mod le {
use super::*;
add_bitwise_ops!(LittleEndian<u16>);
add_bitwise_ops!(LittleEndian<i16>);
add_bitwise_ops!(LittleEndian<u32>);
add_bitwise_ops!(LittleEndian<i32>);
add_bitwise_ops!(LittleEndian<u64>);
add_bitwise_ops!(LittleEndian<i64>);
add_bitwise_ops!(LittleEndian<u128>);
add_bitwise_ops!(LittleEndian<i128>);
add_bitwise_ops!(LittleEndian<usize>);
add_bitwise_ops!(LittleEndian<isize>);
}
}
#[cfg(test)]
mod tests {
extern crate test;
use crate::*;
#[test]
fn bit_and_test() {
let be1 = LittleEndian::<u64>::from(0x0f0);
let be2 = LittleEndian::<u64>::from(0xff0);
assert_eq!(0x0f0, u64::from(be1 & be2));
}
#[test]
fn unary_not_test() {
let be1 = BigEndian::<u16>::from(0x0f0);
assert_eq!(0xff0f, u16::from(!be1));
}
}