typebitset 0.5.1

Type-level bitset implementation
Documentation
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#![doc = include_str!("../README.md")]

use core::fmt;
use core::fmt::{Debug, Display};
use core::hash::Hash;
use core::marker::PhantomData;
use core::ops::{BitAnd, BitOr, BitXor};

pub mod list;
pub mod rel;

pub mod prelude {
	pub use crate::list::RecList;
	pub use crate::Value;
}

/// Implementation of bitset. See [`Value`]
#[derive(Copy, Clone, Default, Hash)]
pub struct Cons<B, S>(PhantomData<(B, S)>);

macro_rules! impl_cmp_op {
	($([$($pname:tt$(: $ptyp:tt)*),*] $m:path, $n:path;)*) => {
		$(
			impl<$($pname$(: $ptyp)*),*> PartialEq<$n> for $m
			{
				fn eq(&self, _: &$n) -> bool {
					<$m as Value>::N == <$n as Value>::N
				}
			}
			impl<$($pname$(: $ptyp)*),*> PartialOrd<$n> for $m
			{
				fn partial_cmp(&self, _: &$n) -> Option<core::cmp::Ordering> {
					<$m as Value>::N.partial_cmp(&<$n as Value>::N)
				}
			}
		)*
	}
}

impl_cmp_op! {
	[B: Bit] Bit0, B;
	[B: Bit] Bit1, B;
	[B: Bit, S: Positive] Bit0, Cons<B, S>;
	[B: Bit, S: Positive] Bit1, Cons<B, S>;
	[B1: Bit, B: Bit, S: Positive] Cons<B, S>, B1;
	[B0: Bit, B1: Bit, S0: Positive, S1: Positive] Cons<B0, S0>, Cons<B1, S1>;
}

macro_rules! impl_disp_dbg {
	($($([$($p:tt),*])? $t:path;)*) => {
		$(
			impl$(<$($p),*>)? Eq for $t where Self: PartialEq<Self> {}
			impl$(<$($p),*>)? Display for $t
			where
				Self: Value,
			{
				fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
					<usize as Display>::fmt(&<Self as Value>::N, f)
				}
			}
			impl$(<$($p),*>)? Debug for $t
			where
				Self: Value,
			{
				fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
					<usize as Debug>::fmt(&<Self as Value>::N, f)
				}
			}
		)*
	}
}

impl_disp_dbg! {
	Bit0;
	Bit1;
	[B, S] Cons<B, S>;
}

/// Represents a bitset represented as zero.
/// Only if a bitset equals to [`Bit0`], the bitset means zero.
/// See [`Value`] for details.
#[derive(Copy, Clone, Default, Hash)]
pub struct Bit0;

/// Represents a bitset represented as one.
/// See [`Value`] for details.
#[derive(Copy, Clone, Default, Hash)]
pub struct Bit1;

/// The main trait represents a bitset.
///
/// [`Value`] is an representation of non-negative number. It can be converted
/// to `usize` number via [`Value::as_usize`] or [`Value::N`].
///
/// [`Value`] is one of [`Bit`], [`Cons<B, S>`] where `B: Bit, S: Positive`.
/// Because the constraint of `S: Positive`, the type representation of a number
/// is uniquely decided. Thus the following code is not allowed:
///
/// ```compile_fail
/// # use typebitset::{Value, Cons, Bit0, Bit1};
/// fn check<T: Value>() -> usize { T::N  }
/// check::<Cons<Bit0, Bit0>>();
/// ```
/// ```compile_fail
/// # use typebitset::{Value, Cons, Bit0, Bit1};
/// # fn check<T: Value>() -> usize { T::N  }
/// check::<Cons<Bit0, Cons<Bit0, Bit0>>>();
/// ```
///
/// And the followings are consistent.
///
/// ```
/// # use typebitset::{Value, Cons, Bit0, Bit1};
/// # fn check<T: Value>() -> usize { T::N  }
/// assert_eq!(
/// 	check::<Cons<Bit1, Cons<Bit0, Bit1>>>(),
/// 	0b101
/// );
/// ```
///
/// You can use [`FromNum<N>`] to create [`Value`] type for small number `N`.
/// See [`FromNum`] for details.
///
/// Operators [`BitAnd`] and [`BitOr`] are supported.
///
/// ```
/// # use typebitset::{Value, FromNum, Bit0, Bit1};
/// let _: FromNum<0b101> = FromNum::<0b100>::VALUE | Bit1;
/// let _: FromNum<0b1> = FromNum::<0b111>::VALUE & Bit1;
/// ```
pub trait Value: Copy + Clone + Default + PartialEq + Eq + Display + Debug + Hash {
	/// Integer representation of the bitset.
	const N: usize;

