word_set/
lib.rs

1pub trait WordSet: Sized {
2    fn subset(self) -> impl Iterator<Item = Self>;
3}
4
5macro_rules! impl_uint {
6    ( $($ty:ty)* ) => { $(
7        impl WordSet for $ty {
8            fn subset(self) -> impl Iterator<Item = Self> {
9                let n = self;
10                std::iter::successors(Some(0), move |&i| {
11                    let d = n ^ i;
12                    (d > 0).then(|| {
13                        let d_neg = d.wrapping_neg();
14                        (i & (d | d_neg)) + (d & d_neg)
15                    })
16                })
17            }
18        }
19    )* };
20}
21
22impl_uint! { u8 u16 u32 u64 u128 usize }
23
24#[test]
25fn sanity_check() {
26    assert!(0_usize.subset().eq([0]));
27    eprintln!("{:?}", 0b1101_usize.subset().collect::<Vec<_>>());
28    assert!(
29        0b1101_usize.subset().eq([
30            0b0000, 0b0001, 0b0100, 0b0101, 0b1000, 0b1001, 0b1100, 0b1101
31        ])
32    );
33}