@@ -24,6 +24,17 @@ pub struct Synth {
24
24
pub ext_in : i32 ,
25
25
}
26
26
27
+ // slice::rotate_left is inefficient for small arrays:
28
+ // https://github.com/rust-lang/rust/issues/89714
29
+ fn rotate3 < T > ( [ a, b, c] : [ T ; 3 ] , i : usize ) -> [ T ; 3 ] {
30
+ match i {
31
+ 0 => [ a, b, c] ,
32
+ 1 => [ b, c, a] ,
33
+ 2 => [ c, a, b] ,
34
+ _ => panic ! ( "index out of bounds" ) ,
35
+ }
36
+ }
37
+
27
38
impl Synth {
28
39
pub fn new ( chip_model : ChipModel ) -> Self {
29
40
Synth {
@@ -36,9 +47,7 @@ impl Synth {
36
47
37
48
pub fn syncable_voice ( & self , i : usize ) -> Syncable < & ' _ Voice > {
38
49
let [ a, b, c] = & self . voices ;
39
- let mut voices_ref = [ a, b, c] ;
40
- voices_ref. rotate_left ( i) ;
41
- let [ main, sync_dest, sync_source] = voices_ref;
50
+ let [ main, sync_dest, sync_source] = rotate3 ( [ a, b, c] , i) ;
42
51
Syncable {
43
52
main,
44
53
sync_dest,
@@ -48,9 +57,7 @@ impl Synth {
48
57
49
58
pub fn syncable_voice_mut ( & mut self , i : usize ) -> Syncable < & ' _ mut Voice > {
50
59
let [ a, b, c] = & mut self . voices ;
51
- let mut voices_mut = [ a, b, c] ;
52
- voices_mut. rotate_left ( i) ;
53
- let [ main, sync_dest, sync_source] = voices_mut;
60
+ let [ main, sync_dest, sync_source] = rotate3 ( [ a, b, c] , i) ;
54
61
Syncable {
55
62
main,
56
63
sync_dest,
0 commit comments