ubits/
lib.rs

1//!
2//! [![crates.io](https://img.shields.io/crates/v/ubits.svg)](https://crates.io/crates/ubits)
3//! [![Downloads](https://img.shields.io/crates/d/ubits.svg)](https://crates.io/crates/ubits)
4//! [![Build](https://github.com/manoadamro/ubits/actions/workflows/rust.yml/badge.svg)](https://github.com/manoadamro/ubits/actions/workflows/rust.yml)
5//! [![Docs](https://img.shields.io/docsrs/ubits)](https://manoadamro.github.io/ubits/doc/ubits/)
6//! [![Licence](https://img.shields.io/github/license/manoadamro/ubits)](https://github.com/manoadamro/ubits/blob/main/LICENSE)
7//!
8//! Bit fields and masks for rust!
9//!
10//! Provides a macro for generating [`bit field`](https://en.wikipedia.org/wiki/Bit_field) types
11//! complete with flags and some helpful trait implementations.
12//!
13//! Supports field widths of `8`, `16`, `32`, `64` and `128` bits.
14//!
15//! - [`bitfield`] - macro documentation.
16//! - [`examples`] - examples of the output generated by the [`bitfield`] macro.
17//!
18//! __Note: the example module is only complied with documentation builds
19//! and is not available for importing in the wild.__
20//!
21//!
22//! # Usage
23//!
24//! Generate a bitfield struct with a flag enum...
25//! (The following examples all use this as a definition.)
26//! ```rust
27//! use ubits::bitfield;
28//!
29//!     bitfield! {
30//!         pub u8 ExampleField
31//!         ExampleFlags {
32//!             0 : Flag0,
33//!             1 : Flag1,
34//!             2 : Flag2,
35//!             3 : Flag3,
36//!             4 : Flag4,
37//!             5 : Flag5,
38//!             6 : Flag6,
39//!             7 : Flag7,
40//!         }
41//!     }
42//! ```
43//!
44//! ## Instances
45//!
46//! From integer:
47//! ```rust
48//! # use ubits::bitfield;
49//! #    bitfield! {
50//! #        pub u8 ExampleField
51//! #        ExampleFlags {
52//! #            0 : Flag0,
53//! #            1 : Flag1,
54//! #            2 : Flag2,
55//! #            3 : Flag3,
56//! #            4 : Flag4,
57//! #            5 : Flag5,
58//! #            6 : Flag6,
59//! #            7 : Flag7,
60//! #        }
61//! #    }
62//!
63//! let from_integer = ExampleField(123);
64//! assert_eq!(ExampleField(123), from_integer);
65//! ```
66//!
67//! From a binary string:
68//! ```rust
69//! # use ubits::bitfield;
70//! #    bitfield! {
71//! #        pub u8 ExampleField
72//! #        ExampleFlags {
73//! #            0 : Flag0,
74//! #            1 : Flag1,
75//! #            2 : Flag2,
76//! #            3 : Flag3,
77//! #            4 : Flag4,
78//! #            5 : Flag5,
79//! #            6 : Flag6,
80//! #            7 : Flag7,
81//! #        }
82//! #    }
83//!
84//! let from_binary = ExampleField::from_binary_str("01111011");
85//! assert_eq!(ExampleField(123), from_binary)
86//! ```
87//!
88//! From ones:
89//! ```rust
90//! # use ubits::bitfield;
91//! #    bitfield! {
92//! #        pub u8 ExampleField
93//! #        ExampleFlags {
94//! #            0 : Flag0,
95//! #            1 : Flag1,
96//! #            2 : Flag2,
97//! #            3 : Flag3,
98//! #            4 : Flag4,
99//! #            5 : Flag5,
100//! #            6 : Flag6,
101//! #            7 : Flag7,
102//! #        }
103//! #    }
104//!
105//! let from_ones = ExampleField::ones();
106//! assert_eq!("11111111", from_ones.as_binary());
107//! assert_eq!(255, from_ones.as_integer());
108//! ```
109//!
110//! From zeros:
111//! ```rust
112//! # use ubits::bitfield;
113//! #    bitfield! {
114//! #        pub u8 ExampleField
115//! #        ExampleFlags {
116//! #            0 : Flag0,
117//! #            1 : Flag1,
118//! #            2 : Flag2,
119//! #            3 : Flag3,
120//! #            4 : Flag4,
121//! #            5 : Flag5,
122//! #            6 : Flag6,
123//! #            7 : Flag7,
124//! #        }
125//! #    }
126//!
127//! let from_zeros = ExampleField::zeros();
128//! assert_eq!("00000000", from_zeros.as_binary());
129//! assert_eq!(0, from_zeros.as_integer());
130//! ```
131//!
132//! ### Field Access
133//!
134//! Get bit value by field:
135//! ```rust
136//! # use ubits::bitfield;
137//! #    bitfield! {
138//! #        pub u8 ExampleField
139//! #        ExampleFlags {
140//! #            0 : Flag0,
141//! #            1 : Flag1,
142//! #            2 : Flag2,
143//! #            3 : Flag3,
144//! #            4 : Flag4,
145//! #            5 : Flag5,
146//! #            6 : Flag6,
147//! #            7 : Flag7,
148//! #        }
149//! #    }
150//!
151//! let field = ExampleField::from_binary_str("01010101");
152//! assert!(field.get(ExampleFlags::Flag0));
153//! assert!(!field.get(ExampleFlags::Flag1));
154//! ```
155//!
156//! Get bit value by index:
157//!
158//! ```rust
159//! # use ubits::bitfield;
160//! #    bitfield! {
161//! #        pub u8 ExampleField
162//! #        ExampleFlags {
163//! #            0 : Flag0,
164//! #            1 : Flag1,
165//! #            2 : Flag2,
166//! #            3 : Flag3,
167//! #            4 : Flag4,
168//! #            5 : Flag5,
169//! #            6 : Flag6,
170//! #            7 : Flag7,
171//! #        }
172//! #    }
173//!
174//! let field = ExampleField::from_binary_str("01010101");
175//! assert!(field.get_index(0));
176//! assert!(!field.get_index(1));
177//! ```
178//!
179//! Set bit value by field:
180//!
181//! ```rust
182//! # use ubits::bitfield;
183//! #    bitfield! {
184//! #        pub u8 ExampleField
185//! #        ExampleFlags {
186//! #            0 : Flag0,
187//! #            1 : Flag1,
188//! #            2 : Flag2,
189//! #            3 : Flag3,
190//! #            4 : Flag4,
191//! #            5 : Flag5,
192//! #            6 : Flag6,
193//! #            7 : Flag7,
194//! #        }
195//! #    }
196//!
197//! let mut field = ExampleField::from_binary_str("01010101");
198//! field.set(ExampleFlags::Flag1);
199//! field.set(ExampleFlags::Flag3);
200//! assert_eq!("01011111", field.as_binary());
201//! ```
202//!
203//! Set bit value by index:
204//!
205//! ```rust
206//! # use ubits::bitfield;
207//! #    bitfield! {
208//! #        pub u8 ExampleField
209//! #        ExampleFlags {
210//! #            0 : Flag0,
211//! #            1 : Flag1,
212//! #            2 : Flag2,
213//! #            3 : Flag3,
214//! #            4 : Flag4,
215//! #            5 : Flag5,
216//! #            6 : Flag6,
217//! #            7 : Flag7,
218//! #        }
219//! #    }
220//!
221//! let mut field = ExampleField::from_binary_str("01010101");
222//! field.set_index(1);
223//! field.set_index(3);
224//! assert_eq!("01011111", field.as_binary());
225//! ```
226//!
227//! Clear bit value by field:
228//!
229//! ```rust
230//! # use ubits::bitfield;
231//! #    bitfield! {
232//! #        pub u8 ExampleField
233//! #        ExampleFlags {
234//! #            0 : Flag0,
235//! #            1 : Flag1,
236//! #            2 : Flag2,
237//! #            3 : Flag3,
238//! #            4 : Flag4,
239//! #            5 : Flag5,
240//! #            6 : Flag6,
241//! #            7 : Flag7,
242//! #        }
243//! #    }
244//!
245//! let mut field = ExampleField::from_binary_str("01010101");
246//! field.clear(ExampleFlags::Flag0);
247//! field.clear(ExampleFlags::Flag2);
248//! assert_eq!("01010000", field.as_binary());
249//! ```
250//!
251//! Clear bit value by index:
252//!
253//! ```rust
254//! # use ubits::bitfield;
255//! #    bitfield! {
256//! #        pub u8 ExampleField
257//! #        ExampleFlags {
258//! #            0 : Flag0,
259//! #            1 : Flag1,
260//! #            2 : Flag2,
261//! #            3 : Flag3,
262//! #            4 : Flag4,
263//! #            5 : Flag5,
264//! #            6 : Flag6,
265//! #            7 : Flag7,
266//! #        }
267//! #    }
268//!
269//! let mut field = ExampleField::from_binary_str("01010101");
270//! field.clear_index(0);
271//! field.clear_index(2);
272//! assert_eq!("01010000", field.as_binary());
273//! ```
274//!
275//! Toggle bit value by field:
276//!
277//! ```rust
278//! # use ubits::bitfield;
279//! #    bitfield! {
280//! #        pub u8 ExampleField
281//! #        ExampleFlags {
282//! #            0 : Flag0,
283//! #            1 : Flag1,
284//! #            2 : Flag2,
285//! #            3 : Flag3,
286//! #            4 : Flag4,
287//! #            5 : Flag5,
288//! #            6 : Flag6,
289//! #            7 : Flag7,
290//! #        }
291//! #    }
292//!
293//! let mut field = ExampleField::from_binary_str("01010101");
294//! field.toggle(ExampleFlags::Flag0);
295//! assert_eq!("01010100", field.as_binary());
296//! field.toggle(ExampleFlags::Flag0);
297//! assert_eq!("01010101", field.as_binary());
298//! ```
299//!
300//! Toggle bit value by index:
301//!
302//! ```rust
303//! # use ubits::bitfield;
304//! #    bitfield! {
305//! #        pub u8 ExampleField
306//! #        ExampleFlags {
307//! #            0 : Flag0,
308//! #            1 : Flag1,
309//! #            2 : Flag2,
310//! #            3 : Flag3,
311//! #            4 : Flag4,
312//! #            5 : Flag5,
313//! #            6 : Flag6,
314//! #            7 : Flag7,
315//! #        }
316//! #    }
317//!
318//! let mut field = ExampleField::from_binary_str("01010101");
319//! field.toggle_index(0);
320//! assert_eq!("01010100", field.as_binary());
321//! field.toggle_index(0);
322//! assert_eq!("01010101", field.as_binary());
323//! ```
324//!
325//! ### Named Getters and Setters
326//!
327//! Ubits can generate getter and setter methods for zero or more fields, if a name is provided.
328//!
329//! ```rust
330//! use ubits::bitfield;
331//!    bitfield! {
332//!        pub u8 ExampleField
333//!        ExampleFlags {
334//!            0 : Flag0 : (field_0),  // is_field_0 & set_field_0 & clear_field_0 & toggle_field_0
335//!            1 : Flag1 : (field_1),  // is_field_1 & set_field_1 & clear_field_1 & toggle_field_1
336//!            2 : Flag2,
337//!            3 : Flag3,
338//!            4 : Flag4,
339//!            5 : Flag5,
340//!            6 : Flag6,
341//!            7 : Flag7,
342//!        }
343//!    }
344//!
345//! let mut field = ExampleField::from_binary_str("01010101");
346//! assert_eq!(true, field.is_field_0());
347//! assert_eq!(false, field.is_field_1());
348//!
349//! field.set_field_1();
350//! assert_eq!(true, field.is_field_1());
351//!
352//! field.clear_field_1();
353//! assert_eq!(false, field.is_field_1());
354//!
355//! field.toggle_field_1();
356//! assert_eq!(true, field.is_field_1());
357//! ```
358//!
359//! ### Combinations
360//!
361//! Combine bit fields: <br>
362//! (use `into_combined` to consume self)
363//!
364//! ```rust
365//! # use ubits::bitfield;
366//! #    bitfield! {
367//! #        pub u8 ExampleField
368//! #        ExampleFlags {
369//! #            0 : Flag0,
370//! #            1 : Flag1,
371//! #            2 : Flag2,
372//! #            3 : Flag3,
373//! #            4 : Flag4,
374//! #            5 : Flag5,
375//! #            6 : Flag6,
376//! #            7 : Flag7,
377//! #        }
378//! #    }
379//!
380//! let mut a = ExampleField::from_binary_str("01010101");
381//! let b = ExampleField::from_binary_str("10101010");
382//! assert_eq!("11111111", a.combine(b).as_binary());
383//! ```
384//!
385//! Get the intersection of two bitfields: <br>
386//! (use `into_intersection` to consume self)
387//! ```rust
388//! # use ubits::bitfield;
389//! #    bitfield! {
390//! #        pub u8 ExampleField
391//! #        ExampleFlags {
392//! #            0 : Flag0,
393//! #            1 : Flag1,
394//! #            2 : Flag2,
395//! #            3 : Flag3,
396//! #            4 : Flag4,
397//! #            5 : Flag5,
398//! #            6 : Flag6,
399//! #            7 : Flag7,
400//! #        }
401//! #    }
402//!
403//! let mut a = ExampleField::from_binary_str("11000011");
404//! let b = ExampleField::from_binary_str("01111110");
405//! assert_eq!("01000010", a.intersect(b).as_binary());
406//! ```
407//!
408//! Get the diff of two bitfields: <br>
409//! (use `into_diff` to consume self)
410//!
411//! ```rust
412//! # use ubits::bitfield;
413//! #    bitfield! {
414//! #        pub u8 ExampleField
415//! #        ExampleFlags {
416//! #            0 : Flag0,
417//! #            1 : Flag1,
418//! #            2 : Flag2,
419//! #            3 : Flag3,
420//! #            4 : Flag4,
421//! #            5 : Flag5,
422//! #            6 : Flag6,
423//! #            7 : Flag7,
424//! #        }
425//! #    }
426//!
427//! let mut a = ExampleField::from_binary_str("11000011");
428//! let b = ExampleField::from_binary_str("01100110");
429//! assert_eq!("10100101", a.diff(b).as_binary());
430//! ```
431//!
432//! ### Bitwise
433//!
434//! Both bit field instances and flags use bitwise operators to change bit values.
435//!
436//! ```rust
437//! # use ubits::bitfield;
438//! #    bitfield! {
439//! #        pub u8 ExampleField
440//! #        ExampleFlags {
441//! #            0 : Flag0,
442//! #            1 : Flag1,
443//! #            2 : Flag2,
444//! #            3 : Flag3,
445//! #            4 : Flag4,
446//! #            5 : Flag5,
447//! #            6 : Flag6,
448//! #            7 : Flag7,
449//! #        }
450//! #    }
451//!
452//! let mut from_zeros = ExampleField::zeros();
453//! assert_eq!("00000000", from_zeros.as_binary());
454//!
455//! // set bit to 1
456//! from_zeros |= ExampleFlags::Flag1;
457//! assert_eq!("00000010", from_zeros.as_binary());
458//!
459//! // set bit back to 0
460//! from_zeros &= ExampleFlags::Flag1;
461//! assert_eq!("00000000", from_zeros.as_binary());
462//!
463//! // toggle a bit
464//! from_zeros ^= ExampleFlags::Flag1;
465//! assert_eq!("00000010", from_zeros.as_binary());
466//!
467//! from_zeros ^= ExampleFlags::Flag1;
468//! assert_eq!("00000000", from_zeros.as_binary());
469//! ```
470//!
471//! Operations can also be chained together:
472//!
473//! ```rust
474//! # use ubits::bitfield;
475//! #    bitfield! {
476//! #        pub u8 ExampleField
477//! #        ExampleFlags {
478//! #            0 : Flag0,
479//! #            1 : Flag1,
480//! #            2 : Flag2,
481//! #            3 : Flag3,
482//! #            4 : Flag4,
483//! #            5 : Flag5,
484//! #            6 : Flag6,
485//! #            7 : Flag7,
486//! #        }
487//! #    }
488//!
489//! let mut from_zeros = ExampleField::zeros() | ExampleFlags::Flag1 | ExampleFlags::Flag3;
490//! assert_eq!("00001010", from_zeros.as_binary());
491//!
492//! ```
493//!
494//! Bitfield instances can also be created from combining flags:
495//!
496//! ```rust
497//! # use ubits::bitfield;
498//! #    bitfield! {
499//! #        pub u8 ExampleField
500//! #        ExampleFlags {
501//! #            0 : Flag0,
502//! #            1 : Flag1,
503//! #            2 : Flag2,
504//! #            3 : Flag3,
505//! #            4 : Flag4,
506//! #            5 : Flag5,
507//! #            6 : Flag6,
508//! #            7 : Flag7,
509//! #        }
510//! #    }
511//!
512//! let mut from_zeros = ExampleFlags::Flag1 | ExampleFlags::Flag3;
513//! assert_eq!("00001010", from_zeros.as_binary());
514//!
515//! ```
516//!
517//! ## Fields named with flags
518//!
519//! The generated flags enum allows you to access bits by name.
520//! The flag has an associated [`u8`] value,
521//! which determines the index its target bit.
522//! (See [`bitfield`] for more info)
523//!
524//! With the following input...
525//! ```no_compile
526//! 1 0 1 0 0 1 1 0
527//! ```
528//!
529//! and the following flags...
530//! ```no_compile
531//! 0 : f1
532//! 1 : f1
533//! 2 : f2
534//! 3 : f3
535//! 4 : f4
536//! 5 : f5
537//! 6 : f6
538//! 7 : f7
539//! ```
540//!
541//! we end up with this layout.
542//!
543//! | name      | f7  | f6  | f5  | f4  | f3  | f2  | f1  | f0  |
544//! |-----------|----|----|----|----|----|----|----|----|
545//! | bit value | 1  | 0  | 1  | 0  | 0  | 1  | 1  | 0  |
546//! | index     | 7  | 6  | 5  | 4  | 3  | 2  | 1  | 0  |
547//!
548//!
549//! With the same input and only the first few flags:
550//!
551//! ```no_compile
552//! 0 : f0
553//! 1 : f1
554//! 2 : f2
555//! ```
556//!
557//! we end up with this layout.
558//!
559//! | name      |    |    |    |    |    | f2 | f1 | f0 |
560//! |-----------|----|----|----|----|----|----|----|----|
561//! | bit value | 1  | 0  | 1  | 0  | 0  | 1  | 1  | 0  |
562//! | index     | 7  | 6  | 5  | 4  | 3  | 2  | 1  | 0  |
563//!
564//!
565//! Using the same input, but with dispersed flags:
566//!
567//! ```no_compile
568//! 1 : f0
569//! 3 : f1
570//! 6 : f2
571//! ```
572//!
573//! we end up with this layout.
574//!
575//! | name      |    | f2 |    |    | f1 |    | f0 |    |
576//! |-----------|----|----|----|----|----|----|----|----|
577//! | bit value | 1  | 0  | 1  | 0  | 0  | 1  | 1  | 0  |
578//! | index     | 7  | 6  | 5  | 4  | 3  | 2  | 1  | 0  |
579//!
580
581// -------------------------------------------------------------------------------------------------
582// Imports
583
584extern crate doc_comment;
585extern crate paste;
586extern crate safe_transmute;
587
588#[doc(hidden)]
589pub use doc_comment::doc_comment as __doc_comment;
590
591#[doc(hidden)]
592pub use safe_transmute::{
593    transmute_one as __transmute_one, TriviallyTransmutable as __TriviallyTransmutable,
594};
595
596#[doc(hidden)]
597pub use paste::paste as __paste;
598
599// -------------------------------------------------------------------------------------------------
600// Generator Macros
601
602/// Generates a bitfield struct and a corresponding flag enum.
603///
604/// Can be used multiple times in a single codebase
605/// as long as the identifiers are unique or are isolated so as not to clash.
606///
607/// __See the [`examples`] module for examples of the generated output.__
608///
609/// # Args
610///
611/// - `pub` - optional accessor: if provided, both generated items will public.
612/// If omitted, both will be private. <br>
613/// [see here](https://doc.rust-lang.org/reference/visibility-and-privacy.html#visibility-and-privacy) for possible visibility options.
614///
615/// - `ExampleField` - name for the generated bitfield struct.
616/// (results in `struct ExampleField(...)`)
617///
618/// - `ExampleFlags` - name for the generated flag enum.
619/// (results in `enum ExampleFlags { ... }`)
620///
621/// - `u8` - unsigned integer type to use as a bit array. *Must be an unsigned integer* <br>
622/// valid options are: [`u8`],[`u16`], [`u32`], [`u64`], [`u128`], [`usize`]
623///
624/// - `0 : Flag0` - defines each member of the flag enum. <br>
625/// The left hand side must be a [`u8`] `(0..=254)`
626/// and must be less than the number of bits in the underlying unsigned type.
627/// This value determines the index of the target bit within the field. <br>
628/// The right hand side can be any valid identifier (unique to this enum).
629/// These identifiers will be used to access the corresponding field.
630///
631/// - `/// docstrings` - Each element can take optional docstrings.
632/// This includes the bit field struct, the flag enum, and each member within the flag enum.
633/// See [`examples`] for more information.
634///
635/// # Examples:
636///
637/// Full Example - (see below for explanation for each parameter):
638/// ```rust
639/// use ubits::bitfield;
640///
641///     bitfield! {
642///         /// Optional docstring
643///         /// for [`ExampleField`] struct
644///         pub u8 ExampleField
645///         /// Optional docstring
646///         /// for [`ExampleFlags`] enum
647///         ExampleFlags {
648///             /// Optional docstring for [`ExampleFlags::Flag0`]
649///             0 : Flag0,
650///             /// Optional docstring for [`ExampleFlags::Flag1`]
651///             1 : Flag1,
652///             /// Optional docstring for [`ExampleFlags::Flag2`]
653///             2 : Flag2,
654///             /// Optional docstring for [`ExampleFlags::Flag3`]
655///             3 : Flag3,
656///             /// Optional docstring for [`ExampleFlags::Flag4`]
657///             4 : Flag4,
658///             /// Optional docstring for [`ExampleFlags::Flag5`]
659///             5 : Flag5,
660///             /// Optional docstring for [`ExampleFlags::Flag6`]
661///             6 : Flag6,
662///             /// Optional docstring for [`ExampleFlags::Flag7`]
663///             7 : Flag7,
664///         }
665///     }
666/// ```
667///
668/// You don't have to name *all* the fields if you don't need them...
669///
670/// You can use just a few from the front:
671/// ```rust
672/// use ubits::bitfield;
673///
674///     bitfield! {
675///         /// Optional docstring
676///         /// for [`ExampleField`] struct
677///         pub u8 ExampleField
678///         /// Optional docstring
679///         /// for [`ExampleFlags`] enum
680///         ExampleFlags {
681///             /// Optional docstring for [`ExampleFlags::Flag0`]
682///             0 : Flag0,
683///             /// Optional docstring for [`ExampleFlags::Flag1`]
684///             1 : Flag1,
685///             /// Optional docstring for [`ExampleFlags::Flag2`]
686///             2 : Flag2,
687///         }
688///     }
689/// ```
690///
691/// ... or any valid indices:
692/// ```rust
693/// use ubits::bitfield;
694///
695///     bitfield! {
696///         /// Optional docstring
697///         /// for [`ExampleField`] struct
698///         pub u8 ExampleField
699///         /// Optional docstring
700///         /// for [`ExampleFlags`] enum
701///         ExampleFlags {
702///             /// Optional docstring for [`ExampleFlags::Flag0`]
703///             0 : Flag0,
704///             /// Optional docstring for [`ExampleFlags::Flag1`]
705///             3 : Flag1,
706///             /// Optional docstring for [`ExampleFlags::Flag2`]
707///             6 : Flag2,
708///         }
709///     }
710/// ```
711/// ... or just omit the flags altogether and use indices only.:
712/// ```rust
713/// use ubits::bitfield;
714///     bitfield! {
715///         /// Optional docstring
716///         /// for [`ExampleField`] struct
717///         pub u8 ExampleField
718///     }
719/// ```
720/// __Note: if you omit flags, no flag enum will be generated
721/// and no methods for the bitfield struct that rely on flags will be generated.__
722///
723#[macro_export(local_inner_macros)]
724macro_rules! bitfield {
725
726    // U8
727    (
728        $(#[$doc:meta])*
729        $access:vis u8 $name:ident
730        $(
731            $(#[$flag_doc:meta])*
732            $flag:ident {
733                $(
734                    $(#[$member_doc:meta])*
735                    $idx:literal : $field:ident $( : ( $named_getter:ident ) )?,
736                )*
737            }
738        )?
739    ) => {
740        __bitfield_unchecked!( $(#[$doc])* $access [u8] [8] [u8::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
741    };
742
743    // U16
744    (
745        $(#[$doc:meta])*
746        $access:vis u16 $name:ident
747        $(
748            $(#[$flag_doc:meta])*
749            $flag:ident {
750                $(
751                    $(#[$member_doc:meta])*
752                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
753                )*
754            }
755        )?
756    ) => {
757        __bitfield_unchecked!( $(#[$doc])* $access [u16] [16] [u16::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
758    };
759
760    // U32
761    (
762        $(#[$doc:meta])*
763        $access:vis u32 $name:ident
764        $(
765            $(#[$flag_doc:meta])*
766            $flag:ident {
767                $(
768                    $(#[$member_doc:meta])*
769                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
770                )*
771            }
772        )?
773    ) => {
774        __bitfield_unchecked!( $(#[$doc])* $access [u32] [32] [u32::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
775    };
776
777    // U64
778    (
779        $(#[$doc:meta])*
780        $access:vis u64 $name:ident
781        $(
782            $(#[$flag_doc:meta])*
783            $flag:ident {
784                $(
785                    $(#[$member_doc:meta])*
786                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
787                )*
788            }
789        )?
790    ) => {
791        __bitfield_unchecked!( $(#[$doc])* $access [u64] [64] [u64::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
792    };
793
794    // U128
795    (
796        $(#[$doc:meta])*
797        $access:vis u128 $name:ident
798        $(
799            $(#[$flag_doc:meta])*
800            $flag:ident {
801                $(
802                    $(#[$member_doc:meta])*
803                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
804                )*
805            }
806        )?
807    ) => {
808        __bitfield_unchecked!( $(#[$doc])* $access [u128] [128] [u128::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
809    };
810
811    // USIZE
812    (
813        $(#[$doc:meta])*
814        $access:vis usize $name:ident
815        $(
816            $(#[$flag_doc:meta])*
817            $flag:ident {
818                $(
819                    $(#[$member_doc:meta])*
820                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
821                )*
822            }
823        )?
824    ) => {
825        __bitfield_unchecked!( $(#[$doc])* $access [usize] [usize::BITS as usize] [usize::MAX] $name $($(#[$flag_doc])* $flag { $( $(#[$member_doc])* $idx : $field $(($named_getter))?, )* } )?);
826    };
827}
828
829/// Not to be used directly.
830///
831/// Generates a bitfield struct and flag enum
832/// but does no type checking.
833/// Passing types that aren't unsigned integer types is undefined behaviour.
834///
835/// Use the [`bitfield`] macro instead as it enforces a closed set of types.
836#[doc(hidden)]
837#[macro_export(local_inner_macros)]
838macro_rules! __bitfield_unchecked {
839    (
840        $(#[$field_doc:meta])*
841        $access:vis [$type:ty] [$($bits:tt)*] [$max:path] $name:ident
842        $(
843            $(#[$flag_doc:meta])*
844            $flag:ident {
845                $(
846                    $(#[$member_doc:meta])*
847                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
848                )*
849            }
850        )?
851    ) => {
852
853        $(
854            __def_flag_enum! {
855                $(#[$flag_doc])*
856                $access $name
857                $flag {
858                    $(
859                        $(#[$member_doc])*
860                        $idx : $field $(($named_getter))?,
861                    )*
862                }
863            }
864        )?
865
866        __def_field_struct! {
867            $(#[$field_doc])*
868            $access [$type] [$($bits)*] [$max] $name
869            $(
870                $flag {
871                    $(
872                        $(#[$member_doc])*
873                        $idx : $field $(($named_getter))?,
874                    )*
875                }
876            )?
877        }
878    };
879}
880
881// -------------------------------------------------------------------------------------------------
882// Type Definitions
883
884/// Defines a field struct with its members and implementations
885#[doc(hidden)]
886#[macro_export(local_inner_macros)]
887macro_rules! __def_field_struct {
888    (
889        $(#[$field_doc:meta])*
890        $access:vis [$type:ty] [$($bits:tt)*] [$max:path] $name:ident
891        $(
892            $flag:ident {
893                $(
894                    $(#[$member_doc:meta])*
895                    $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
896                )*
897            }
898        )?
899    ) => {
900
901        // Struct: $name
902        $(#[$field_doc])*
903        #[derive(Copy, Clone, PartialEq, PartialOrd)]
904        $access struct $name($type);
905
906        $(
907            impl $name {
908                $(
909                    $(
910                        __paste! {
911                            pub fn [< is_ $named_getter >](&self) -> bool {
912                                self.get($flag::$field)
913                            }
914                        }
915
916                        __paste! {
917                            pub fn [< set_ $named_getter >](&mut self) {
918                                self.set($flag::$field);
919                            }
920                        }
921
922                        __paste! {
923                            pub fn [< clear_ $named_getter >](&mut self) {
924                                self.clear($flag::$field);
925                            }
926                        }
927
928                        __paste! {
929                            pub fn [< toggle_ $named_getter >](&mut self) {
930                                self.toggle($flag::$field);
931                            }
932                        }
933                    )?
934                )*
935            }
936        )?
937
938        // Constants
939        __impl_field_constants! {
940            $name { type: $type, max: $max, bits: [$($bits)*] }
941        }
942
943        // Constructors
944        __impl_field_ctors! {
945            $name : $type
946        }
947
948        // State
949        __impl_field_state! {
950            $name : $type
951        }
952
953        // Indexers
954        __impl_field_index_accessors! {
955            $name
956        }
957
958        $(
959            // Flags
960            __impl_field_flag_accessors! {
961                $name $flag $type
962            }
963        )?
964
965        // Combinators
966        __impl_field_flag_combinators! {
967            $name
968        }
969
970        // Converters
971        __impl_field_flag_converters! {
972            $name
973        }
974
975        // Name: $name
976        __impl_default! {
977            $name => { $name(0) }
978        }
979
980        // From: $type -> $name
981        __impl_from! {
982            /// Returns a field instance from value of its underlying type.
983            $type as $name (value) => {
984                $name(value)
985            }
986        }
987
988        $(
989            // From: $flag -> $name
990            __impl_from! {
991                /// Returns a field instance from a flag.
992                $flag as $name (value) => {
993                    Self(0 | (1 << (value as $type)) )
994                }
995            }
996        )?
997
998        // Bitwise: $name + $name
999        __impl_bitwise_operators! {
1000            [Self] for $name : (self rhs -> Self)
1001            BitAnd => { Self(self.0 & rhs.0) }
1002            BitOr => { Self(self.0 | rhs.0) }
1003            BitXor => {  Self(self.0 ^ rhs.0) }
1004            BitAndAssign => { self.0 &= rhs.0; }
1005            BitOrAssign => { self.0 |= rhs.0; }
1006            BitXorAssign => { self.0 ^= rhs.0; }
1007        }
1008
1009        $(
1010            // Bitwise: $name + $flag
1011            __impl_bitwise_operators! {
1012                [$flag] for $name : (self rhs -> $name)
1013                BitAnd => { $name(self.0 & !(1 << (rhs as $type))) }
1014                BitOr => { $name(self.0 | (1 << (rhs as $type))) }
1015                BitXor => { $name(self.0 ^ (1 << (rhs as $type))) }
1016                BitAndAssign => { self.clear(rhs); }
1017                BitOrAssign => { self.set(rhs); }
1018                BitXorAssign => { self.toggle(rhs); }
1019            }
1020
1021            // Bitwise: &$name + $flag
1022            __impl_bitwise_operators! {
1023                [$flag] for & $name : (self rhs -> $name)
1024                BitAnd => { $name(self.0 & !(1 << (rhs as $type))) }
1025                BitOr => { $name(self.0 | (1 << (rhs as $type))) }
1026                BitXor => { $name(self.0 ^ (1 << (rhs as $type))) }
1027            }
1028
1029            // Bitwise: $mut $name + $flag
1030            __impl_bitwise_operators! {
1031                [$flag] for &mut $name : (self rhs -> $name)
1032                BitAnd => { $name(self.0 & !(1 << (rhs as $type))) }
1033                BitOr => { $name(self.0 | (1 << (rhs as $type))) }
1034                BitXor => { $name(self.0 ^ (1 << (rhs as $type))) }
1035            }
1036        )?
1037
1038        // Bitwise: Debug & Binary
1039        __impl_formatters! {
1040            $name (self f) {
1041                Debug => { core::write!(f, "{}({})", core::stringify!($name), self.as_binary()) }
1042                Binary => { core::write!(f, "{}", self.as_binary()) }
1043            }
1044        }
1045    };
1046}
1047
1048/// Defines a flag enum with its members and implementations
1049#[doc(hidden)]
1050#[macro_export(local_inner_macros)]
1051macro_rules! __def_flag_enum {
1052    (
1053        $(#[$flag_doc:meta])*
1054        $access:vis $name:ident
1055        $flag:ident {
1056            $(
1057                $(#[$member_doc:meta])*
1058                $idx:literal : $field:ident $( ( $named_getter:ident ) )?,
1059            )*
1060        }
1061    ) => {
1062
1063        // Enum: $flag
1064        $(#[$flag_doc])*
1065        #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
1066        #[repr(u8)]
1067        $access enum $flag {
1068            $( $(#[$member_doc])* $field = $idx ),*
1069        }
1070
1071        // Bitwise: $flag + $flag
1072        __impl_bitwise_operators! {
1073            [Self] for $flag : (self rhs -> $name)
1074            BitAnd => { $name(0).clear(self) & rhs }
1075            BitOr => { $name(0).set(self) | rhs }
1076            BitXor => { $name(0).toggle(self) ^ rhs }
1077        }
1078
1079        // Bitwise: u8 -> $flag
1080        __impl_from! {
1081            /// Converts a [`u8`] to a flag
1082            ///
1083            /// # Errors
1084            ///
1085            /// Panics if the supplied [`u8`] is out of range for fields base type.
1086            /// eg: for a 16-bit field (using [`u16`]),
1087            /// providing an index of `20` will cause a panic.
1088            u8 as $flag (value) => {
1089                $crate::__transmute_one::<$flag>(&[value])
1090                .expect(std::format!(
1091                    "failed to transmute {} to {}. {} is not a valid index in {}.",
1092                    value,
1093                    core::stringify!($flag),
1094                    value,
1095                    core::stringify!($flag)
1096                ).as_str())
1097            }
1098        }
1099
1100        // Bitwise: $flag -> u8
1101        __impl_from! {
1102            /// Returns a flag as its corresponding [`u8`] value.
1103            $flag as u8 (value) => {
1104                value as u8
1105            }
1106        }
1107
1108        unsafe impl $crate::__TriviallyTransmutable for $flag {}
1109    };
1110}
1111
1112// -------------------------------------------------------------------------------------------------
1113// Field Implementations
1114
1115/// Generates constants for `$name`
1116#[doc(hidden)]
1117#[macro_export]
1118macro_rules! __impl_field_constants {
1119    ($name:ident { type: $type:ty, max: $max:path, bits: [$($bits:tt)*] }) => {
1120        $crate::__doc_comment! {
1121            core::concat!(
1122                "Constant values describing [`", core::stringify!($name), "`]."
1123            ),
1124            impl $name {
1125                $crate::__doc_comment! {
1126                    core::concat!(
1127                      "Number of bits in an instance of [`", core::stringify!($name), "`]."
1128                    ),
1129                    pub const BITS: usize = $($bits)*;
1130                }
1131                $crate::__doc_comment! {
1132                    core::concat!(
1133                      "Number of bytes used by an instance of [`", core::stringify!($name), "`]."
1134                    ),
1135                    pub const BYTES: usize = $($bits)* / 8;
1136                }
1137                $crate::__doc_comment! {
1138                    core::concat!(
1139                      "Maximum valid integer for [`", core::stringify!($name), "`]."
1140                    ),
1141                    pub const MAX: $type = $max;
1142                }
1143            }
1144        }
1145    };
1146}
1147
1148/// Generates constructors for `$name`
1149#[doc(hidden)]
1150#[macro_export]
1151macro_rules! __impl_field_ctors {
1152    ( $name:ident : $type:ty ) => {
1153        $crate::__doc_comment! {
1154            core::concat!(
1155                "Constructors for creating instances of [`", core::stringify!($name), "`]."
1156            ),
1157            impl $name {
1158                $crate::__doc_comment! {
1159                    core::concat!(
1160                      "Create a new instance of [`", core::stringify!($name), "`] ",
1161                      "from a [`", core::stringify!($type), "`] value."
1162                    ),
1163                    pub fn new(value: $type) -> Self {
1164                        Self(value)
1165                    }
1166                }
1167
1168                $crate::__doc_comment! {
1169                    core::concat!(
1170                      "Create a new instance of [`", core::stringify!($name), "`] ",
1171                      "from `0`."
1172                    ),
1173                    pub fn zeros() -> Self {
1174                        Self(0)
1175                    }
1176                }
1177
1178                $crate::__doc_comment! {
1179                    core::concat!(
1180                      "Create a new instance of [`", core::stringify!($name), "`] ",
1181                      "from the maximum possible integer value."
1182                    ),
1183                    pub fn ones() -> Self {
1184                        Self(Self::MAX)
1185                    }
1186                }
1187
1188                $crate::__doc_comment! {
1189                    core::concat!(
1190                      "Create a new instance of [`", core::stringify!($name), "`] ",
1191                      "from a binary string."
1192                    ),
1193                    pub fn from_binary_str(value: &str) -> Self {
1194                        Self(<$type>::from_str_radix(value, 2).unwrap())
1195                    }
1196                }
1197
1198                $crate::__doc_comment! {
1199                    core::concat!(
1200                      "Create a new instance of [`", core::stringify!($name), "`] ",
1201                      "from a binary string."
1202                    ),
1203                    pub fn from_binary_string(value: String) -> Self {
1204                        Self(<$type>::from_str_radix(value.as_str(), 2).unwrap())
1205                    }
1206                }
1207            }
1208        }
1209    };
1210}
1211
1212/// Generates getters for state fields in `$name`
1213#[doc(hidden)]
1214#[macro_export]
1215macro_rules! __impl_field_state {
1216    ($name:ident : $type:ty) => {
1217        $crate::__doc_comment! {
1218            core::concat!(
1219                "Current state of this bitfield."
1220            ),
1221            impl $name {
1222                $crate::__doc_comment! {
1223                    core::concat!(
1224                      "Returns the current field value as a [`", core::stringify!($type), "`]"
1225                    ),
1226                    pub fn as_integer(&self) -> $type {
1227                        self.0
1228                    }
1229                }
1230
1231                $crate::__doc_comment! {
1232                    core::concat!(
1233                      "Returns the current field value as a binary formatted string."
1234                    ),
1235                    pub fn as_binary(&self) -> String {
1236                        std::format!("{:0width$b}", self.0, width = Self::BITS)
1237                    }
1238                }
1239            }
1240        }
1241    };
1242}
1243
1244/// Generates getters by index for bit fields in `$name`
1245#[doc(hidden)]
1246#[macro_export]
1247macro_rules! __impl_field_index_accessors {
1248    ($name:ident) => {
1249        $crate::__doc_comment! {
1250            core::concat!(
1251                "Field accessors by index for [`", core::stringify!($name), "`]."
1252            ),
1253            impl $name {
1254                $crate::__doc_comment! {
1255                    core::concat!(
1256                        "Returns the value of the bit at the supplied index as a boolean."
1257                    ),
1258                    pub fn get_index(&self, index: u8) -> bool {
1259                        ((self.0 >> index) & 1) == 1
1260                    }
1261                }
1262
1263                $crate::__doc_comment! {
1264                    core::concat!(
1265                        "Sets the value of the bit at the supplied index to `1`."
1266                    ),
1267                    pub fn set_index(&mut self, index: u8) {
1268                        self.0 |= (1 << index);
1269                    }
1270                }
1271
1272                $crate::__doc_comment! {
1273                    core::concat!(
1274                        "Sets the value of the bit at the supplied index to `0`."
1275                    ),
1276                    pub fn clear_index(&mut self, index: u8) {
1277                        self.0 &= !(1 << index);
1278                    }
1279                }
1280
1281                $crate::__doc_comment! {
1282                    core::concat!(
1283                        "Flips the value of the bit at the supplied index."
1284                    ),
1285                    pub fn toggle_index(&mut self, index: u8) {
1286                        self.0 ^= (1 << index);
1287                    }
1288                }
1289            }
1290        }
1291    };
1292}
1293
1294/// Generates getters by flag for bit fields in `$name`
1295#[doc(hidden)]
1296#[macro_export]
1297macro_rules! __impl_field_flag_accessors {
1298    ($name:ident $flag:ident $type:ty) => {
1299        $crate::__doc_comment! {
1300            core::concat!(
1301                "Named field accessors by [`", core::stringify!($flag), "`] for [`", core::stringify!($name), "`]."
1302            ),
1303            impl $name {
1304                $crate::__doc_comment! {
1305                    core::concat!(
1306                        "Returns the value of the bit at the supplied flag as a boolean."
1307                    ),
1308                    pub fn get(&self, flag: $flag) -> bool {
1309                        ((self.0 >> (flag as $type)) & 1) == 1
1310                    }
1311                }
1312
1313                $crate::__doc_comment! {
1314                    core::concat!(
1315                        "Sets the value of the bit at the supplied flag to `1`."
1316                    ),
1317                    pub fn set(&mut self, flag: $flag) -> &mut Self {
1318                        self.0 |= (1 << (flag as $type));
1319                        self
1320                    }
1321                }
1322
1323                $crate::__doc_comment! {
1324                    core::concat!(
1325                        "Sets the value of the bit at the supplied flag to `0`."
1326                    ),
1327                    pub fn clear(&mut self, flag: $flag) -> &mut Self {
1328                        self.0 &= !(1 << (flag as $type));
1329                        self
1330                    }
1331                }
1332
1333                $crate::__doc_comment! {
1334                    core::concat!(
1335                        "Flips the value of the bit at the supplied flag."
1336                    ),
1337                    pub fn toggle(&mut self, flag: $flag) -> &mut Self {
1338                        self.0 ^= (1 << (flag as $type));
1339                        self
1340                    }
1341                }
1342            }
1343        }
1344    };
1345}
1346
1347/// Generates combinator methods for `$name`
1348#[doc(hidden)]
1349#[macro_export]
1350macro_rules! __impl_field_flag_combinators {
1351    ($name:ident) => {
1352        $crate::__doc_comment! {
1353            core::concat!(
1354                "Combinators for [`", core::stringify!($name), "`]."
1355            ),
1356            impl $name {
1357                $crate::__doc_comment! {
1358                    core::concat!(
1359                        "Returns a new [`", core::stringify!($name), "`]",
1360                        "with ones for flags that do not match. ",
1361                        "Does not consume `self`."
1362                    ),
1363                    pub fn diff(&self, other: Self) -> Self {
1364                        Self(self.0 ^ other.0)
1365                    }
1366                }
1367
1368                $crate::__doc_comment! {
1369                    core::concat!(
1370                        "Returns a new [`", core::stringify!($name), "`]",
1371                        "with ones for flags that were set on either input. ",
1372                        "Does not consume `self`."
1373                    ),
1374                    pub fn combine(&self, other: Self) -> Self {
1375                        Self(self.0 | other.0)
1376                    }
1377                }
1378
1379                $crate::__doc_comment! {
1380                    core::concat!(
1381                        "Returns a new [`", core::stringify!($name), "`]",
1382                        "with ones for flags that were set on both inputs. ",
1383                        "Does not consume `self`."
1384                    ),
1385                    pub fn intersect(&self, other: Self) -> Self {
1386                        Self(self.0 & other.0)
1387                    }
1388                }
1389            }
1390        }
1391    };
1392}
1393
1394/// Generates conversion methods for `$name`
1395#[doc(hidden)]
1396#[macro_export]
1397macro_rules! __impl_field_flag_converters {
1398    ($name:ident) => {
1399        $crate::__doc_comment! {
1400            core::concat!(
1401                "Conversion methods."
1402            ),
1403            impl $name {
1404                $crate::__doc_comment! {
1405                    core::concat!(
1406                        "Returns a new [`", core::stringify!($name), "`]",
1407                        "with ones for flags that do not match. ",
1408                        "Consumes `self`."
1409                    ),
1410                    pub fn into_diff(self, other: Self) -> Self {
1411                        Self(self.0 ^ other.0)
1412                    }
1413                }
1414
1415                $crate::__doc_comment! {
1416                    core::concat!(
1417                        "Returns a new [`", core::stringify!($name), "`]",
1418                        "with ones for flags that were set on either input. ",
1419                        "Consumes `self`."
1420                    ),
1421                    pub fn into_combined(self, other: Self) -> Self {
1422                        Self(self.0 | other.0)
1423                    }
1424                }
1425
1426                $crate::__doc_comment! {
1427                    core::concat!(
1428                        "Returns a new [`", core::stringify!($name), "`]",
1429                        "with ones for flags that were set on both inputs. ",
1430                        "Consumes `self`."
1431                    ),
1432                    pub fn into_intersection(self, other: Self) -> Self {
1433                        Self(self.0 & other.0)
1434                    }
1435                }
1436            }
1437        }
1438    };
1439}
1440
1441// -------------------------------------------------------------------------------------------------
1442// Trait Implementations
1443
1444// Operators
1445
1446/// Generates a set of bitwise operators
1447/// for a set of parameters
1448#[doc(hidden)]
1449#[macro_export(local_inner_macros)]
1450macro_rules! __impl_bitwise_operators {
1451
1452    // Owned
1453    (
1454        [$flag:ty] for $dest:ident : ($self:ident $other:ident -> $output:ident)
1455        $( BitAnd => $bitand:block )?
1456        $( BitOr => $bitor:block )?
1457        $( BitXor => $bitxor:block )?
1458        $( BitAndAssign => $bitand_assign:block )?
1459        $( BitOrAssign => $bitor_assign:block )?
1460        $( BitXorAssign => $bitxor_assign:block )?
1461    ) => {
1462        $( __impl_operator! {BitAnd [$flag] bitand for $dest ($self $other -> $output) => $bitand} )?
1463        $( __impl_operator! {BitOr [$flag] bitor for $dest ($self $other -> $output) => $bitor} )?
1464        $( __impl_operator! {BitXor [$flag] bitxor for $dest ($self $other -> $output) => $bitxor} )?
1465
1466        $( __impl_assign_operator! {BitAndAssign [$flag] bitand_assign for $dest ($self $other) => $bitand_assign} )?
1467        $( __impl_assign_operator! {BitOrAssign [$flag] bitor_assign for $dest ($self $other) => $bitor_assign} )?
1468        $( __impl_assign_operator! {BitXorAssign [$flag] bitxor_assign for $dest ($self $other) => $bitxor_assign} )?
1469    };
1470
1471    // Reference
1472    (
1473        [$flag:ty] for & $dest:ident : ($self:ident $other:ident -> $output:ident)
1474        $( BitAnd => $bitand:block )?
1475        $( BitOr => $bitor:block )?
1476        $( BitXor => $bitxor:block )?
1477    ) => {
1478        $( __impl_operator! {BitAnd [$flag] bitand for & $dest ($self $other -> $output) => $bitand} )?
1479        $( __impl_operator! {BitOr [$flag] bitor for & $dest ($self $other -> $output) => $bitor} )?
1480        $( __impl_operator! {BitXor [$flag] bitxor for & $dest ($self $other -> $output) => $bitxor} )?
1481    };
1482
1483    // Mutable Reference
1484    (
1485        [$flag:ty] for &mut $dest:ident : ($self:ident $other:ident -> $output:ident)
1486        $( BitAnd => $bitand:block )?
1487        $( BitOr => $bitor:block )?
1488        $( BitXor => $bitxor:block )?
1489    ) => {
1490        $( __impl_operator! {BitAnd [$flag] bitand for &mut $dest ($self $other -> $output) => $bitand} )?
1491        $( __impl_operator! {BitOr [$flag] bitor for &mut $dest ($self $other -> $output) => $bitor} )?
1492        $( __impl_operator! {BitXor [$flag] bitxor for &mut $dest ($self $other -> $output) => $bitxor} )?
1493    };
1494}
1495
1496/// Generates a generic operator
1497#[doc(hidden)]
1498#[macro_export]
1499macro_rules! __impl_operator {
1500
1501    // Owned
1502    ( $op_type:ident [$rhs:ty] $op_lower:ident for $dest:ident ($self:ident $other:ident -> $output:ident) => $block:block ) => {
1503        impl std::ops::$op_type<$rhs> for $dest {
1504            type Output = $output;
1505            fn $op_lower($self, $other: $rhs) -> Self::Output $block
1506        }
1507    };
1508
1509    // Reference
1510    ( $op_type:ident [$rhs:ty] $op_lower:ident for & $dest:ident ($self:ident $other:ident -> $output:ident) => $block:block ) => {
1511        impl std::ops::$op_type<$rhs> for &$dest {
1512            type Output = $output;
1513            fn $op_lower($self, $other: $rhs) -> Self::Output $block
1514        }
1515    };
1516
1517    // Mutable Reference
1518    ( $op_type:ident [$rhs:ty] $op_lower:ident for &mut $dest:ident ($self:ident $other:ident -> $output:ident) => $block:block ) => {
1519        impl std::ops::$op_type<$rhs> for &mut $dest {
1520            type Output = $output;
1521            fn $op_lower($self, $other: $rhs) -> Self::Output $block
1522        }
1523    };
1524}
1525
1526/// Generates a generic assign operator
1527#[doc(hidden)]
1528#[macro_export]
1529macro_rules! __impl_assign_operator {
1530    ( $op_type:ident [$rhs:ty] $op_lower:ident for $dest:ident ($self:ident $other:ident) => $block:block ) => {
1531        impl std::ops::$op_type<$rhs> for $dest {
1532            fn $op_lower(&mut $self, $other: $rhs) $block
1533        }
1534    };
1535}
1536
1537// Defaults
1538
1539/// Generates a [`Default`] implementation.
1540#[doc(hidden)]
1541#[macro_export]
1542macro_rules! __impl_default {
1543    ($name:ident => $block:block) => {
1544        impl Default for $name {
1545            fn default() -> Self $block
1546        }
1547    };
1548}
1549
1550// Converters
1551
1552/// Generates a [`From`] implementation.
1553#[doc(hidden)]
1554#[macro_export]
1555macro_rules! __impl_from {
1556    ($(#[$doc:meta])* $from:ty as $to:ident ($arg:ident) => $block:block) => {
1557        $(#[$doc])*
1558        impl From<$from> for $to {
1559            fn from($arg: $from) -> $to $block
1560        }
1561    };
1562}
1563
1564// Formatters
1565
1566/// Generates formatter implementations
1567/// from a set of parameters.
1568#[doc(hidden)]
1569#[macro_export(local_inner_macros)]
1570macro_rules! __impl_formatters {
1571    ($name:ident ($self:ident $f:ident) { $( $formatter:ident => $block:block )+ } ) => {
1572        $( __impl_formatter!($formatter for $name ($self $f) => $block); )+
1573    };
1574}
1575
1576/// Generates a generic formatter implementation.
1577#[doc(hidden)]
1578#[macro_export]
1579macro_rules! __impl_formatter {
1580    ($formatter:ident for $name:ident ($self:ident $f:ident) => $block:block) => {
1581        impl std::fmt::$formatter for $name {
1582            fn fmt(&$self, $f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result $block
1583        }
1584    };
1585}
1586
1587// -------------------------------------------------------------------------------------------------
1588// Example
1589
1590#[allow(dead_code)]
1591#[allow(unused_variables)]
1592#[cfg(doc)]
1593pub mod examples {
1594    //! Examples of generated output using different configurations.
1595    //!
1596    //! __This module is only compiled with documentation builds
1597    //! and will not be available for import.__
1598    //!
1599    //! use the [`bitfield`] macro to generate a field struct and flag enum.
1600    //!
1601
1602    #[allow(dead_code)]
1603    #[allow(unused_variables)]
1604    #[cfg(doc)]
1605    pub mod full_example {
1606        //! Example of generated output, using both a bit field and a flag enum.
1607        //!
1608        //! __This module is only compiled with documentation builds
1609        //! and will not be available for import.__
1610        //!
1611        //! use the [`bitfield`] macro to generate a field struct and flag enum.
1612        //!
1613        use super::*;
1614
1615        bitfield! {
1616            /// Optional docstring for my example bit field.
1617            pub u8 ExampleField
1618            /// Optional docstring for my flag enum.
1619            ExampleFlags {
1620                /// Optional docstring for Flag0
1621                0 : Flag0,
1622                /// Optional docstring for Flag1
1623                1 : Flag1,
1624                /// Optional docstring for Flag2
1625                2 : Flag2,
1626                /// Optional docstring for Flag3
1627                3 : Flag3,
1628                /// Optional docstring for Flag4
1629                4 : Flag4,
1630                /// Optional docstring for Flag5
1631                5 : Flag5,
1632                /// Optional docstring for Flag6
1633                6 : Flag6,
1634                /// Optional docstring for Flag7
1635                7 : Flag7,
1636            }
1637        }
1638    }
1639
1640    #[allow(dead_code)]
1641    #[allow(unused_variables)]
1642    #[cfg(doc)]
1643    pub mod flagless_example {
1644        //! Example of generated output, using only a bit field without a flag enum.
1645        //!
1646        //! __This module is only compiled with documentation builds
1647        //! and will not be available for import.__
1648        //!
1649        //! use the [`bitfield`] macro to generate a field struct and flag enum.
1650        //!
1651        use super::*;
1652
1653        bitfield! {
1654            /// Optional docstring for my example bit field.
1655            pub u8 ExampleField
1656        }
1657    }
1658}
1659
1660// -------------------------------------------------------------------------------------------------
1661// Tests
1662
1663#[allow(dead_code)]
1664#[allow(unused_variables)]
1665#[cfg(test)]
1666mod test {
1667    use super::*;
1668    use std::{u128, u16, u32, u64, usize};
1669
1670    macro_rules! tests {
1671        ( $( $name:ident => $block:block )+ ) => {
1672            $(
1673                #[test]
1674                fn $name() $block
1675            )+
1676        };
1677    }
1678
1679    bitfield! {
1680        /// My Test Thing
1681        u8 MyFieldU8
1682        MyFlagsU8 {
1683            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1684        }
1685    }
1686
1687    bitfield! {
1688        /// My Test Thing
1689        u16 MyFieldU16
1690        MyFlagsU16 {
1691            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1692            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1693        }
1694    }
1695
1696    bitfield! {
1697        /// My Test Thing
1698        u32 MyFieldU32
1699        MyFlagsU32 {
1700            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1701            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1702            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1703            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1704        }
1705    }
1706
1707    bitfield! {
1708        /// My Test Thing
1709        u64 MyFieldU64
1710        MyFlagsU64 {
1711            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1712            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1713            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1714            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1715            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1716            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1717            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1718            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1719        }
1720    }
1721
1722    bitfield! {
1723        /// My Test Thing
1724        u128 MyFieldU128
1725        MyFlagsU128 {
1726            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1727            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1728            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1729            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1730            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1731            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1732            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1733            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1734            64 : Flag64, 65 : Flag65, 66 : Flag66, 67 : Flag67, 68 : Flag68, 69 : Flag69, 70 : Flag70, 71 : Flag71,
1735            72 : Flag72, 73 : Flag73, 74 : Flag74, 75 : Flag75, 76 : Flag76, 77 : Flag77, 78 : Flag78, 79 : Flag79,
1736            80 : Flag80, 81 : Flag81, 82 : Flag82, 83 : Flag83, 84 : Flag84, 85 : Flag85, 86 : Flag86, 87 : Flag87,
1737            88 : Flag88, 89 : Flag89, 90 : Flag90, 91 : Flag91, 92 : Flag92, 93 : Flag93, 94 : Flag94, 95 : Flag95,
1738            96 : Flag96, 97 : Flag97, 98 : Flag98, 99 : Flag99, 100 : Flag100, 101 : Flag101, 102 : Flag102, 103 : Flag103,
1739            104 : Flag104, 105 : Flag105, 106 : Flag106, 107 : Flag107, 108 : Flag108, 109 : Flag109, 110 : Flag110, 111 : Flag111,
1740            112 : Flag112, 113 : Flag113, 114 : Flag114, 115 : Flag115, 116 : Flag116, 117 : Flag117, 118 : Flag118, 119 : Flag119,
1741            120 : Flag120, 121 : Flag121, 122 : Flag122, 123 : Flag123, 124 : Flag124, 125 : Flag125, 126 : Flag126, 127 : Flag127,
1742        }
1743    }
1744
1745    #[cfg(target_pointer_width = "64")]
1746    bitfield! {
1747        /// My Test Thing
1748        usize MyFieldUsize
1749        MyFlagsUsize {
1750            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1751            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1752            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1753            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1754            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1755            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1756            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1757            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1758        }
1759    }
1760
1761    #[cfg(target_pointer_width = "32")]
1762    bitfield! {
1763        /// My Test Thing
1764        usize MyFieldUsize
1765        MyFlagsUsize {
1766            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1767            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1768            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1769            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1770        }
1771    }
1772
1773    bitfield! {
1774        /// My Test Thing
1775        u8 MyFieldU8Nf
1776        MyFlagsU8Nf {
1777            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1778        }
1779    }
1780
1781    bitfield! {
1782        /// My Test Thing
1783        u16 MyFieldU16Nf
1784        MyFlagsU16Nf {
1785            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1786            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1787        }
1788    }
1789
1790    bitfield! {
1791        /// My Test Thing
1792        u32 MyFieldU32Nf
1793        MyFlagsU32Nf {
1794            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1795            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1796            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1797            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1798        }
1799    }
1800
1801    bitfield! {
1802        /// My Test Thing
1803        u64 MyFieldU64Nf
1804        MyFlagsU64Nf {
1805            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1806            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1807            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1808            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1809            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1810            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1811            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1812            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1813        }
1814    }
1815
1816    bitfield! {
1817        /// My Test Thing
1818        u128 MyFieldU128Nf
1819        MyFlagsU128Nf {
1820            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1821            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1822            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1823            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1824            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1825            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1826            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1827            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1828            64 : Flag64, 65 : Flag65, 66 : Flag66, 67 : Flag67, 68 : Flag68, 69 : Flag69, 70 : Flag70, 71 : Flag71,
1829            72 : Flag72, 73 : Flag73, 74 : Flag74, 75 : Flag75, 76 : Flag76, 77 : Flag77, 78 : Flag78, 79 : Flag79,
1830            80 : Flag80, 81 : Flag81, 82 : Flag82, 83 : Flag83, 84 : Flag84, 85 : Flag85, 86 : Flag86, 87 : Flag87,
1831            88 : Flag88, 89 : Flag89, 90 : Flag90, 91 : Flag91, 92 : Flag92, 93 : Flag93, 94 : Flag94, 95 : Flag95,
1832            96 : Flag96, 97 : Flag97, 98 : Flag98, 99 : Flag99, 100 : Flag100, 101 : Flag101, 102 : Flag102, 103 : Flag103,
1833            104 : Flag104, 105 : Flag105, 106 : Flag106, 107 : Flag107, 108 : Flag108, 109 : Flag109, 110 : Flag110, 111 : Flag111,
1834            112 : Flag112, 113 : Flag113, 114 : Flag114, 115 : Flag115, 116 : Flag116, 117 : Flag117, 118 : Flag118, 119 : Flag119,
1835            120 : Flag120, 121 : Flag121, 122 : Flag122, 123 : Flag123, 124 : Flag124, 125 : Flag125, 126 : Flag126, 127 : Flag127,
1836        }
1837    }
1838
1839    #[cfg(target_pointer_width = "64")]
1840    bitfield! {
1841        /// My Test Thing
1842        usize MyFieldUsizeNf
1843        MyFlagsUsizeNf {
1844            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1845            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1846            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1847            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1848            32 : Flag32, 33 : Flag33, 34 : Flag34, 35 : Flag35, 36 : Flag36, 37 : Flag37, 38 : Flag38, 39 : Flag39,
1849            40 : Flag40, 41 : Flag41, 42 : Flag42, 43 : Flag43, 44 : Flag44, 45 : Flag45, 46 : Flag46, 47 : Flag47,
1850            48 : Flag48, 49 : Flag49, 50 : Flag50, 51 : Flag51, 52 : Flag52, 53 : Flag53, 54 : Flag54, 55 : Flag55,
1851            56 : Flag56, 57 : Flag57, 58 : Flag58, 59 : Flag59, 60 : Flag60, 61 : Flag61, 62 : Flag62, 63 : Flag63,
1852        }
1853    }
1854
1855    #[cfg(target_pointer_width = "32")]
1856    bitfield! {
1857        /// My Test Thing
1858        usize MyFieldUsizeNf
1859        MyFlagsUsizeNf {
1860            0 : Flag0, 1 : Flag1, 2 : Flag2, 3 : Flag3, 4 : Flag4, 5 : Flag5, 6 : Flag6, 7 : Flag7,
1861            8 : Flag8, 9 : Flag9, 10 : Flag10, 11 : Flag11, 12 : Flag12, 13 : Flag13, 14 : Flag14, 15 : Flag15,
1862            16 : Flag16, 17 : Flag17, 18 : Flag18, 19 : Flag19, 20 : Flag20, 21 : Flag21, 22 : Flag22, 23 : Flag23,
1863            24 : Flag24, 25 : Flag25, 26 : Flag26, 27 : Flag27, 28 : Flag28, 29 : Flag29, 30 : Flag30, 31 : Flag31,
1864        }
1865    }
1866
1867    const U8: u8 = 123;
1868    const B8: &str = "01111011";
1869    const U16: u16 = 51526;
1870    const B16: &str = "1100100101000110";
1871    const U32: u32 = 2828091834;
1872    const B32: &str = "10101000100100010100000110111010";
1873    const U64: u64 = 18325744043706550615;
1874    const B64: &str = "1111111001010010000111110001110100100001110111000000100101010111";
1875    const U128: u128 = 160140183460469230739687303714882104727;
1876    const B128: &str = "01111000011110011110000010100001011000111110000100101011010111011111111110000110011101100010110100100000110001101010110110010111";
1877
1878    #[cfg(target_pointer_width = "64")]
1879    const USIZE: usize = 18325744043706550615;
1880    #[cfg(target_pointer_width = "64")]
1881    const BSIZE: &str = "1111111001010010000111110001110100100001110111000000100101010111";
1882
1883    #[cfg(target_pointer_width = "32")]
1884    const USIZE: usize = 2828091834;
1885    #[cfg(target_pointer_width = "32")]
1886    const BSIZE: &str = "10101000100100010100000110111010";
1887
1888    // constructors
1889    tests! {
1890        new_u8 => {
1891            MyFieldU8(U8);
1892        }
1893        new_u16 => {
1894            MyFieldU16(U16);
1895        }
1896        new_u32 => {
1897            MyFieldU32(U32);
1898        }
1899        new_u64 => {
1900            MyFieldU64(U64);
1901        }
1902        new_u128 => {
1903            MyFieldU128(U128);
1904        }
1905        new_usize => {
1906            MyFieldUsize(USIZE);
1907        }
1908    }
1909
1910    // from binary str
1911    tests! {
1912        from_binary_str_u8 => {
1913            assert_eq!(MyFieldU8::from_binary_str(B8), MyFieldU8(U8));
1914        }
1915        from_binary_str_u16 => {
1916            assert_eq!(MyFieldU16::from_binary_str(B16), MyFieldU16(U16));
1917        }
1918        from_binary_str_u32 => {
1919            assert_eq!(MyFieldU32::from_binary_str(B32), MyFieldU32(U32));
1920        }
1921        from_binary_str_u64 => {
1922            assert_eq!(MyFieldU64::from_binary_str(B64), MyFieldU64(U64));
1923        }
1924        from_binary_str_u128 => {
1925            assert_eq!(MyFieldU128::from_binary_str(B128), MyFieldU128(U128));
1926        }
1927        from_binary_str_usize => {
1928            assert_eq!(MyFieldUsize::from_binary_str(BSIZE), MyFieldUsize(USIZE));
1929        }
1930    }
1931
1932    // from binary string
1933    tests! {
1934        from_binary_string_u8 => {
1935            assert_eq!(MyFieldU8::from_binary_string(String::from(B8)), MyFieldU8(U8));
1936        }
1937        from_binary_string_u16 => {
1938            assert_eq!(MyFieldU16::from_binary_string(String::from(B16)), MyFieldU16(U16));
1939        }
1940        from_binary_string_u32 => {
1941            assert_eq!(MyFieldU32::from_binary_string(String::from(B32)), MyFieldU32(U32));
1942        }
1943        from_binary_string_u64 => {
1944            assert_eq!(MyFieldU64::from_binary_string(String::from(B64)), MyFieldU64(U64));
1945        }
1946        from_binary_string_u128 => {
1947            assert_eq!(MyFieldU128::from_binary_string(String::from(B128)), MyFieldU128(U128));
1948        }
1949        from_binary_string_usize => {
1950            assert_eq!(MyFieldUsize::from_binary_string(String::from(BSIZE)), MyFieldUsize(USIZE));
1951        }
1952    }
1953
1954    // ones
1955    tests! {
1956        ones_u8 => {
1957            assert_eq!(MyFieldU8::ones(), MyFieldU8(u8::MAX));
1958        }
1959        ones_u16 => {
1960            assert_eq!(MyFieldU16::ones(), MyFieldU16(u16::MAX));
1961        }
1962        ones_u32 => {
1963            assert_eq!(MyFieldU32::ones(), MyFieldU32(u32::MAX));
1964        }
1965        ones_u64 => {
1966            assert_eq!(MyFieldU64::ones(), MyFieldU64(u64::MAX));
1967        }
1968        ones_u128 => {
1969            assert_eq!(MyFieldU128::ones(), MyFieldU128(u128::MAX));
1970        }
1971        ones_usize => {
1972            assert_eq!(MyFieldUsize::ones(), MyFieldUsize(usize::MAX));
1973        }
1974    }
1975
1976    // zeros
1977    tests! {
1978        zeros_u8 => {
1979            assert_eq!(MyFieldU8::zeros(), MyFieldU8(0));
1980        }
1981        zeros_u16 => {
1982            assert_eq!(MyFieldU16::zeros(), MyFieldU16(0));
1983        }
1984        zeros_u32 => {
1985            assert_eq!(MyFieldU32::zeros(), MyFieldU32(0));
1986        }
1987        zeros_u64 => {
1988            assert_eq!(MyFieldU64::zeros(), MyFieldU64(0));
1989        }
1990        zeros_u128 => {
1991            assert_eq!(MyFieldU128::zeros(), MyFieldU128(0));
1992        }
1993        zeros_usize => {
1994            assert_eq!(MyFieldUsize::zeros(), MyFieldUsize(0));
1995        }
1996    }
1997
1998    // defaults
1999    tests! {
2000        default_u8 => {
2001            assert_eq!(0, MyFieldU8::default().as_integer())
2002        }
2003        default_u16 => {
2004            assert_eq!(0, MyFieldU16::default().as_integer())
2005        }
2006        default_u32 => {
2007            assert_eq!(0, MyFieldU32::default().as_integer())
2008        }
2009        default_u64 => {
2010            assert_eq!(0, MyFieldU64::default().as_integer())
2011        }
2012        default_u128 => {
2013            assert_eq!(0, MyFieldU128::default().as_integer())
2014        }
2015        default_usize => {
2016            assert_eq!(0, MyFieldUsize::default().as_integer())
2017        }
2018    }
2019
2020    // bit count
2021    tests! {
2022        bit_count_u8 => {
2023            let field = MyFieldU8(U8);
2024            assert_eq!(u8::BITS as usize, MyFieldU8::BITS)
2025        }
2026        bit_count_u16 => {
2027            let field = MyFieldU16(U16);
2028            assert_eq!(u16::BITS as usize, MyFieldU16::BITS)
2029        }
2030        bit_count_u32 => {
2031            let field = MyFieldU32(U32);
2032            assert_eq!(u32::BITS as usize, MyFieldU32::BITS)
2033        }
2034        bit_count_u64 => {
2035            let field = MyFieldU64(U64);
2036            assert_eq!(u64::BITS as usize, MyFieldU64::BITS)
2037        }
2038        bit_count_u128 => {
2039            let field = MyFieldU128(U128);
2040            assert_eq!(u128::BITS as usize, MyFieldU128::BITS)
2041        }
2042        bit_count_usize => {
2043            let field = MyFieldUsize(USIZE);
2044            assert_eq!(usize::BITS as usize, MyFieldUsize::BITS)
2045        }
2046    }
2047
2048    // byte count
2049    tests! {
2050        byte_count_u8 => {
2051            let field = MyFieldU8(U8);
2052            assert_eq!(1, MyFieldU8::BYTES)
2053        }
2054        byte_count_u16 => {
2055            let field = MyFieldU16(U16);
2056            assert_eq!(2, MyFieldU16::BYTES)
2057        }
2058        byte_count_u32 => {
2059            let field = MyFieldU32(U32);
2060            assert_eq!(4, MyFieldU32::BYTES)
2061        }
2062        byte_count_u64 => {
2063            let field = MyFieldU64(U64);
2064            assert_eq!(8, MyFieldU64::BYTES)
2065        }
2066        byte_count_u128 => {
2067            let field = MyFieldU128(U128);
2068            assert_eq!(16, MyFieldU128::BYTES)
2069        }
2070        byte_count_usize => {
2071            let field = MyFieldUsize(USIZE);
2072            assert_eq!((usize::BITS as usize) / 8, MyFieldUsize::BYTES)
2073        }
2074    }
2075
2076    // integer value
2077    tests! {
2078        integer_u8 => {
2079            let field = MyFieldU8(U8);
2080            assert_eq!(U8, field.as_integer())
2081        }
2082        integer_u16 => {
2083            let field = MyFieldU16(U16);
2084            assert_eq!(U16, field.as_integer())
2085        }
2086        integer_u32 => {
2087            let field = MyFieldU32(U32);
2088            assert_eq!(U32, field.as_integer())
2089        }
2090        integer_u64 => {
2091            let field = MyFieldU64(U64);
2092            assert_eq!(U64, field.as_integer())
2093        }
2094        integer_u128 => {
2095            let field = MyFieldU128(U128);
2096            assert_eq!(U128, field.as_integer())
2097        }
2098        integer_usize => {
2099            let field = MyFieldUsize(USIZE);
2100            assert_eq!(USIZE, field.as_integer())
2101        }
2102    }
2103
2104    // binary value
2105    tests! {
2106        binary_u8 => {
2107            let field = MyFieldU8(U8);
2108            assert_eq!(B8, field.as_binary())
2109        }
2110        binary_u16 => {
2111            let field = MyFieldU16(U16);
2112            assert_eq!(B16, field.as_binary())
2113        }
2114        binary_u32 => {
2115            let field = MyFieldU32(U32);
2116            assert_eq!(B32, field.as_binary())
2117        }
2118        binary_u64 => {
2119            let field = MyFieldU64(U64);
2120            assert_eq!(B64, field.as_binary())
2121        }
2122        binary_u128 => {
2123            let field = MyFieldU128(U128);
2124            assert_eq!(B128, field.as_binary())
2125        }
2126        binary_usize => {
2127            let field = MyFieldUsize(USIZE);
2128            assert_eq!(BSIZE, field.as_binary())
2129        }
2130    }
2131
2132    // get index
2133    tests! {
2134        get_index_u8 => {
2135            let field = MyFieldU8(U8);
2136            for (index, char) in B8.chars().rev().enumerate() {
2137                if char == '1' {
2138                    assert!(field.get_index(index as u8))
2139                } else if char == '0' {
2140                    assert!(!field.get_index(index as u8))
2141                } else {
2142                    panic!("invalid char in binary string")
2143                }
2144            }
2145        }
2146        get_index_u16 => {
2147            let field = MyFieldU16(U16);
2148            for (index, char) in B16.chars().rev().enumerate() {
2149                if char == '1' {
2150                    assert!(field.get_index(index as u8))
2151                } else if char == '0' {
2152                    assert!(!field.get_index(index as u8))
2153                } else {
2154                    panic!("invalid char in binary string")
2155                }
2156            }
2157        }
2158        get_index_u32 => {
2159            let field = MyFieldU32(U32);
2160            for (index, char) in B32.chars().rev().enumerate() {
2161                if char == '1' {
2162                    assert!(field.get_index(index as u8))
2163                } else if char == '0' {
2164                    assert!(!field.get_index(index as u8))
2165                } else {
2166                    panic!("invalid char in binary string")
2167                }
2168            }
2169        }
2170        get_index_u64 => {
2171            let field = MyFieldU64(U64);
2172            for (index, char) in B64.chars().rev().enumerate() {
2173                if char == '1' {
2174                    assert!(field.get_index(index as u8))
2175                } else if char == '0' {
2176                    assert!(!field.get_index(index as u8))
2177                } else {
2178                    panic!("invalid char in binary string")
2179                }
2180            }
2181        }
2182        get_index_u128 => {
2183            let field = MyFieldU128(U128);
2184            for (index, char) in B128.chars().rev().enumerate() {
2185                if char == '1' {
2186                    assert!(field.get_index(index as u8))
2187                } else if char == '0' {
2188                    assert!(!field.get_index(index as u8))
2189                } else {
2190                    panic!("invalid char in binary string")
2191                }
2192            }
2193        }
2194        get_index_usize => {
2195            let field = MyFieldUsize(USIZE);
2196            for (index, char) in BSIZE.chars().rev().enumerate() {
2197                if char == '1' {
2198                    assert!(field.get_index(index as u8))
2199                } else if char == '0' {
2200                    assert!(!field.get_index(index as u8))
2201                } else {
2202                    panic!("invalid char in binary string")
2203                }
2204            }
2205        }
2206    }
2207
2208    // set index
2209    tests! {
2210        set_index_u8 => {
2211            let mut field = MyFieldU8(0);
2212            for (index, char) in B8.chars().rev().enumerate() {
2213                field.set_index(index as u8);
2214                for (s_index, _char) in B8.chars().rev().enumerate() {
2215                    if s_index <= index {
2216                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2217                    }  else {
2218                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2219                    }
2220                }
2221            }
2222        }
2223        set_index_u16 => {
2224            let mut field = MyFieldU16(0);
2225            for (index, char) in B16.chars().rev().enumerate() {
2226                field.set_index(index as u8);
2227                for (s_index, _char) in B16.chars().rev().enumerate() {
2228                    if s_index <= index {
2229                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2230                    }  else {
2231                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2232                    }
2233                }
2234            }
2235        }
2236        set_index_u32 => {
2237            let mut field = MyFieldU32(0);
2238            for (index, char) in B32.chars().rev().enumerate() {
2239                field.set_index(index as u8);
2240                for (s_index, _char) in B32.chars().rev().enumerate() {
2241                    if s_index <= index {
2242                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2243                    }  else {
2244                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2245                    }
2246                }
2247            }
2248        }
2249        set_index_u64 => {
2250            let mut field = MyFieldU64(0);
2251            for (index, char) in B64.chars().rev().enumerate() {
2252                field.set_index(index as u8);
2253                for (s_index, _char) in B64.chars().rev().enumerate() {
2254                    if s_index <= index {
2255                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2256                    }  else {
2257                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2258                    }
2259                }
2260            }
2261        }
2262        set_index_u128 => {
2263            let mut field = MyFieldU128(0);
2264            for (index, char) in B128.chars().rev().enumerate() {
2265                field.set_index(index as u8);
2266                for (s_index, _char) in B128.chars().rev().enumerate() {
2267                    if s_index <= index {
2268                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2269                    }  else {
2270                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2271                    }
2272                }
2273            }
2274        }
2275        set_index_usize => {
2276            let mut field = MyFieldUsize(0);
2277            for (index, char) in BSIZE.chars().rev().enumerate() {
2278                field.set_index(index as u8);
2279                for (s_index, _char) in BSIZE.chars().rev().enumerate() {
2280                    if s_index <= index {
2281                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2282                    }  else {
2283                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2284                    }
2285                }
2286            }
2287        }
2288    }
2289
2290    // clear index
2291    tests! {
2292        clear_index_u8 => {
2293            let mut field = MyFieldU8(u8::MAX);
2294            for (index, char) in B8.chars().rev().enumerate() {
2295                assert!(field.get_index(index as u8), "{:#?}", field);
2296                field.clear_index(index as u8);
2297                for (s_index, _char) in B8.chars().rev().enumerate() {
2298                    if s_index <= index {
2299                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2300                    }  else {
2301                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2302                    }
2303                }
2304            }
2305        }
2306        clear_index_u16 => {
2307            let mut field = MyFieldU16(u16::MAX);
2308            for (index, char) in B16.chars().rev().enumerate() {
2309                assert!(field.get_index(index as u8), "{:#?}", field);
2310                field.clear_index(index as u8);
2311                for (s_index, _char) in B16.chars().rev().enumerate() {
2312                    if s_index <= index {
2313                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2314                    }  else {
2315                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2316                    }
2317                }
2318            }
2319        }
2320        clear_index_u32 => {
2321            let mut field = MyFieldU32(u32::MAX);
2322            for (index, char) in B32.chars().rev().enumerate() {
2323                assert!(field.get_index(index as u8), "{:#?}", field);
2324                field.clear_index(index as u8);
2325                for (s_index, _char) in B32.chars().rev().enumerate() {
2326                    if s_index <= index {
2327                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2328                    }  else {
2329                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2330                    }
2331                }
2332            }
2333        }
2334        clear_index_u64 => {
2335            let mut field = MyFieldU64(u64::MAX);
2336            for (index, char) in B64.chars().rev().enumerate() {
2337                assert!(field.get_index(index as u8), "{:#?}", field);
2338                field.clear_index(index as u8);
2339                for (s_index, _char) in B64.chars().rev().enumerate() {
2340                    if s_index <= index {
2341                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2342                    }  else {
2343                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2344                    }
2345                }
2346            }
2347        }
2348        clear_index_u128 => {
2349            let mut field = MyFieldU128(u128::MAX);
2350            for (index, char) in B128.chars().rev().enumerate() {
2351                assert!(field.get_index(index as u8), "{:#?}", field);
2352                field.clear_index(index as u8);
2353                for (s_index, _char) in B128.chars().rev().enumerate() {
2354                    if s_index <= index {
2355                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2356                    }  else {
2357                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2358                    }
2359                }
2360            }
2361        }
2362        clear_index_usize => {
2363            let mut field = MyFieldUsize(usize::MAX);
2364            for (index, char) in BSIZE.chars().rev().enumerate() {
2365                assert!(field.get_index(index as u8), "{:#?}", field);
2366                field.clear_index(index as u8);
2367                for (s_index, _char) in BSIZE.chars().rev().enumerate() {
2368                    if s_index <= index {
2369                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2370                    }  else {
2371                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2372                    }
2373                }
2374            }
2375        }
2376    }
2377
2378    // toggle index
2379    tests! {
2380        toggle_index_u8 => {
2381            let mut field = MyFieldU8(U8);
2382            for (index, char) in B8.chars().rev().enumerate() {
2383                if char == '1' {
2384                    assert!(field.get_index(index as u8));
2385                    field.toggle_index(index as u8);
2386                    assert!(!field.get_index(index as u8));
2387                } else if char == '0' {
2388                    assert!(!field.get_index(index as u8));
2389                    field.toggle_index(index as u8);
2390                    assert!(field.get_index(index as u8));
2391                } else {
2392                    panic!("invalid char in binary string");
2393                }
2394            }
2395        }
2396        toggle_index_u16 => {
2397            let mut field = MyFieldU16(U16);
2398            for (index, char) in B16.chars().rev().enumerate() {
2399                if char == '1' {
2400                    assert!(field.get_index(index as u8));
2401                    field.toggle_index(index as u8);
2402                    assert!(!field.get_index(index as u8));
2403                } else if char == '0' {
2404                    assert!(!field.get_index(index as u8));
2405                    field.toggle_index(index as u8);
2406                    assert!(field.get_index(index as u8));
2407                } else {
2408                    panic!("invalid char in binary string")
2409                }
2410            }
2411        }
2412        toggle_index_u32 => {
2413            let mut field = MyFieldU32(U32);
2414            for (index, char) in B32.chars().rev().enumerate() {
2415                if char == '1' {
2416                    assert!(field.get_index(index as u8));
2417                    field.toggle_index(index as u8);
2418                    assert!(!field.get_index(index as u8));
2419                } else if char == '0' {
2420                    assert!(!field.get_index(index as u8));
2421                    field.toggle_index(index as u8);
2422                    assert!(field.get_index(index as u8));
2423                } else {
2424                    panic!("invalid char in binary string")
2425                }
2426            }
2427        }
2428        toggle_index_u64 => {
2429            let mut field = MyFieldU64(U64);
2430            for (index, char) in B64.chars().rev().enumerate() {
2431                if char == '1' {
2432                    assert!(field.get_index(index as u8));
2433                    field.toggle_index(index as u8);
2434                    assert!(!field.get_index(index as u8));
2435                } else if char == '0' {
2436                    assert!(!field.get_index(index as u8));
2437                    field.toggle_index(index as u8);
2438                    assert!(field.get_index(index as u8));
2439                } else {
2440                    panic!("invalid char in binary string")
2441                }
2442            }
2443        }
2444        toggle_index_u128 => {
2445            let mut field = MyFieldU128(U128);
2446            for (index, char) in B128.chars().rev().enumerate() {
2447                if char == '1' {
2448                    assert!(field.get_index(index as u8));
2449                    field.toggle_index(index as u8);
2450                    assert!(!field.get_index(index as u8));
2451                } else if char == '0' {
2452                    assert!(!field.get_index(index as u8));
2453                    field.toggle_index(index as u8);
2454                    assert!(field.get_index(index as u8));
2455                } else {
2456                    panic!("invalid char in binary string")
2457                }
2458            }
2459        }
2460        toggle_index_usize => {
2461            let mut field = MyFieldUsize(USIZE);
2462            for (index, char) in BSIZE.chars().rev().enumerate() {
2463                if char == '1' {
2464                    assert!(field.get_index(index as u8));
2465                    field.toggle_index(index as u8);
2466                    assert!(!field.get_index(index as u8));
2467                } else if char == '0' {
2468                    assert!(!field.get_index(index as u8));
2469                    field.toggle_index(index as u8);
2470                    assert!(field.get_index(index as u8));
2471                } else {
2472                    panic!("invalid char in binary string")
2473                }
2474            }
2475        }
2476    }
2477
2478    // get
2479    tests! {
2480        get_u8 => {
2481            let field = MyFieldU8(U8);
2482            for (index, char) in B8.chars().rev().enumerate() {
2483                if char == '1' {
2484                    assert!(field.get(MyFlagsU8::from(index as u8)))
2485                } else if char == '0' {
2486                    assert!(!field.get(MyFlagsU8::from(index as u8)))
2487                } else {
2488                    panic!("invalid char in binary string")
2489                }
2490            }
2491        }
2492        get_u16 => {
2493            let field = MyFieldU16(U16);
2494            for (index, char) in B16.chars().rev().enumerate() {
2495                if char == '1' {
2496                    assert!(field.get(MyFlagsU16::from(index as u8)))
2497                } else if char == '0' {
2498                    assert!(!field.get(MyFlagsU16::from(index as u8)))
2499                } else {
2500                    panic!("invalid char in binary string")
2501                }
2502            }
2503        }
2504        get_u32 => {
2505            let field = MyFieldU32(U32);
2506            for (index, char) in B32.chars().rev().enumerate() {
2507                if char == '1' {
2508                    assert!(field.get(MyFlagsU32::from(index as u8)))
2509                } else if char == '0' {
2510                    assert!(!field.get(MyFlagsU32::from(index as u8)))
2511                } else {
2512                    panic!("invalid char in binary string")
2513                }
2514            }
2515        }
2516        get_u64 => {
2517            let field = MyFieldU64(U64);
2518            for (index, char) in B64.chars().rev().enumerate() {
2519                if char == '1' {
2520                    assert!(field.get(MyFlagsU64::from(index as u8)))
2521                } else if char == '0' {
2522                    assert!(!field.get(MyFlagsU64::from(index as u8)))
2523                } else {
2524                    panic!("invalid char in binary string")
2525                }
2526            }
2527        }
2528        get_u128 => {
2529            let field = MyFieldU128(U128);
2530            for (index, char) in B128.chars().rev().enumerate() {
2531                if char == '1' {
2532                    assert!(field.get(MyFlagsU128::from(index as u8)))
2533                } else if char == '0' {
2534                    assert!(!field.get(MyFlagsU128::from(index as u8)))
2535                } else {
2536                    panic!("invalid char in binary string")
2537                }
2538            }
2539        }
2540        get_usize => {
2541            let field = MyFieldUsize(USIZE);
2542            for (index, char) in BSIZE.chars().rev().enumerate() {
2543                if char == '1' {
2544                    assert!(field.get(MyFlagsUsize::from(index as u8)))
2545                } else if char == '0' {
2546                    assert!(!field.get(MyFlagsUsize::from(index as u8)))
2547                } else {
2548                    panic!("invalid char in binary string")
2549                }
2550            }
2551        }
2552    }
2553
2554    // set
2555    tests! {
2556        set_u8 => {
2557            let mut field = MyFieldU8(0);
2558            for (index, char) in B8.chars().rev().enumerate() {
2559                field.set(MyFlagsU8::from(index as u8));
2560                for (s_index, _char) in B8.chars().rev().enumerate() {
2561                    if s_index <= index {
2562                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2563                    }  else {
2564                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2565                    }
2566                }
2567            }
2568        }
2569        set_u16 => {
2570            let mut field = MyFieldU16(0);
2571            for (index, char) in B16.chars().rev().enumerate() {
2572                field.set(MyFlagsU16::from(index as u8));
2573                for (s_index, _char) in B16.chars().rev().enumerate() {
2574                    if s_index <= index {
2575                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2576                    }  else {
2577                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2578                    }
2579                }
2580            }
2581        }
2582        set_u32 => {
2583            let mut field = MyFieldU32(0);
2584            for (index, char) in B32.chars().rev().enumerate() {
2585                field.set(MyFlagsU32::from(index as u8));
2586                for (s_index, _char) in B32.chars().rev().enumerate() {
2587                    if s_index <= index {
2588                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2589                    }  else {
2590                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2591                    }
2592                }
2593            }
2594        }
2595        set_u64 => {
2596            let mut field = MyFieldU64(0);
2597            for (index, char) in B64.chars().rev().enumerate() {
2598                field.set(MyFlagsU64::from(index as u8));
2599                for (s_index, _char) in B64.chars().rev().enumerate() {
2600                    if s_index <= index {
2601                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2602                    }  else {
2603                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2604                    }
2605                }
2606            }
2607        }
2608        set_u128 => {
2609            let mut field = MyFieldU128(0);
2610            for (index, char) in B128.chars().rev().enumerate() {
2611                field.set(MyFlagsU128::from(index as u8));
2612                for (s_index, _char) in B128.chars().rev().enumerate() {
2613                    if s_index <= index {
2614                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2615                    }  else {
2616                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2617                    }
2618                }
2619            }
2620        }
2621        set_usize => {
2622            let mut field = MyFieldUsize(0);
2623            for (index, char) in BSIZE.chars().rev().enumerate() {
2624                field.set(MyFlagsUsize::from(index as u8));
2625                for (s_index, _char) in BSIZE.chars().rev().enumerate() {
2626                    if s_index <= index {
2627                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2628                    }  else {
2629                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2630                    }
2631                }
2632            }
2633        }
2634    }
2635
2636    // clear
2637    tests! {
2638        clear_u8 => {
2639            let mut field = MyFieldU8(u8::MAX);
2640            for (index, char) in B8.chars().rev().enumerate() {
2641                assert!(field.get_index(index as u8), "{:#?}", field);
2642                field.clear(MyFlagsU8::from(index as u8));
2643                for (s_index, _char) in B8.chars().rev().enumerate() {
2644                    if s_index <= index {
2645                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2646                    }  else {
2647                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2648                    }
2649                }
2650            }
2651        }
2652        clear_u16 => {
2653            let mut field = MyFieldU16(u16::MAX);
2654            for (index, char) in B16.chars().rev().enumerate() {
2655                assert!(field.get_index(index as u8), "{:#?}", field);
2656                field.clear(MyFlagsU16::from(index as u8));
2657                for (s_index, _char) in B16.chars().rev().enumerate() {
2658                    if s_index <= index {
2659                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2660                    }  else {
2661                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2662                    }
2663                }
2664            }
2665        }
2666        clear_u32 => {
2667            let mut field = MyFieldU32(u32::MAX);
2668            for (index, char) in B32.chars().rev().enumerate() {
2669                assert!(field.get_index(index as u8), "{:#?}", field);
2670                field.clear(MyFlagsU32::from(index as u8));
2671                for (s_index, _char) in B32.chars().rev().enumerate() {
2672                    if s_index <= index {
2673                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2674                    }  else {
2675                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2676                    }
2677                }
2678            }
2679        }
2680        clear_u64 => {
2681            let mut field = MyFieldU64(u64::MAX);
2682            for (index, char) in B64.chars().rev().enumerate() {
2683                assert!(field.get_index(index as u8), "{:#?}", field);
2684                field.clear(MyFlagsU64::from(index as u8));
2685                for (s_index, _char) in B64.chars().rev().enumerate() {
2686                    if s_index <= index {
2687                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2688                    }  else {
2689                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2690                    }
2691                }
2692            }
2693        }
2694        clear_u128 => {
2695            let mut field = MyFieldU128(u128::MAX);
2696            for (index, char) in B128.chars().rev().enumerate() {
2697                assert!(field.get_index(index as u8), "{:#?}", field);
2698                field.clear(MyFlagsU128::from(index as u8));
2699                for (s_index, _char) in B128.chars().rev().enumerate() {
2700                    if s_index <= index {
2701                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2702                    }  else {
2703                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2704                    }
2705                }
2706            }
2707        }
2708        clear_usize => {
2709            let mut field = MyFieldUsize(usize::MAX);
2710            for (index, char) in BSIZE.chars().rev().enumerate() {
2711                assert!(field.get_index(index as u8), "{:#?}", field);
2712                field.clear(MyFlagsUsize::from(index as u8));
2713                for (s_index, _char) in BSIZE.chars().rev().enumerate() {
2714                    if s_index <= index {
2715                        assert!(!field.get_index(s_index as u8), "{:#?}", field);
2716                    }  else {
2717                        assert!(field.get_index(s_index as u8), "{:#?}", field);
2718                    }
2719                }
2720            }
2721        }
2722    }
2723
2724    // toggle
2725    tests! {
2726        toggle_u8 => {
2727            let mut field = MyFieldU8(U8);
2728            for (index, char) in B8.chars().rev().enumerate() {
2729                if char == '1' {
2730                    assert!(field.get_index(index as u8));
2731                    field.toggle(MyFlagsU8::from(index as u8));
2732                    assert!(!field.get_index(index as u8));
2733                } else if char == '0' {
2734                    assert!(!field.get_index(index as u8));
2735                    field.toggle(MyFlagsU8::from(index as u8));
2736                    assert!(field.get_index(index as u8));
2737                } else {
2738                    panic!("invalid char in binary string");
2739                }
2740            }
2741        }
2742        toggle_u16 => {
2743            let mut field = MyFieldU16(U16);
2744            for (index, char) in B16.chars().rev().enumerate() {
2745                if char == '1' {
2746                    assert!(field.get_index(index as u8));
2747                    field.toggle(MyFlagsU16::from(index as u8));
2748                    assert!(!field.get_index(index as u8));
2749                } else if char == '0' {
2750                    assert!(!field.get_index(index as u8));
2751                    field.toggle(MyFlagsU16::from(index as u8));
2752                    assert!(field.get_index(index as u8));
2753                } else {
2754                    panic!("invalid char in binary string")
2755                }
2756            }
2757        }
2758        toggle_u32 => {
2759            let mut field = MyFieldU32(U32);
2760            for (index, char) in B32.chars().rev().enumerate() {
2761                if char == '1' {
2762                    assert!(field.get_index(index as u8));
2763                    field.toggle(MyFlagsU32::from(index as u8));
2764                    assert!(!field.get_index(index as u8));
2765                } else if char == '0' {
2766                    assert!(!field.get_index(index as u8));
2767                    field.toggle(MyFlagsU32::from(index as u8));
2768                    assert!(field.get_index(index as u8));
2769                } else {
2770                    panic!("invalid char in binary string")
2771                }
2772            }
2773        }
2774        toggle_u64 => {
2775            let mut field = MyFieldU64(U64);
2776            for (index, char) in B64.chars().rev().enumerate() {
2777                if char == '1' {
2778                    assert!(field.get_index(index as u8));
2779                    field.toggle(MyFlagsU64::from(index as u8));
2780                    assert!(!field.get_index(index as u8));
2781                } else if char == '0' {
2782                    assert!(!field.get_index(index as u8));
2783                    field.toggle(MyFlagsU64::from(index as u8));
2784                    assert!(field.get_index(index as u8));
2785                } else {
2786                    panic!("invalid char in binary string")
2787                }
2788            }
2789        }
2790        toggle_u128 => {
2791            let mut field = MyFieldU128(U128);
2792            for (index, char) in B128.chars().rev().enumerate() {
2793                if char == '1' {
2794                    assert!(field.get_index(index as u8));
2795                    field.toggle(MyFlagsU128::from(index as u8));
2796                    assert!(!field.get_index(index as u8));
2797                } else if char == '0' {
2798                    assert!(!field.get_index(index as u8));
2799                    field.toggle(MyFlagsU128::from(index as u8));
2800                    assert!(field.get_index(index as u8));
2801                } else {
2802                    panic!("invalid char in binary string")
2803                }
2804            }
2805        }
2806        toggle_usize => {
2807            let mut field = MyFieldUsize(USIZE);
2808            for (index, char) in BSIZE.chars().rev().enumerate() {
2809                if char == '1' {
2810                    assert!(field.get_index(index as u8));
2811                    field.toggle(MyFlagsUsize::from(index as u8));
2812                    assert!(!field.get_index(index as u8));
2813                } else if char == '0' {
2814                    assert!(!field.get_index(index as u8));
2815                    field.toggle(MyFlagsUsize::from(index as u8));
2816                    assert!(field.get_index(index as u8));
2817                } else {
2818                    panic!("invalid char in binary string")
2819                }
2820            }
2821        }
2822    }
2823
2824    // diff
2825    tests! {
2826        diff_u8 => {
2827            let field = MyFieldU8(U8);
2828            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
2829            let out = field.diff(reversed);
2830            assert_eq!("10100101", out.as_binary());
2831            assert_eq!(165, out.as_integer());
2832        }
2833        diff_u16 => {
2834            let field = MyFieldU16(U16);
2835            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
2836            let out = field.diff(reversed);
2837            assert_eq!("1010101111010101", out.as_binary());
2838            assert_eq!(43989, out.as_integer());
2839        }
2840        diff_u32 => {
2841            let field = MyFieldU32(U32);
2842            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
2843            let out = field.diff(reversed);
2844            assert_eq!("11110101000100111100100010101111", out.as_binary());
2845            assert_eq!(4111714479, out.as_integer());
2846        }
2847        diff_u64 => {
2848            let field = MyFieldU64(U64);
2849            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
2850            let out = field.diff(reversed);
2851            assert_eq!("0001010011000010001001001001100110011001001001000100001100101000", out.as_binary());
2852            assert_eq!(1495798268358312744, out.as_integer());
2853        }
2854        diff_u128 => {
2855            let field = MyFieldU128(U128);
2856            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
2857            let out = field.diff(reversed);
2858            assert_eq!("10010001110011001000001110100101110101111000111101001010101000100100010101010010111100011110101110100101110000010011001110001001", out.as_binary());
2859            assert_eq!(193799958082971305581995159990093034377, out.as_integer());
2860        }
2861        // diff_usize => {
2862        //     let field = MyFieldUsize(USIZE);
2863        // }
2864    }
2865
2866    // combine
2867    tests! {
2868        combine_u8 => {
2869            let field = MyFieldU8(U8);
2870            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
2871            let out = field.combine(reversed);
2872            assert_eq!("11111111", out.as_binary());
2873            assert_eq!(255, out.as_integer());
2874        }
2875        combine_u16 => {
2876            let field = MyFieldU16(U16);
2877            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
2878            let out = field.combine(reversed);
2879            assert_eq!("1110101111010111", out.as_binary());
2880            assert_eq!(60375, out.as_integer());
2881        }
2882        combine_u32 => {
2883            let field = MyFieldU32(U32);
2884            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
2885            let out = field.combine(reversed);
2886            assert_eq!("11111101100100111100100110111111", out.as_binary());
2887            assert_eq!(4254321087, out.as_integer());
2888        }
2889        combine_u64 => {
2890            let field = MyFieldU64(U64);
2891            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
2892            let out = field.combine(reversed);
2893            assert_eq!("1111111011010010001111111001110110111001111111000100101101111111", out.as_binary());
2894            assert_eq!(18361808577405668223, out.as_integer());
2895        }
2896        combine_u128 => {
2897            let field = MyFieldU128(U128);
2898            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
2899            let out = field.combine(reversed);
2900            assert_eq!("11111001111111011110001110100101111101111110111101101011111111111111111111010110111101111110111110100101110001111011111110011111", out.as_binary());
2901            assert_eq!(332296039312012448238196841470979719071, out.as_integer());
2902        }
2903        // combine_usize => {
2904        //     let field = MyFieldUsize(USIZE);
2905        // }
2906    }
2907
2908    // intersect
2909    tests! {
2910        intersect_u8 => {
2911            let field = MyFieldU8(U8);
2912            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
2913            let out = field.intersect(reversed);
2914            assert_eq!("01011010", out.as_binary());
2915            assert_eq!(90, out.as_integer());
2916        }
2917        intersect_u16 => {
2918            let field = MyFieldU16(U16);
2919            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
2920            let out = field.intersect(reversed);
2921            assert_eq!("0100000000000010", out.as_binary());
2922            assert_eq!(16386, out.as_integer());
2923        }
2924        intersect_u32 => {
2925            let field = MyFieldU32(U32);
2926            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
2927            let out = field.intersect(reversed);
2928            assert_eq!("00001000100000000000000100010000", out.as_binary());
2929            assert_eq!(142606608, out.as_integer());
2930        }
2931        intersect_u64 => {
2932            let field = MyFieldU64(U64);
2933            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
2934            let out = field.intersect(reversed);
2935            assert_eq!("1110101000010000000110110000010000100000110110000000100001010111", out.as_binary());
2936            assert_eq!(16866010309047355479, out.as_integer());
2937        }
2938        intersect_u128 => {
2939            let field = MyFieldU128(U128);
2940            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
2941            let out = field.intersect(reversed);
2942            assert_eq!("01101000001100010110000000000000001000000110000000100001010111011011101010000100000001100000010000000000000001101000110000010110", out.as_binary());
2943            assert_eq!(138496081229041142656201681480886684694, out.as_integer());
2944        }
2945        // intersect_usize => {
2946        //     let field = MyFieldUsize(USIZE);
2947        // }
2948    }
2949
2950    // into diff
2951    tests! {
2952        into_diff_u8 => {
2953            let field = MyFieldU8(U8);
2954            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
2955            let out = field.into_diff(reversed);
2956            assert_eq!("10100101", out.as_binary());
2957            assert_eq!(165, out.as_integer());
2958        }
2959        into_diff_u16 => {
2960            let field = MyFieldU16(U16);
2961            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
2962            let out = field.into_diff(reversed);
2963            assert_eq!("1010101111010101", out.as_binary());
2964            assert_eq!(43989, out.as_integer());
2965        }
2966        into_diff_u32 => {
2967            let field = MyFieldU32(U32);
2968            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
2969            let out = field.into_diff(reversed);
2970            assert_eq!("11110101000100111100100010101111", out.as_binary());
2971            assert_eq!(4111714479, out.as_integer());
2972        }
2973        into_diff_u64 => {
2974            let field = MyFieldU64(U64);
2975            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
2976            let out = field.into_diff(reversed);
2977            assert_eq!("0001010011000010001001001001100110011001001001000100001100101000", out.as_binary());
2978            assert_eq!(1495798268358312744, out.as_integer());
2979        }
2980        into_diff_u128 => {
2981            let field = MyFieldU128(U128);
2982            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
2983            let out = field.into_diff(reversed);
2984            assert_eq!("10010001110011001000001110100101110101111000111101001010101000100100010101010010111100011110101110100101110000010011001110001001", out.as_binary());
2985            assert_eq!(193799958082971305581995159990093034377, out.as_integer());
2986        }
2987        // into_diff_usize => {
2988        //     let field = MyFieldUsize(USIZE);
2989        // }
2990    }
2991
2992    // into combined
2993    tests! {
2994        into_combined_u8 => {
2995            let field = MyFieldU8(U8);
2996            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
2997            let out = field.into_combined(reversed);
2998            assert_eq!("11111111", out.as_binary());
2999            assert_eq!(255, out.as_integer());
3000        }
3001        into_combined_u16 => {
3002            let field = MyFieldU16(U16);
3003            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3004            let out = field.into_combined(reversed);
3005            assert_eq!("1110101111010111", out.as_binary());
3006            assert_eq!(60375, out.as_integer());
3007        }
3008        into_combined_u32 => {
3009            let field = MyFieldU32(U32);
3010            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3011            let out = field.into_combined(reversed);
3012            assert_eq!("11111101100100111100100110111111", out.as_binary());
3013            assert_eq!(4254321087, out.as_integer());
3014        }
3015        into_combined_u64 => {
3016            let field = MyFieldU64(U64);
3017            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3018            let out = field.into_combined(reversed);
3019            assert_eq!("1111111011010010001111111001110110111001111111000100101101111111", out.as_binary());
3020            assert_eq!(18361808577405668223, out.as_integer());
3021        }
3022        into_combined_u128 => {
3023            let field = MyFieldU128(U128);
3024            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3025            let out = field.into_combined(reversed);
3026            assert_eq!("11111001111111011110001110100101111101111110111101101011111111111111111111010110111101111110111110100101110001111011111110011111", out.as_binary());
3027            assert_eq!(332296039312012448238196841470979719071, out.as_integer());
3028        }
3029        // into_combined_usize => {
3030        //     let field = MyFieldUsize(USIZE);
3031        // }
3032    }
3033
3034    // into intersection
3035    tests! {
3036        into_intersection_u8 => {
3037            let field = MyFieldU8(U8);
3038            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3039            let out = field.into_intersection(reversed);
3040            assert_eq!("01011010", out.as_binary());
3041            assert_eq!(90, out.as_integer());
3042        }
3043        into_intersection_u16 => {
3044            let field = MyFieldU16(U16);
3045            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3046            let out = field.into_intersection(reversed);
3047            assert_eq!("0100000000000010", out.as_binary());
3048            assert_eq!(16386, out.as_integer());
3049        }
3050        into_intersection_u32 => {
3051            let field = MyFieldU32(U32);
3052            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3053            let out = field.into_intersection(reversed);
3054            assert_eq!("00001000100000000000000100010000", out.as_binary());
3055            assert_eq!(142606608, out.as_integer());
3056        }
3057        into_intersection_u64 => {
3058            let field = MyFieldU64(U64);
3059            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3060            let out = field.into_intersection(reversed);
3061            assert_eq!("1110101000010000000110110000010000100000110110000000100001010111", out.as_binary());
3062            assert_eq!(16866010309047355479, out.as_integer());
3063        }
3064        into_intersection_u128 => {
3065            let field = MyFieldU128(U128);
3066            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3067            let out = field.into_intersection(reversed);
3068            assert_eq!("01101000001100010110000000000000001000000110000000100001010111011011101010000100000001100000010000000000000001101000110000010110", out.as_binary());
3069            assert_eq!(138496081229041142656201681480886684694, out.as_integer());
3070        }
3071        // into_intersection_usize => {
3072        //     let field = MyFieldUsize(USIZE);
3073        // }
3074    }
3075
3076    // field from flag
3077    tests! {
3078        field_from_flag_u8 => {
3079            for index in 0..u8::BITS {
3080                let flag = MyFlagsU8::from(index as u8);
3081                let field = MyFieldU8::from(flag);
3082                assert_eq!(MyFieldU8(0u8 | (1 << index)), field)
3083            }
3084        }
3085        field_from_flag_u16 => {
3086            for index in 0..u16::BITS {
3087                let flag = MyFlagsU16::from(index as u8);
3088                let field = MyFieldU16::from(flag);
3089                assert_eq!(MyFieldU16(0u16 | (1 << index)), field)
3090            }
3091        }
3092        field_from_flag_u32 => {
3093            for index in 0..u32::BITS {
3094                let flag = MyFlagsU32::from(index as u8);
3095                let field = MyFieldU32::from(flag);
3096                assert_eq!(MyFieldU32(0u32 | (1 << index)), field)
3097            }
3098        }
3099        field_from_flag_u64 => {
3100            for index in 0..u64::BITS {
3101                let flag = MyFlagsU64::from(index as u8);
3102                let field = MyFieldU64::from(flag);
3103                assert_eq!(MyFieldU64(0u64 | (1 << index)), field)
3104            }
3105        }
3106        field_from_flag_u128 => {
3107            for index in 0..u128::BITS {
3108                let flag = MyFlagsU128::from(index as u8);
3109                let field = MyFieldU128::from(flag);
3110                assert_eq!(MyFieldU128(0u128 | (1 << index)), field)
3111            }
3112        }
3113        field_from_flag_usize => {
3114            for index in 0..usize::BITS {
3115                let flag = MyFlagsUsize::from(index as u8);
3116                let field = MyFieldUsize::from(flag);
3117                assert_eq!(MyFieldUsize(0usize | (1 << index)), field)
3118            }
3119        }
3120    }
3121
3122    // $name & $name
3123    tests! {
3124        bitwise_name_and_name_u8 => {
3125            let field = MyFieldU8(U8);
3126            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3127            let out = field & reversed;
3128            assert_eq!("01011010", out.as_binary());
3129            assert_eq!(90, out.as_integer());
3130        }
3131        bitwise_name_and_name_u16 => {
3132            let field = MyFieldU16(U16);
3133            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3134            let out = field & reversed;
3135            assert_eq!("0100000000000010", out.as_binary());
3136            assert_eq!(16386, out.as_integer());
3137        }
3138        bitwise_name_and_name_u32 => {
3139            let field = MyFieldU32(U32);
3140            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3141            let out = field & reversed;
3142            assert_eq!("00001000100000000000000100010000", out.as_binary());
3143            assert_eq!(142606608, out.as_integer());
3144        }
3145        bitwise_name_and_name_u64 => {
3146            let field = MyFieldU64(U64);
3147            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3148            let out = field & reversed;
3149            assert_eq!("1110101000010000000110110000010000100000110110000000100001010111", out.as_binary());
3150            assert_eq!(16866010309047355479, out.as_integer());
3151        }
3152        bitwise_name_and_name_u128 => {
3153            let field = MyFieldU128(U128);
3154            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3155            let out = field & reversed;
3156            assert_eq!("01101000001100010110000000000000001000000110000000100001010111011011101010000100000001100000010000000000000001101000110000010110", out.as_binary());
3157            assert_eq!(138496081229041142656201681480886684694, out.as_integer());
3158        }
3159        // bitwise_name_and_name_usize => {
3160        //
3161        // }
3162    }
3163
3164    // $name | $name
3165    tests! {
3166        bitwise_name_or_name_u8 => {
3167            let field = MyFieldU8(U8);
3168            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3169            let out = field | reversed;
3170            assert_eq!("11111111", out.as_binary());
3171            assert_eq!(255, out.as_integer());
3172        }
3173        bitwise_name_or_name_u16 => {
3174            let field = MyFieldU16(U16);
3175            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3176            let out = field | reversed;
3177            assert_eq!("1110101111010111", out.as_binary());
3178            assert_eq!(60375, out.as_integer());
3179        }
3180        bitwise_name_or_name_u32 => {
3181            let field = MyFieldU32(U32);
3182            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3183            let out = field | reversed;
3184            assert_eq!("11111101100100111100100110111111", out.as_binary());
3185            assert_eq!(4254321087, out.as_integer());
3186        }
3187        bitwise_name_or_name_u64 => {
3188            let field = MyFieldU64(U64);
3189            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3190            let out = field | reversed;
3191            assert_eq!("1111111011010010001111111001110110111001111111000100101101111111", out.as_binary());
3192            assert_eq!(18361808577405668223, out.as_integer());
3193        }
3194        bitwise_name_or_name_u128 => {
3195            let field = MyFieldU128(U128);
3196            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3197            let out = field | reversed;
3198            assert_eq!("11111001111111011110001110100101111101111110111101101011111111111111111111010110111101111110111110100101110001111011111110011111", out.as_binary());
3199            assert_eq!(332296039312012448238196841470979719071, out.as_integer());
3200        }
3201        // bitwise_name_or_name_usize => {
3202        //
3203        // }
3204    }
3205
3206    // $name ^ $name
3207    tests! {
3208        bitwise_name_xor_name_u8 => {
3209            let field = MyFieldU8(U8);
3210            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3211            let out = field ^ reversed;
3212            assert_eq!("10100101", out.as_binary());
3213            assert_eq!(165, out.as_integer());
3214        }
3215        bitwise_name_xor_name_u16 => {
3216            let field = MyFieldU16(U16);
3217            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3218            let out = field ^ reversed;
3219            assert_eq!("1010101111010101", out.as_binary());
3220            assert_eq!(43989, out.as_integer());
3221        }
3222        bitwise_name_xor_name_u32 => {
3223            let field = MyFieldU32(U32);
3224            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3225            let out = field ^ reversed;
3226            assert_eq!("11110101000100111100100010101111", out.as_binary());
3227            assert_eq!(4111714479, out.as_integer());
3228        }
3229        bitwise_name_xor_name_u64 => {
3230            let field = MyFieldU64(U64);
3231            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3232            let out = field ^ reversed;
3233            assert_eq!("0001010011000010001001001001100110011001001001000100001100101000", out.as_binary());
3234            assert_eq!(1495798268358312744, out.as_integer());
3235        }
3236        bitwise_name_xor_name_u128 => {
3237            let field = MyFieldU128(U128);
3238            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3239            let out = field ^ reversed;
3240            assert_eq!("10010001110011001000001110100101110101111000111101001010101000100100010101010010111100011110101110100101110000010011001110001001", out.as_binary());
3241            assert_eq!(193799958082971305581995159990093034377, out.as_integer());
3242        }
3243        // bitwise_name_xor_name_usize => {
3244        //
3245        // }
3246    }
3247
3248    // $name &= $name
3249    tests! {
3250        bitwise_name_and_assign_name_u8 => {
3251            let mut field = MyFieldU8(U8);
3252            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3253            field &= reversed;
3254            assert_eq!("01011010", field.as_binary());
3255            assert_eq!(90, field.as_integer());
3256        }
3257        bitwise_name_and_assign_name_u16 => {
3258            let mut field = MyFieldU16(U16);
3259            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3260            field &= reversed;
3261            assert_eq!("0100000000000010", field.as_binary());
3262            assert_eq!(16386, field.as_integer());
3263        }
3264        bitwise_name_and_assign_name_u32 => {
3265            let mut field = MyFieldU32(U32);
3266            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3267            field &= reversed;
3268            assert_eq!("00001000100000000000000100010000", field.as_binary());
3269            assert_eq!(142606608, field.as_integer());
3270        }
3271        bitwise_name_and_assign_name_u64 => {
3272            let mut field = MyFieldU64(U64);
3273            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3274            field &= reversed;
3275            assert_eq!("1110101000010000000110110000010000100000110110000000100001010111", field.as_binary());
3276            assert_eq!(16866010309047355479, field.as_integer());
3277        }
3278        bitwise_name_and_assign_name_u128 => {
3279            let mut field = MyFieldU128(U128);
3280            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3281            field &= reversed;
3282            assert_eq!("01101000001100010110000000000000001000000110000000100001010111011011101010000100000001100000010000000000000001101000110000010110", field.as_binary());
3283            assert_eq!(138496081229041142656201681480886684694, field.as_integer());
3284        }
3285    //     bitwise_name_and_assign_name_usize => {
3286    //
3287    //     }
3288    }
3289
3290    // $name |= $name
3291    tests! {
3292        bitwise_name_or_assign_name_u8 => {
3293            let mut field = MyFieldU8(U8);
3294            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3295            field |= reversed;
3296            assert_eq!("11111111", field.as_binary());
3297            assert_eq!(255, field.as_integer());
3298        }
3299        bitwise_name_or_assign_name_u16 => {
3300            let mut field = MyFieldU16(U16);
3301            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3302            field |= reversed;
3303            assert_eq!("1110101111010111", field.as_binary());
3304            assert_eq!(60375, field.as_integer());
3305        }
3306        bitwise_name_or_assign_name_u32 => {
3307            let mut field = MyFieldU32(U32);
3308            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3309            field |= reversed;
3310            assert_eq!("11111101100100111100100110111111", field.as_binary());
3311            assert_eq!(4254321087, field.as_integer());
3312        }
3313        bitwise_name_or_assign_name_u64 => {
3314            let mut field = MyFieldU64(U64);
3315            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3316            field |= reversed;
3317            assert_eq!("1111111011010010001111111001110110111001111111000100101101111111", field.as_binary());
3318            assert_eq!(18361808577405668223, field.as_integer());
3319        }
3320        bitwise_name_or_assign_name_u128 => {
3321            let mut field = MyFieldU128(U128);
3322            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3323            field |= reversed;
3324            assert_eq!("11111001111111011110001110100101111101111110111101101011111111111111111111010110111101111110111110100101110001111011111110011111", field.as_binary());
3325            assert_eq!(332296039312012448238196841470979719071, field.as_integer());
3326        }
3327        // bitwise_name_or_assign_name_usize => {
3328        //
3329        // }
3330    }
3331
3332    // $name ^= $name
3333    tests! {
3334        bitwise_name_xor_assign_name_u8 => {
3335            let mut field = MyFieldU8(U8);
3336            let reversed = MyFieldU8(u8::from_str_radix(B8.chars().rev().collect::<String>().as_str(), 2).unwrap());
3337            field ^= reversed;
3338            assert_eq!("10100101", field.as_binary());
3339            assert_eq!(165, field.as_integer());
3340        }
3341        bitwise_name_xor_assign_name_u16 => {
3342            let mut field = MyFieldU16(U16);
3343            let reversed = MyFieldU16(u16::from_str_radix(B16.chars().rev().collect::<String>().as_str(), 2).unwrap());
3344            field ^= reversed;
3345            assert_eq!("1010101111010101", field.as_binary());
3346            assert_eq!(43989, field.as_integer());
3347        }
3348        bitwise_name_xor_assign_name_u32 => {
3349            let mut field = MyFieldU32(U32);
3350            let reversed = MyFieldU32(u32::from_str_radix(B32.chars().rev().collect::<String>().as_str(), 2).unwrap());
3351            field ^= reversed;
3352            assert_eq!("11110101000100111100100010101111", field.as_binary());
3353            assert_eq!(4111714479, field.as_integer());
3354        }
3355        bitwise_name_xor_assign_name_u64 => {
3356            let mut field = MyFieldU64(U64);
3357            let reversed = MyFieldU64(u64::from_str_radix(B64.chars().rev().collect::<String>().as_str(), 2).unwrap());
3358            field ^= reversed;
3359            assert_eq!("0001010011000010001001001001100110011001001001000100001100101000", field.as_binary());
3360            assert_eq!(1495798268358312744, field.as_integer());
3361        }
3362        bitwise_name_xor_assign_name_u128 => {
3363            let mut field = MyFieldU128(U128);
3364            let reversed = MyFieldU128(u128::from_str_radix(B128.chars().rev().collect::<String>().as_str(), 2).unwrap());
3365            field ^= reversed;
3366            assert_eq!("10010001110011001000001110100101110101111000111101001010101000100100010101010010111100011110101110100101110000010011001110001001", field.as_binary());
3367            assert_eq!(193799958082971305581995159990093034377, field.as_integer());
3368        }
3369        // bitwise_name_xor_assign_name_usize => {
3370        //
3371        // }
3372    }
3373
3374    // $name & $flag
3375    tests! {
3376        bitwise_name_and_flag_u8 => {
3377            for index in 0..u8::BITS {
3378                let mask = MyFieldU8(u8::MAX) & MyFlagsU8::from(index as u8);
3379                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3380            }
3381        }
3382        bitwise_name_and_flag_u16 => {
3383            for index in 0..u16::BITS {
3384                let mask = MyFieldU16(u16::MAX) & MyFlagsU16::from(index as u8);
3385                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3386            }
3387        }
3388        bitwise_name_and_flag_u32 => {
3389            for index in 0..u32::BITS {
3390                let mask = MyFieldU32(u32::MAX) & MyFlagsU32::from(index as u8);
3391                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3392            }
3393        }
3394        bitwise_name_and_flag_u64 => {
3395            for index in 0..u64::BITS {
3396                let mask = MyFieldU64(u64::MAX) & MyFlagsU64::from(index as u8);
3397                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3398            }
3399        }
3400        bitwise_name_and_flag_u128 => {
3401            for index in 0..u128::BITS {
3402                let mask = MyFieldU128(u128::MAX) & MyFlagsU128::from(index as u8);
3403                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3404            }
3405        }
3406        // bitwise_name_and_flag_usize => {
3407        //
3408        // }
3409    }
3410
3411    // $name | $flag
3412    tests! {
3413        bitwise_name_or_flag_u8 => {
3414            for index in 0..u8::BITS {
3415                let mask = MyFieldU8(0) | MyFlagsU8::from(index as u8);
3416                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3417            }
3418        }
3419        bitwise_name_or_flag_u16 => {
3420            for index in 0..u16::BITS {
3421                let mask = MyFieldU16(0) | MyFlagsU16::from(index as u8);
3422                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3423            }
3424        }
3425        bitwise_name_or_flag_u32 => {
3426            for index in 0..u32::BITS {
3427                let mask = MyFieldU32(0) | MyFlagsU32::from(index as u8);
3428                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3429            }
3430        }
3431        bitwise_name_or_flag_u64 => {
3432            for index in 0..u64::BITS {
3433                let mask = MyFieldU64(0) | MyFlagsU64::from(index as u8);
3434                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3435            }
3436        }
3437        bitwise_name_or_flag_u128 => {
3438            for index in 0..u128::BITS {
3439                let mask = MyFieldU128(0) | MyFlagsU128::from(index as u8);
3440                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3441            }
3442        }
3443        // bitwise_name_or_flag_usize => {
3444        //
3445        // }
3446    }
3447
3448    // $name ^ $flag
3449    tests! {
3450        bitwise_name_xor_flag_u8 => {
3451            for index in 0..u8::BITS {
3452                let mask = MyFieldU8(0) ^ MyFlagsU8::from(index as u8);
3453                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3454            }
3455        }
3456        bitwise_name_xor_flag_u16 => {
3457            for index in 0..u16::BITS {
3458                let mask = MyFieldU16(0) ^ MyFlagsU16::from(index as u8);
3459                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3460            }
3461        }
3462        bitwise_name_xor_flag_u32 => {
3463            for index in 0..u32::BITS {
3464                let mask = MyFieldU32(0) ^ MyFlagsU32::from(index as u8);
3465                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3466            }
3467        }
3468        bitwise_name_xor_flag_u64 => {
3469            for index in 0..u64::BITS {
3470                let mask = MyFieldU64(0) ^ MyFlagsU64::from(index as u8);
3471                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3472            }
3473        }
3474        bitwise_name_xor_flag_u128 => {
3475            for index in 0..u128::BITS {
3476                let mask = MyFieldU128(0) ^ MyFlagsU128::from(index as u8);
3477                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3478            }
3479        }
3480        // bitwise_name_xor_flag_usize => {
3481        //
3482        // }
3483    }
3484
3485    // $name &= $flag
3486    tests! {
3487        bitwise_name_and_assign_flag_u8 => {
3488            for index in 0..u8::BITS {
3489                let mut mask = MyFieldU8(u8::MAX);
3490                mask &= MyFlagsU8::from(index as u8);
3491                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3492            }
3493        }
3494        bitwise_name_and_assign_flag_u16 => {
3495            for index in 0..u16::BITS {
3496                let mut mask = MyFieldU16(u16::MAX);
3497                mask &= MyFlagsU16::from(index as u8);
3498                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3499            }
3500        }
3501        bitwise_name_and_assign_flag_u32 => {
3502            for index in 0..u32::BITS {
3503                let mut mask = MyFieldU32(u32::MAX);
3504                mask &= MyFlagsU32::from(index as u8);
3505                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3506            }
3507        }
3508        bitwise_name_and_assign_flag_u64 => {
3509            for index in 0..u64::BITS {
3510                let mut mask = MyFieldU64(u64::MAX);
3511                mask &= MyFlagsU64::from(index as u8);
3512                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3513            }
3514        }
3515        bitwise_name_and_assign_flag_u128 => {
3516            for index in 0..u128::BITS {
3517                let mut mask = MyFieldU128(u128::MAX);
3518                mask &= MyFlagsU128::from(index as u8);
3519                assert_eq!(0 | !(1 << (index as u8) ), mask.as_integer());
3520            }
3521        }
3522        // bitwise_name_and_assign_flag_usize => {
3523        //
3524        // }
3525    }
3526
3527    // $name |= $flag
3528    tests! {
3529        bitwise_name_or_assign_flag_u8 => {
3530            for index in 0..u8::BITS {
3531                let mut mask = MyFieldU8(0);
3532                mask |= MyFlagsU8::from(index as u8);
3533                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3534            }
3535        }
3536        bitwise_name_or_assign_flag_u16 => {
3537            for index in 0..u16::BITS {
3538                let mut mask = MyFieldU16(0);
3539                mask |= MyFlagsU16::from(index as u8);
3540                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3541            }
3542        }
3543        bitwise_name_or_assign_flag_u32 => {
3544            for index in 0..u32::BITS {
3545                let mut mask = MyFieldU32(0);
3546                mask |= MyFlagsU32::from(index as u8);
3547                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3548            }
3549        }
3550        bitwise_name_or_assign_flag_u64 => {
3551            for index in 0..u64::BITS {
3552                let mut mask = MyFieldU64(0);
3553                mask |= MyFlagsU64::from(index as u8);
3554                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3555            }
3556        }
3557        bitwise_name_or_assign_flag_u128 => {
3558            for index in 0..u128::BITS {
3559                let mut mask = MyFieldU128(0);
3560                mask |= MyFlagsU128::from(index as u8);
3561                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3562            }
3563        }
3564        // bitwise_name_or_assign_flag_usize => {
3565        //
3566        // }
3567    }
3568
3569    // $name ^= $flag
3570    tests! {
3571        bitwise_name_xor_assign_flag_u8 => {
3572            for index in 0..u8::BITS {
3573                let mut mask = MyFieldU8(0);
3574                mask ^= MyFlagsU8::from(index as u8);
3575                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3576            }
3577        }
3578        bitwise_name_xor_assign_flag_u16 => {
3579            for index in 0..u16::BITS {
3580                let mut mask = MyFieldU16(0);
3581                mask ^= MyFlagsU16::from(index as u8);
3582                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3583            }
3584        }
3585        bitwise_name_xor_assign_flag_u32 => {
3586            for index in 0..u32::BITS {
3587                let mut mask = MyFieldU32(0);
3588                mask ^= MyFlagsU32::from(index as u8);
3589                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3590            }
3591        }
3592        bitwise_name_xor_assign_flag_u64 => {
3593            for index in 0..u64::BITS {
3594                let mut mask = MyFieldU64(0);
3595                mask ^= MyFlagsU64::from(index as u8);
3596                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3597            }
3598        }
3599        bitwise_name_xor_assign_flag_u128 => {
3600            for index in 0..u128::BITS {
3601                let mut mask = MyFieldU128(0);
3602                mask ^= MyFlagsU128::from(index as u8);
3603                assert_eq!(0 | (1 << (index as u8) ), mask.as_integer());
3604            }
3605        }
3606        // bitwise_name_xor_assign_flag_usize => {
3607        //
3608        // }
3609    }
3610
3611    // $flag & $flag
3612    tests! {
3613        bitwise_flag_and_flag_u8 => {
3614            for index_a in 0..u8::BITS {
3615                let a = MyFlagsU8::from(index_a as u8);
3616                for index_b in 0..u8::BITS {
3617                    let b = MyFlagsU8::from(index_b as u8);
3618                    assert_eq!(MyFieldU8(0 & 1 << index_a as u8 & 1 << index_b as u8), a & b);
3619                }
3620            }
3621        }
3622        bitwise_flag_and_flag_u16 => {
3623            for index_a in 0..u16::BITS {
3624                let a = MyFlagsU16::from(index_a as u8);
3625                for index_b in 0..u16::BITS {
3626                    let b = MyFlagsU16::from(index_b as u8);
3627                    assert_eq!(MyFieldU16(0 & 1 << index_a as u8 & 1 << index_b as u8), a & b);
3628                }
3629            }
3630        }
3631        bitwise_flag_and_flag_u32 => {
3632            for index_a in 0..u32::BITS {
3633                let a = MyFlagsU32::from(index_a as u8);
3634                for index_b in 0..u32::BITS {
3635                    let b = MyFlagsU32::from(index_b as u8);
3636                    assert_eq!(MyFieldU32(0 & 1 << index_a as u8 & 1 << index_b as u8), a & b);
3637                }
3638            }
3639        }
3640        bitwise_flag_and_flag_u64 => {
3641            for index_a in 0..u64::BITS {
3642                let a = MyFlagsU64::from(index_a as u8);
3643                for index_b in 0..u64::BITS {
3644                    let b = MyFlagsU64::from(index_b as u8);
3645                    assert_eq!(MyFieldU64(0 & 1 << index_a as u8 & 1 << index_b as u8), a & b);
3646                }
3647            }
3648        }
3649        bitwise_flag_and_flag_u128 => {
3650            for index_a in 0..u128::BITS {
3651                let a = MyFlagsU128::from(index_a as u8);
3652                for index_b in 0..u128::BITS {
3653                    let b = MyFlagsU128::from(index_b as u8);
3654                    assert_eq!(MyFieldU128(0 & 1 << index_a as u8 & 1 << index_b as u8), a & b);
3655                }
3656            }
3657        }
3658        // bitwise_flag_and_flag_usize => {
3659        //
3660        // }
3661    }
3662
3663    // $flag | $flag
3664    tests! {
3665        bitwise_flag_or_flag_u8 => {
3666            for index_a in 0..u8::BITS {
3667                let a = MyFlagsU8::from(index_a as u8);
3668                for index_b in 0..u8::BITS {
3669                    let b = MyFlagsU8::from(index_b as u8);
3670                    assert_eq!(MyFieldU8(0 | 1 << index_a as u8 | 1 << index_b as u8), a | b);
3671                }
3672            }
3673        }
3674        bitwise_flag_or_flag_u16 => {
3675            for index_a in 0..u16::BITS {
3676                let a = MyFlagsU16::from(index_a as u8);
3677                for index_b in 0..u16::BITS {
3678                    let b = MyFlagsU16::from(index_b as u8);
3679                    assert_eq!(MyFieldU16(0 | 1 << index_a as u8 | 1 << index_b as u8), a | b);
3680                }
3681            }
3682        }
3683        bitwise_flag_or_flag_u32 => {
3684            for index_a in 0..u32::BITS {
3685                let a = MyFlagsU32::from(index_a as u8);
3686                for index_b in 0..u32::BITS {
3687                    let b = MyFlagsU32::from(index_b as u8);
3688                    assert_eq!(MyFieldU32(0 | 1 << index_a as u8 | 1 << index_b as u8), a | b);
3689                }
3690            }
3691        }
3692        bitwise_flag_or_flag_u64 => {
3693            for index_a in 0..u64::BITS {
3694                let a = MyFlagsU64::from(index_a as u8);
3695                for index_b in 0..u64::BITS {
3696                    let b = MyFlagsU64::from(index_b as u8);
3697                    assert_eq!(MyFieldU64(0 | 1 << index_a as u8 | 1 << index_b as u8), a | b);
3698                }
3699            }
3700        }
3701        bitwise_flag_or_flag_u128 => {
3702            for index_a in 0..u128::BITS {
3703                let a = MyFlagsU128::from(index_a as u8);
3704                for index_b in 0..u128::BITS {
3705                    let b = MyFlagsU128::from(index_b as u8);
3706                    assert_eq!(MyFieldU128(0 | 1 << index_a as u8 | 1 << index_b as u8), a | b);
3707                }
3708            }
3709        }
3710        // bitwise_flag_or_flag_usize => {
3711        //
3712        // }
3713    }
3714
3715    // $flag ^ $flag
3716    tests! {
3717        bitwise_flag_xor_flag_u8 => {
3718            for index_a in 0..u8::BITS {
3719                let a = MyFlagsU8::from(index_a as u8);
3720                for index_b in 0..u8::BITS {
3721                    let b = MyFlagsU8::from(index_b as u8);
3722                    assert_eq!(MyFieldU8(0 ^ 1 << index_a as u8 ^ 1 << index_b as u8), a ^ b);
3723                }
3724            }
3725        }
3726        bitwise_flag_xor_flag_u16 => {
3727            for index_a in 0..u16::BITS {
3728                let a = MyFlagsU16::from(index_a as u8);
3729                for index_b in 0..u16::BITS {
3730                    let b = MyFlagsU16::from(index_b as u8);
3731                    assert_eq!(MyFieldU16(0 ^ 1 << index_a as u8 ^ 1 << index_b as u8), a ^ b);
3732                }
3733            }
3734        }
3735        bitwise_flag_xor_flag_u32 => {
3736            for index_a in 0..u32::BITS {
3737                let a = MyFlagsU32::from(index_a as u8);
3738                for index_b in 0..u32::BITS {
3739                    let b = MyFlagsU32::from(index_b as u8);
3740                    assert_eq!(MyFieldU32(0 ^ 1 << index_a as u8 ^ 1 << index_b as u8), a ^ b);
3741                }
3742            }
3743        }
3744        bitwise_flag_xor_flag_u64 => {
3745            for index_a in 0..u64::BITS {
3746                let a = MyFlagsU64::from(index_a as u8);
3747                for index_b in 0..u64::BITS {
3748                    let b = MyFlagsU64::from(index_b as u8);
3749                    assert_eq!(MyFieldU64(0 ^ 1 << index_a as u8 ^ 1 << index_b as u8), a ^ b);
3750                }
3751            }
3752        }
3753        bitwise_flag_xor_flag_u128 => {
3754            for index_a in 0..u128::BITS {
3755                let a = MyFlagsU128::from(index_a as u8);
3756                for index_b in 0..u128::BITS {
3757                    let b = MyFlagsU128::from(index_b as u8);
3758                    assert_eq!(MyFieldU128(0 ^ 1 << index_a as u8 ^ 1 << index_b as u8), a ^ b);
3759                }
3760            }
3761        }
3762        // bitwise_flag_xor_flag_usize => {
3763        //
3764        // }
3765    }
3766
3767    // flag from u8
3768    tests! {
3769        flag_from_u8_u8 => {
3770            for index in 0..u8::BITS {
3771                let _ = MyFlagsU8::from(index as u8);
3772            }
3773        }
3774        flag_from_u8_u16 => {
3775            for index in 0..u16::BITS {
3776                let _ = MyFlagsU16::from(index as u8);
3777            }
3778        }
3779        flag_from_u8_u32 => {
3780            for index in 0..u32::BITS {
3781                let _ = MyFlagsU32::from(index as u8);
3782            }
3783        }
3784        flag_from_u8_u64 => {
3785            for index in 0..u64::BITS {
3786                let _ = MyFlagsU64::from(index as u8);
3787            }
3788        }
3789        flag_from_u8_u128 => {
3790            for index in 0..u128::BITS {
3791                let _ = MyFlagsU128::from(index as u8);
3792            }
3793        }
3794        flag_from_u8_usize => {
3795            for index in 0..usize::BITS {
3796                let _ = MyFlagsUsize::from(index as u8);
3797            }
3798        }
3799    }
3800
3801    // u8 from flag
3802    tests! {
3803        u8_from_flag_u8 => {
3804            for index in 0..u8::BITS {
3805                assert_eq!(MyFlagsU8::from(index as u8) as u8, index as u8)
3806            }
3807        }
3808        u8_from_flag_u16 => {
3809            for index in 0..u16::BITS {
3810                assert_eq!(MyFlagsU16::from(index as u8) as u8, index as u8)
3811            }
3812        }
3813        u8_from_flag_u32 => {
3814            for index in 0..u32::BITS {
3815                assert_eq!(MyFlagsU32::from(index as u8) as u8, index as u8)
3816            }
3817        }
3818        u8_from_flag_u64 => {
3819            for index in 0..u64::BITS {
3820                assert_eq!(MyFlagsU64::from(index as u8) as u8, index as u8)
3821            }
3822        }
3823        u8_from_flag_u128 => {
3824            for index in 0..u128::BITS {
3825                assert_eq!(MyFlagsU128::from(index as u8) as u8, index as u8)
3826            }
3827        }
3828        u8_from_flag_usize => {
3829            for index in 0..usize::BITS {
3830                assert_eq!(MyFlagsUsize::from(index as u8) as u8, index as u8)
3831            }
3832        }
3833    }
3834}
3835
3836// -------------------------------------------------------------------------------------------------