Skip to content

Commit 8a7ff64

Browse files
committed
Make the Bits/Mut implementations blanket on BitStore
1 parent fc608ba commit 8a7ff64

3 files changed

Lines changed: 91 additions & 115 deletions

File tree

examples/sieve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std::{
4747
fn main() {
4848
let max: usize = env::args()
4949
.nth(1)
50-
.unwrap_or("1000000".into())
50+
.unwrap_or_else(|| "1000000".into())
5151
.parse()
5252
.unwrap_or(1_000_000);
5353

@@ -58,7 +58,7 @@ fn main() {
5858
bv.set(0, false);
5959
bv.set(1, false);
6060

61-
for n in 2 .. (1 + (max as f64).sqrt() as usize) {
61+
for n in 2 ..= ((max as f64).sqrt() as usize) {
6262
// Adjust the frequency of log statements vaguely logarithmically.
6363
if n < 20_000 && n % 1_000 == 0
6464
|| n < 50_000 && n % 5_000 == 0
@@ -104,7 +104,7 @@ fn main() {
104104

105105
let dim: usize = env::args()
106106
.nth(2)
107-
.unwrap_or("10".into())
107+
.unwrap_or_else(|| "10".into())
108108
.parse()
109109
.unwrap_or(10);
110110

src/bits.rs

Lines changed: 87 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -123,140 +123,116 @@ pub trait BitsMut: Bits {
123123
}
124124
}
125125

126-
macro_rules! impl_bits_for {
127-
( $( $t:ty ),* ) => { $(
128-
impl<C> AsMut<BitSlice<C, $t>> for $t
129-
where C: Cursor {
130-
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
131-
BitsMut::bits_mut(self)
132-
}
133-
}
134-
135-
impl<C> AsRef<BitSlice<C, $t>> for $t
136-
where C: Cursor {
137-
fn as_ref(&self) -> &BitSlice<C, $t> {
138-
Bits::bits(self)
139-
}
140-
}
141-
142-
impl Bits for $t {
143-
type Store = $t;
144-
145-
fn bits<C>(&self) -> &BitSlice<C, Self::Store>
126+
impl<T> Bits for T
127+
where T: BitStore {
128+
type Store = T;
129+
fn bits<C>(&self) -> &BitSlice<C, T>
146130
where C: Cursor {
147131
BitSlice::from_element(self)
148132
}
149133
}
150134

151-
impl BitsMut for $t {
152-
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, Self::Store>
135+
impl<T> BitsMut for T
136+
where T: BitStore {
137+
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, T>
153138
where C: Cursor {
154139
BitSlice::from_element_mut(self)
155140
}
156141
}
157142

158-
impl<C> AsMut<BitSlice<C, $t>> for [$t]
159-
where C: Cursor {
160-
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
161-
BitsMut::bits_mut(self)
162-
}
163-
}
164-
165-
impl<C> AsRef<BitSlice<C, $t>> for [$t]
166-
where C: Cursor {
167-
fn as_ref(&self) -> &BitSlice<C, $t> {
168-
Bits::bits(self)
169-
}
170-
}
171-
172-
impl Bits for [$t] {
173-
type Store = $t;
174-
175-
fn bits<C>(&self) -> &BitSlice<C, Self::Store>
143+
impl<T> Bits for [T]
144+
where T: BitStore {
145+
type Store = T;
146+
fn bits<C>(&self) -> &BitSlice<C, T>
176147
where C: Cursor {
177148
BitSlice::from_slice(self)
178149
}
179150
}
180151

181-
impl BitsMut for [$t] {
182-
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, Self::Store>
152+
impl<T> BitsMut for [T]
153+
where T: BitStore {
154+
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, T>
183155
where C: Cursor {
184156
BitSlice::from_slice_mut(self)
185157
}
186158
}
187159

188-
impl<C> AsMut<BitSlice<C, $t>> for [$t; 0]
189-
where C: Cursor {
190-
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
191-
BitsMut::bits_mut(self)
192-
}
193-
}
194-
195-
impl<C> AsRef<BitSlice<C, $t>> for [$t; 0]
196-
where C: Cursor {
197-
fn as_ref(&self) -> &BitSlice<C, $t> {
198-
Bits::bits(self)
199-
}
200-
}
201-
202-
impl Bits for [$t; 0] {
203-
type Store = $t;
204-
205-
fn bits<C>(&self) -> &BitSlice<C, Self::Store>
206-
where C: Cursor {
207-
BitSlice::empty()
208-
}
209-
}
210-
211-
impl BitsMut for [$t; 0] {
212-
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, Self::Store>
213-
where C: Cursor {
214-
BitSlice::empty_mut()
215-
}
216-
}
217-
218-
impl_bits_for! { array $t ;
219-
1 2 3 4 5 6 7 8 9
220-
10 11 12 13 14 15 16 17 18 19
221-
20 21 22 23 24 25 26 27 28 29
222-
30 31 32 // going above 32 is a DoS attack on the compiler
223-
}
160+
macro_rules! impl_bits_for {
161+
( n $( $n:expr ),* ) => { $(
162+
impl<T> Bits for [T; $n]
163+
where T: BitStore {
164+
type Store = T;
165+
fn bits<C>(&self) -> &BitSlice<C, T>
166+
where C: Cursor {
167+
BitSlice::from_slice(self)
168+
}
169+
}
170+
impl<T> BitsMut for [T; $n]
171+
where T: BitStore {
172+
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, T>
173+
where C: Cursor {
174+
BitSlice::from_slice_mut(self)
175+
}
176+
}
224177
)* };
225-
226-
( array $t:ty ; $( $n:expr )* ) => { $(
227-
impl<C> AsMut<BitSlice<C, $t>> for [$t; $n]
228-
where C: Cursor {
229-
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
230-
BitsMut::bits_mut(self)
231-
}
232-
}
233-
234-
impl<C> AsRef<BitSlice<C, $t>> for [$t; $n]
235-
where C: Cursor {
236-
fn as_ref(&self) -> &BitSlice<C, $t> {
237-
Bits::bits(self)
238-
}
239-
}
240-
241-
impl Bits for [$t; $n] {
242-
type Store = $t;
243-
244-
fn bits<C>(&self) -> &BitSlice<C, Self::Store>
245-
where C: Cursor {
246-
BitSlice::from_slice(&self[..])
247-
}
248-
}
249-
250-
impl BitsMut for [$t; $n] {
251-
fn bits_mut<C>(&mut self) -> &mut BitSlice<C, Self::Store>
252-
where C: Cursor {
253-
BitSlice::from_slice_mut(&mut self[..])
254-
}
255-
}
178+
( t $( $t:ty ),*) => { $(
179+
impl<C> AsMut<BitSlice<C, $t>> for $t
180+
where C: Cursor {
181+
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
182+
BitSlice::from_element_mut(self)
183+
}
184+
}
185+
impl<C> AsRef<BitSlice<C, $t>> for $t
186+
where C: Cursor {
187+
fn as_ref(&self) -> &BitSlice<C, $t> {
188+
BitSlice::from_element(self)
189+
}
190+
}
191+
impl<C> AsMut<BitSlice<C, $t>> for [$t]
192+
where C: Cursor {
193+
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
194+
BitSlice::from_slice_mut(self)
195+
}
196+
}
197+
impl<C> AsRef<BitSlice<C, $t>> for [$t]
198+
where C: Cursor {
199+
fn as_ref(&self) -> &BitSlice<C, $t> {
200+
BitSlice::from_slice(self)
201+
}
202+
}
203+
impl_bits_for!(ts $t ; n
204+
0, 1, 2, 3, 4, 5, 6, 7,
205+
8, 9, 10, 11, 12, 13, 14, 15,
206+
16, 17, 18, 19, 20, 21, 22, 23,
207+
24, 25, 26, 27, 28, 29, 30, 31,
208+
32
209+
);
210+
)* };
211+
( ts $t:ty ; n $( $n:expr ),* ) => { $(
212+
impl<C> AsMut<BitSlice<C, $t>> for [$t; $n]
213+
where C: Cursor {
214+
fn as_mut(&mut self) -> &mut BitSlice<C, $t> {
215+
BitSlice::from_slice_mut(self)
216+
}
217+
}
218+
impl<C> AsRef<BitSlice<C, $t>> for [$t; $n]
219+
where C: Cursor {
220+
fn as_ref(&self) -> &BitSlice<C, $t> {
221+
BitSlice::from_slice(self)
222+
}
223+
}
256224
)* };
257225
}
258226

259-
impl_bits_for! { u8, u16, u32 }
227+
impl_bits_for![n
228+
0, 1, 2, 3, 4, 5, 6, 7,
229+
8, 9, 10, 11, 12, 13, 14, 15,
230+
16, 17, 18, 19, 20, 21, 22, 23,
231+
24, 25, 26, 27, 28, 29, 30, 31,
232+
32
233+
];
234+
235+
impl_bits_for![t u8, u16, u32];
260236

261237
#[cfg(target_pointer_width = "64")]
262-
impl_bits_for! { u64 }
238+
impl_bits_for![t u64];

src/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub trait BitStore:
7373
+ Copy
7474
+ Debug
7575
+ Display
76-
// Permit testing a value against 1 in `get()`.
76+
// Permit testing a value against 0 in `get()`.
7777
+ Eq
7878
// Rust treats numeric literals in code as vaguely typed and does not make
7979
// them concrete until long after trait expansion, so this enables building

0 commit comments

Comments
 (0)