	/// Convert type to value. Synonym of `<T as Default>::default()`.
	const VALUE: Self;

	/// Convert the [`Value`] val to `usize`.
	fn as_usize(&self) -> usize {
		Self::N
	}
}

impl Value for Bit0 {
	const N: usize = 0;
	const VALUE: Bit0 = Bit0;
}

impl Value for Bit1 {
	const N: usize = 1;
	const VALUE: Bit1 = Bit1;
}

impl<B: Bit, S: Positive> Value for Cons<B, S> {
	const N: usize = <S as Value>::N * 2 + <B as Value>::N;
	const VALUE: Self = Cons(PhantomData);
}

macro_rules! value_from {
	([$($par:ident: $type:ident),*] $obj:ident) => {
		impl<$($par : $type),*> From<$obj<$($par),*>> for usize
		{
			fn from(_: $obj<$($par),*>) -> usize {
				<$obj<$($par),*>>::N
			}
		}

		impl<$($par : $type),*> $obj<$($par),*>
		{
			const N: usize = <Self as Value>::N;
			pub fn as_usize(&self) -> usize {
				Self::N
			}
		}
	};
}

value_from!([] Bit0);
value_from!([] Bit1);
value_from!([B: Bit, S: Positive] Cons);

/// A trait implemented if the bitset is positive (not zero).
///
/// ```
/// # use typebitset::{FromNum, Positive};
/// fn test<T: Positive>() {}
/// test::<FromNum<1>>();
/// test::<FromNum<2>>();
/// test::<FromNum<3>>();
/// ```
/// ```compile_fail
/// # use typebitset::{FromNum, Positive};
/// # fn test<T: Positive>() {}
/// // fail
/// test::<FromNum<0>>();
/// ```
pub trait Positive: Value {}
impl Positive for Bit1 {}
impl<B: Bit, S: Positive> Positive for Cons<B, S> {}

/// A trait which represents a bit.
pub trait Bit: Value {}
impl Bit for Bit0 {}
impl Bit for Bit1 {}

/// Generate left shift of the bitset.
///
/// ```
/// # use typebitset::{Value, FromNum, ShiftRaising};
/// let a: FromNum<0b1010> =
/// 	<
/// 		FromNum<0b101> as ShiftRaising
/// 	>::Output::VALUE;
///
/// assert_eq!(a.as_usize(), 0b1010);
/// ```
pub trait ShiftRaising: Value {
	type Output: Value;
	fn shift_raising(self) -> Self::Output {
		Self::Output::VALUE
	}
}

impl ShiftRaising for Bit0 {
	type Output = Bit0;
}

impl ShiftRaising for Bit1 {
	type Output = Cons<Bit0, Bit1>;
}

impl<B, S> ShiftRaising for Cons<B, S>
where
	S: Positive,
	B: Bit,
{
	type Output = Cons<Bit0, Cons<B, S>>;
}

/// Generate right shift of the bitset.
///
/// ```
/// # use typebitset::{Value, FromNum, ShiftLowering};
/// let a: FromNum<0b101> = <
/// 	FromNum<0b1010> as ShiftLowering
/// >::Output::VALUE;
/// assert_eq!(a.as_usize(), 0b101);
/// ```
pub trait ShiftLowering: Value {
	type Output: Value;
	/// The LSB, dropped from `Self`.
	///
	/// ```
	/// # use typebitset::{Value, Bit0,Bit1,FromNum, ShiftLowering};
	/// let _: Bit0 = <
	/// 	FromNum<0b1010> as ShiftLowering
	/// >::Lsb::VALUE;
	/// let _: Bit1 = <
	/// 	FromNum<0b101> as ShiftLowering
	/// >::Lsb::VALUE;
	/// ```
	type Lsb: Bit;
	fn shift_lowering(self) -> Self::Output {
		Self::Output::VALUE
	}
}

