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
pub trait WordSet: Sized {
    fn subset(self) -> impl Iterator<Item = Self>;
}

macro_rules! impl_uint {
    ( $($ty:ty)* ) => { $(
        impl WordSet for $ty {
            fn subset(self) -> impl Iterator<Item = Self> {
                let n = self;
                std::iter::successors(Some(0), move |&i| {
                    let d = n ^ i;
                    (d > 0).then(|| {
                        let d_neg = d.wrapping_neg();
                        (i & (d | d_neg)) + (d & d_neg)
                    })
                })
            }
        }
    )* };
}

impl_uint! { u8 u16 u32 u64 u128 usize }

#[test]
fn sanity_check() {
    assert!(0_usize.subset().eq([0]));
    eprintln!("{:?}", 0b1101_usize.subset().collect::<Vec<_>>());
    assert!(
        0b1101_usize.subset().eq([
            0b0000, 0b0001, 0b0100, 0b0101, 0b1000, 0b1001, 0b1100, 0b1101
        ])
    );
}