@@ -14,21 +14,10 @@ General Public License for more details.
14
14
You should have received a copy of the GNU General Public License
15
15
along with ws2812-avr. If not, see <https://www.gnu.org/licenses/>.
16
16
*/
17
- use core:: { arch:: asm, marker:: PhantomData } ;
18
-
19
- // This file allows to generate block of NOP instructions based on the
20
- // value of a const generic type. It is done by using a simple
21
- // implementation of Peano numbers, for being able to count how many
22
- // NOPs should be generated using type recursion. Then, the
23
- // "IntoNopPeano" trait is provided along with the "Num" struct that
24
- // allows to convert between common numbers and Peano ones.
25
-
26
- // This could have been done using const expressions and const
27
- // generics directly, but due to an opened in the Rust compiler it was
28
- // giving me a lot of headaches to actually make this to be able to
29
- // link successfully, so this is just a workaround for that issue:
30
- // https://github.com/rust-lang/rust/issues/84669.
17
+ use core:: arch:: asm;
31
18
19
+ // This code requires Rust Nightly 2022-08-12 to compile because of
20
+ // https://github.com/rust-lang/rust/issues/84669
32
21
pub trait IsTrue { }
33
22
pub struct BExpr < const B : bool > { }
34
23
impl IsTrue for BExpr < true > { }
@@ -37,54 +26,26 @@ pub trait NopGen {
37
26
fn gen ( ) ;
38
27
}
39
28
40
- pub struct Num < const V : u8 > { }
41
- pub struct Zero { }
42
- pub struct Succ < N > {
43
- _n : PhantomData < N > ,
44
- }
45
-
46
- // This trait is specialized on returning Peano numbers that are
47
- // verified that return instances that can generate NOP blocks,
48
- // instead of using a generic definition, because some issues I've
49
- // encountered while trying to apply this type restriction in other
50
- // places in the code.
51
- pub trait IntoNopPeano {
52
- type Peano : NopGen ;
53
- }
54
-
55
- impl IntoNopPeano for Num < 0 > {
56
- type Peano = Zero ;
57
- }
58
-
59
- impl < const X : u8 > IntoNopPeano for Num < X >
60
- where
61
- BExpr < { X > 0 } > : IsTrue ,
62
- Num < { X - 1 } > : IntoNopPeano ,
63
- {
64
- type Peano = Succ < <Num < { X - 1 } > as IntoNopPeano >:: Peano > ;
65
- }
29
+ pub struct NopBlock < const SIZE : u8 > { }
66
30
67
- impl NopGen for Zero {
31
+ impl NopGen for NopBlock < 0 > {
32
+ #[ inline( always) ]
68
33
fn gen ( ) { }
69
34
}
70
35
71
- impl < X > NopGen for Succ < X >
72
- where
73
- X : NopGen ,
74
- {
36
+ impl NopGen for NopBlock < 1 > {
75
37
#[ inline( always) ]
76
- default fn gen ( ) {
38
+ fn gen ( ) {
77
39
unsafe {
78
40
asm ! ( "nop" ) ;
79
41
}
80
-
81
- <X as NopGen >:: gen ( ) ;
82
42
}
83
43
}
84
44
85
- impl < X > NopGen for Succ < Succ < X > >
45
+ impl < const N : u8 > NopGen for NopBlock < N >
86
46
where
87
- X : NopGen ,
47
+ BExpr < { N > 1 } > : IsTrue ,
48
+ NopBlock < { N - 1 } > : NopGen ,
88
49
{
89
50
#[ inline( always) ]
90
51
fn gen ( ) {
97
58
asm ! ( "rjmp +0" ) ;
98
59
}
99
60
100
- <X as NopGen >:: gen ( ) ;
61
+ <NopBlock < { N - 1 } > as NopGen >:: gen ( ) ;
101
62
}
102
63
}
0 commit comments