impl ShiftLowering for Bit0 {
	type Output = Bit0;
	type Lsb = Bit0;
}

impl ShiftLowering for Bit1 {
	type Output = Bit0;
	type Lsb = Bit1;
}

impl<B, S> ShiftLowering for Cons<B, S>
where
	S: Positive,
	B: Bit,
{
	type Output = S;
	type Lsb = B;
}

/// Make left shift of the bitset and use give bit as the LSB.
///
/// ```
/// # use typebitset::{Value, FromNum, ShiftRaising};
/// let a: FromNum<0b1010> = <
/// 	FromNum<0b101> as ShiftRaising
/// >::Output::VALUE;
/// assert_eq!(a.as_usize(), 0b1010);
/// ```
pub trait Push<B>: Value {
	type Output: Value;
	fn push(self) -> Self::Output {
		Self::Output::VALUE
	}
}

impl<B: Bit> Push<B> for Bit0 {
	type Output = B;
}

impl<B: Bit> Push<B> for Bit1 {
	type Output = Cons<B, Bit1>;
}

impl<B0: Bit, B: Bit, S: Positive> Push<B0> for Cons<B, S> {
	type Output = Cons<B0, Cons<B, S>>;
}

/// Replacing Bit1 in Self with S.
/// S should be [`Positive`].
///
/// ```
/// # use typebitset::{Value, FromNum, ReplaceOnes};
/// let a: FromNum<0b1101100> = <
/// 	FromNum<0b10100> as ReplaceOnes<FromNum<0b11>>
/// >::Output::VALUE;
/// let b: FromNum<0b11100> = <
/// 	FromNum<0b100> as ReplaceOnes<FromNum<0b111>>
/// >::Output::VALUE;
/// assert_eq!(a.as_usize(), 0b1101100);
/// assert_eq!(b.as_usize(), 0b11100);
/// ```
pub trait ReplaceOnes<S>: Value {
	type Output: Value;
	fn replace_ones(self) -> Self::Output {
		Self::Output::VALUE
	}
}

impl<S> ReplaceOnes<S> for Bit0 {
	type Output = Bit0;
}

impl<S: Value> ReplaceOnes<S> for Bit1 {
	type Output = S;
}

impl<S, S0: ReplaceOnes<S> + Positive> ReplaceOnes<S> for Cons<Bit0, S0>
where
	<S0 as ReplaceOnes<S>>::Output: Positive,
{
	type Output = Cons<Bit0, <S0 as ReplaceOnes<S>>::Output>;
}

impl<S: PushAfterMsb<<S0 as ReplaceOnes<S>>::Output>, S0: ReplaceOnes<S> + Positive> ReplaceOnes<S>
	for Cons<Bit1, S0>
{
	type Output = <S as PushAfterMsb<<S0 as ReplaceOnes<S>>::Output>>::Output;
}

/// Concat bitset S after the MSB of Self.
///
/// ```
/// # use typebitset::{Value, FromNum, PushAfterMsb};
/// let a: FromNum<0b1101> = <
/// 	FromNum<0b0> as PushAfterMsb<FromNum<0b1101>>
/// >::Output::VALUE;
/// let b: FromNum<0b1101100> = <
/// 	FromNum<0b100> as PushAfterMsb<FromNum<0b1101>>
/// >::Output::VALUE;
/// assert_eq!(FromNum::<0b1101>::N, 0b1101);
/// assert_eq!(a.as_usize(), 0b1101);
/// assert_eq!(b.as_usize(), 0b1101100);
/// ```
pub trait PushAfterMsb<S>: Value {
	type Output: Value;
	fn push_after_msb(self) -> Self::Output {
		Self::Output::VALUE
	}
}

impl<S: Value> PushAfterMsb<S> for Bit0 {
	type Output = S;
}

impl<S: Positive> PushAfterMsb<S> for Bit1 {
	type Output = Cons<Bit1, S>;
}

impl<B: Bit, S: Positive, S0: PushAfterMsb<S> + Positive> PushAfterMsb<S> for Cons<B, S0>
where
	<S0 as PushAfterMsb<S>>::Output: Positive,
{
	type Output = Cons<B, <S0 as PushAfterMsb<S>>::Output>;
}

macro_rules! impl_binary_op {
	($($bita:ident, $bitb:ident, $bito_and:ident, $bito_or:ident, $bito_xor:ident;)*) => {
		$(
			impl BitAnd<$bita> for $bitb {
				type Output = $bito_and;
				fn bitand(self, _: $bita) -> Self::Output {
					$bito_and
				}
			}

			impl BitOr<$bita> for $bitb {
				type Output = $bito_or;
				fn bitor(self, _: $bita) -> Self::Output {
					$bito_or
				}
			}

			impl BitXor<$bita> for $bitb {
				type Output = $bito_xor;
				fn bitxor(self, _: $bita) -> Self::Output {
					$bito_xor
				}
			}

			impl<Sa> BitAnd<Cons<$bita, Sa>> for $bitb
			where
				Cons<$bita, Sa>: Value
			{
				type Output = $bito_and;
				fn bitand(self, _: Cons<$bita, Sa>) -> Self::Output {
					$bito_and
				}
			}

			impl<Sb> BitAnd<$bita> for Cons<$bitb, Sb>
			where
				Cons<$bitb, Sb>: Value
			{
				type Output = $bito_and;
				fn bitand(self, _: $bita) -> Self::Output {
					$bito_and
				}
			}

			impl<Sa> BitOr<Cons<$bita, Sa>> for $bitb
			where
				Cons<$bita, Sa>: Value,
				Sa: Push<$bito_or>,
			{
				type Output = <Sa as Push<$bito_or>>::Output;
				fn bitor(self, _: Cons<$bita, Sa>) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sb> BitOr<$bita> for Cons<$bitb, Sb>
			where
				Cons<$bitb, Sb>: Value,
				Sb: Push<$bito_or>,
			{
				type Output = <Sb as Push<$bito_or>>::Output;
				fn bitor(self, _: $bita) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sa> BitXor<Cons<$bita, Sa>> for $bitb
			where
				Cons<$bita, Sa>: Value,
				Sa: Push<$bito_xor>,
			{
				type Output = <Sa as Push<$bito_xor>>::Output;
				fn bitxor(self, _: Cons<$bita, Sa>) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sb> BitXor<$bita> for Cons<$bitb, Sb>
			where
				Cons<$bitb, Sb>: Value,
				Sb: Push<$bito_xor>,
			{
				type Output = <Sb as Push<$bito_xor>>::Output;
				fn bitxor(self, _: $bita) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sa, Sb> BitAnd<Cons<$bita, Sa>> for Cons<$bitb, Sb>
			where
				Cons<$bita, Sa>: Value,
				Cons<$bitb, Sb>: Value,
				Sb: BitAnd<Sa>,
				<Sb as BitAnd<Sa>>::Output: Push<$bito_and>,
			{
				type Output = <<Sb as BitAnd<Sa>>::Output as Push<$bito_and>>::Output;
				fn bitand(self, _rhs: Cons<$bita, Sa>) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sa, Sb> BitOr<Cons<$bita, Sa>> for Cons<$bitb, Sb>
			where
				Cons<$bita, Sa>: Value,
				Cons<$bitb, Sb>: Value,
				Sb: BitOr<Sa>,
				<Sb as BitOr<Sa>>::Output: Push<$bito_or>,
			{
				type Output = <<Sb as BitOr<Sa>>::Output as Push<$bito_or>>::Output;
				fn bitor(self, _rhs: Cons<$bita, Sa>) -> Self::Output {
					Self::Output::VALUE
				}
			}

			impl<Sa, Sb> BitXor<Cons<$bita, Sa>> for Cons<$bitb, Sb>
			where
				Cons<$bita, Sa>: Value,
				Cons<$bitb, Sb>: Value,
				Sb: BitXor<Sa>,
				<Sb as BitXor<Sa>>::Output: Push<$bito_xor>,
			{
				type Output = <<Sb as BitXor<Sa>>::Output as Push<$bito_xor>>::Output;
				fn bitxor(self, _rhs: Cons<$bita, Sa>) -> Self::Output {
					Self::Output::VALUE
				}
			}
		)*
	}
}

impl_binary_op! {
	// Lhs, Rhs, And, Or, Xor
	Bit0, Bit0, Bit0, Bit0, Bit0;
	Bit1, Bit0, Bit0, Bit1, Bit1;
	Bit0, Bit1, Bit0, Bit1, Bit1;
	Bit1, Bit1, Bit1, Bit1, Bit0;
}

#[doc(hidden)]
pub trait FromNumImpl<const N: usize> {
	type Output: Value;
}

/// Generate [`Value`] type for small number `N`.
/// [`FromNum<N>`] is defined where `N` is in `0..2 ** 7`.
///
/// For example, `FromNum<2>` is expanded to `Cons<Bit0, Bit1>`.
///
/// To generate an object instead of type, use [`from_num()`].
///
/// ```
/// # use typebitset::{Value, Bit0, Bit1, Cons,FromNum};
/// let _: FromNum<0> = Bit0; // for minimum N
/// let _: FromNum<1> = Bit1;
/// let _: FromNum<2> = Cons::<Bit0, Bit1>::VALUE;
/// let _: FromNum<3> = Cons::<Bit1, Bit1>::VALUE;
/// let _: FromNum<4> = Cons::<Bit0, Cons<Bit0, Bit1>>::VALUE;
/// let _: FromNum<{2_usize.pow(7) - 1}>; // for maximum N
/// ```
/// ```compile_fail
/// # use typebitset::FromNum;
/// let _: FromNum<{2_usize.pow(7)}>; // Compile error
/// ```
///
/// To create larger number, you can use [`ShiftRaising`] or [`Push`].
pub type FromNum<const N: usize> = <Bit0 as FromNumImpl<N>>::Output;

/// Generate [`Value`] object using const generics.
///
/// See [`FromNum`]
pub fn from_num<const N: usize>() -> FromNum<N>
where
	Bit0: FromNumImpl<N>,
{
	Default::default()
}

macro_rules! impl_set_of {
	(@out ) => { $crate::Bit0 };
	(@out 1) => { $crate::Bit1 };
	(@out 0 $(,$xs:tt)+) => {
		$crate::Cons<$crate::Bit0, impl_set_of!(@out $($xs),+)>
	};
	(@out 1 $(,$xs:tt)+) => {
		$crate::Cons<$crate::Bit1, impl_set_of!(@out $($xs),+)>
	};
	(@imp $($xs:tt),*) => {
		impl FromNumImpl<{impl_set_of!(@bittonum $($xs),*)}> for Bit0 {
			type Output = impl_set_of!(@out $($xs),*);
		}
	};
	(@bittonum ) => {0usize};
	(@bittonum $x0:tt $(,$xs:tt)*) => {
		2usize * (impl_set_of!(@bittonum $($xs),*)) + $x0
	};
	(@i [$($ys:tt),*] ) => {
		impl_set_of!(@imp $($ys),*);
	};
	(@i [$($ys:tt),*] 1) => {
		impl_set_of!(@i [$($ys,)* 1] );
	};
	(@i [$($ys:tt),*] 1 $(,$xs:tt)+) => {
		impl_set_of!(@i [$($ys,)* 1] $($xs),+);
		impl_set_of!(@i [$($ys,)* 0] $($xs),+);
	};
	() => {
		impl_set_of!(@imp );
	};
	(1 $(,$xs:tt)*) => {
		impl_set_of!(@i [] 1 $(,$xs)*);
		impl_set_of!($($xs),*);
	}
}

impl_set_of!(1, 1, 1, 1, 1, 1, 1);

#[cfg(test)]
pub(crate) mod test {
	use super::*;

	macro_rules! if_impl_trait {
		($t:ty : $tr:path) => {{
			trait DoesNotImpl {
				const IMPLS: bool = false;
			}
			impl<T> DoesNotImpl for T {}
			struct Wrapper<T>(::core::marker::PhantomData<T>);
			#[allow(dead_code)]
			impl<T> Wrapper<T>
			where
				T: $tr,
			{
				const IMPLS: bool = true;
			}
			<Wrapper<$t>>::IMPLS
		}};
	}
	pub(crate) use if_impl_trait;

	macro_rules! test_with_number {
		(@run $n:expr) => {
			let _: FromNum<{ $n * 2 }> = <<FromNum<{ $n }> as ShiftRaising>::Output>::default();
			let _: FromNum<{ $n * 4 }> =
				<<<FromNum<{ $n }> as ShiftRaising>::Output as ShiftRaising>::Output>::default();
			let _: FromNum<{ $n }> = <<FromNum<{ $n * 2 }> as ShiftLowering>::Output>::default();
			let _: FromNum<{ $n }> =
				<<<FromNum<{ $n * 4 }> as ShiftLowering>::Output as ShiftLowering>::Output>::default();
			let _: [(); <FromNum<{$n}>>::N - $n] = [];
		};
		(@ [$($ys:expr),*]) => {
			test_with_number!(@run 0usize $(+$ys)*);
		};
		(@ [$($ys:expr),*] $x:expr $(,$xs:expr)*) => {
			test_with_number!(@[$($ys),*]$($xs),*);
			test_with_number!(@[$($ys,)*$x]$($xs),*);
		};
		($($xs:expr),*) => {
			test_with_number!(@ [] $($xs),*);
		};
	}

	macro_rules! test_and_or {
		(@run $a: expr, $b: expr) => {
			let _: <FromNum<{$a}> as BitAnd<FromNum<{$b}>>>::Output = from_num::<{$a & $b}>();
			let _: <FromNum<{$a}> as BitOr<FromNum<{$b}>>>::Output = from_num::<{$a | $b}>();
			let _: <FromNum<{$a}> as BitXor<FromNum<{$b}>>>::Output = from_num::<{$a ^ $b}>();
		};
		(@ [$($ys: expr),*] [$($zs: expr),*]) => {
			test_and_or!(@run 0usize $(+ $ys)*, 0usize $(+ $zs)*);
		};
		(@ [$($ys: expr),*] [$($zs: expr),*] $x: expr $(,$xs: expr)*) => {
			test_and_or!(@ [$($ys),*] [$($zs),*] $($xs),*);
			test_and_or!(@ [$x $(,$ys)*] [$($zs),*] $($xs),*);
			test_and_or!(@ [$($ys),*] [$x $(,$zs)*] $($xs),*);
			test_and_or!(@ [$x $(,$ys)*] [$x $(,$zs)*] $($xs),*);
		};
		($($xs:expr),*) => {
			test_and_or!(@ [] [] $($xs),*);
		};
	}

	#[test]
	fn test() {
		let v1: Cons<Bit1, Cons<Bit0, Bit1>> = Default::default();
		let v2: Cons<Bit1, Bit1> = Default::default();
		let _: Bit1 = v1 & v2;
		let _: Cons<Bit1, Cons<Bit1, Bit1>> = v1 | v2;
		let _v4: <<Bit0 as ShiftRaising>::Output as Push<Bit1>>::Output = Default::default();
		let _: (FromNum<6>, FromNum<12>) = <<(FromNum<6>, FromNum<8>) as list::BitOrAll<
			FromNum<4>,
		>>::Output as Default>::default();
		test_with_number!(1, 2, 4, 8, 16);
		test_and_or!(1, 2, 4, 8, 16);

		assert!(if_impl_trait!((FromNum<3>, FromNum<3>): list::SameList));
		assert!(!if_impl_trait!((FromNum<4>, FromNum<3>): list::SameList));
		assert!(if_impl_trait!((FromNum<3>, FromNum<1>): list::PositiveAll));
		assert!(!if_impl_trait!((FromNum<0>, FromNum<1>): list::PositiveAll));
	}
}