@@ -10,9 +10,6 @@ use core::ptr;
10
10
use core:: slice;
11
11
12
12
use crate :: rcc:: AHB1 ;
13
- use as_slice:: AsSlice ;
14
- pub use generic_array:: typenum:: { self , consts} ;
15
- use generic_array:: { ArrayLength , GenericArray } ;
16
13
use stable_deref_trait:: StableDeref ;
17
14
18
15
#[ non_exhaustive]
@@ -34,21 +31,18 @@ pub enum Half {
34
31
}
35
32
36
33
/// Frame reader "worker", access and handling of frame reads is made through this structure.
37
- pub struct FrameReader < BUFFER , CHANNEL , N >
34
+ pub struct FrameReader < BUFFER , CHANNEL , const N : usize >
38
35
where
39
36
BUFFER : Sized + StableDeref < Target = DMAFrame < N > > + DerefMut + ' static ,
40
- N : ArrayLength < MaybeUninit < u8 > > ,
41
37
{
42
38
buffer : BUFFER ,
43
39
channel : CHANNEL ,
44
40
matching_character : u8 ,
45
- _marker : core:: marker:: PhantomData < N > , // Needed to make the compiler happy
46
41
}
47
42
48
- impl < BUFFER , CHANNEL , N > FrameReader < BUFFER , CHANNEL , N >
43
+ impl < BUFFER , CHANNEL , const N : usize > FrameReader < BUFFER , CHANNEL , N >
49
44
where
50
45
BUFFER : Sized + StableDeref < Target = DMAFrame < N > > + DerefMut + ' static ,
51
- N : ArrayLength < MaybeUninit < u8 > > ,
52
46
{
53
47
pub ( crate ) fn new (
54
48
buffer : BUFFER ,
@@ -59,33 +53,28 @@ where
59
53
buffer,
60
54
channel,
61
55
matching_character,
62
- _marker : core:: marker:: PhantomData ,
63
56
}
64
57
}
65
58
}
66
59
67
60
/// Frame sender "worker", access and handling of frame transmissions is made through this
68
61
/// structure.
69
- pub struct FrameSender < BUFFER , CHANNEL , N >
62
+ pub struct FrameSender < BUFFER , CHANNEL , const N : usize >
70
63
where
71
64
BUFFER : Sized + StableDeref < Target = DMAFrame < N > > + DerefMut + ' static ,
72
- N : ArrayLength < MaybeUninit < u8 > > ,
73
65
{
74
66
buffer : Option < BUFFER > ,
75
67
channel : CHANNEL ,
76
- _marker : core:: marker:: PhantomData < N > ,
77
68
}
78
69
79
- impl < BUFFER , CHANNEL , N > FrameSender < BUFFER , CHANNEL , N >
70
+ impl < BUFFER , CHANNEL , const N : usize > FrameSender < BUFFER , CHANNEL , N >
80
71
where
81
72
BUFFER : Sized + StableDeref < Target = DMAFrame < N > > + DerefMut + ' static ,
82
- N : ArrayLength < MaybeUninit < u8 > > ,
83
73
{
84
74
pub ( crate ) fn new ( channel : CHANNEL ) -> FrameSender < BUFFER , CHANNEL , N > {
85
75
Self {
86
76
buffer : None ,
87
77
channel,
88
- _marker : core:: marker:: PhantomData ,
89
78
}
90
79
}
91
80
}
@@ -96,27 +85,18 @@ where
96
85
/// used with, for example, [`heapless::pool`] to create a pool of serial frames.
97
86
///
98
87
/// [`heapless::pool`]: https://docs.rs/heapless/0.5.3/heapless/pool/index.html
99
- pub struct DMAFrame < N >
100
- where
101
- N : ArrayLength < MaybeUninit < u8 > > ,
102
- {
88
+ pub struct DMAFrame < const N : usize > {
103
89
len : u16 ,
104
- buf : GenericArray < MaybeUninit < u8 > , N > ,
90
+ buf : [ MaybeUninit < u8 > ; N ] ,
105
91
}
106
92
107
- impl < N > fmt:: Debug for DMAFrame < N >
108
- where
109
- N : ArrayLength < MaybeUninit < u8 > > ,
110
- {
93
+ impl < const N : usize > fmt:: Debug for DMAFrame < N > {
111
94
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112
95
write ! ( f, "{:?}" , self . read( ) )
113
96
}
114
97
}
115
98
116
- impl < N > fmt:: Write for DMAFrame < N >
117
- where
118
- N : ArrayLength < MaybeUninit < u8 > > ,
119
- {
99
+ impl < const N : usize > fmt:: Write for DMAFrame < N > {
120
100
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
121
101
let free = self . free ( ) ;
122
102
@@ -129,29 +109,21 @@ where
129
109
}
130
110
}
131
111
132
- impl < N > Default for DMAFrame < N >
133
- where
134
- N : ArrayLength < MaybeUninit < u8 > > ,
135
- {
112
+ impl < const N : usize > Default for DMAFrame < N > {
136
113
fn default ( ) -> Self {
137
114
Self :: new ( )
138
115
}
139
116
}
140
117
141
- impl < N > DMAFrame < N >
142
- where
143
- N : ArrayLength < MaybeUninit < u8 > > ,
144
- {
118
+ impl < const N : usize > DMAFrame < N > {
119
+ const INIT : MaybeUninit < u8 > = MaybeUninit :: uninit ( ) ;
145
120
/// Creates a new node for the Serial DMA
146
121
#[ inline]
147
- pub fn new ( ) -> Self {
148
- // Create an uninitialized array of `MaybeUninit<u8>`. The `assume_init` is
149
- // safe because the type we are claiming to have initialized here is a
150
- // bunch of `MaybeUninit`s, which do not require initialization.
151
- #[ allow( clippy:: uninit_assumed_init) ]
122
+ pub const fn new ( ) -> Self {
123
+ // Create an uninitialized array of `MaybeUninit<u8>`.
152
124
Self {
153
125
len : 0 ,
154
- buf : unsafe { MaybeUninit :: uninit ( ) . assume_init ( ) } ,
126
+ buf : [ Self :: INIT ; N ] ,
155
127
}
156
128
}
157
129
@@ -162,7 +134,7 @@ where
162
134
pub fn write ( & mut self ) -> & mut [ u8 ] {
163
135
// Initialize remaining memory with a safe value
164
136
for elem in & mut self . buf [ self . len as usize ..] {
165
- * elem = MaybeUninit :: zeroed ( ) ;
137
+ * elem = MaybeUninit :: new ( 0 ) ;
166
138
}
167
139
168
140
self . len = self . max_len ( ) as u16 ;
@@ -183,7 +155,7 @@ where
183
155
/// Gives an uninitialized `&mut [MaybeUninit<u8>]` slice to write into, the `set_len` method
184
156
/// must then be used to set the actual number of bytes written.
185
157
#[ inline]
186
- pub fn write_uninit ( & mut self ) -> & mut [ MaybeUninit < u8 > ] {
158
+ pub fn write_uninit ( & mut self ) -> & mut [ MaybeUninit < u8 > ; N ] {
187
159
& mut self . buf
188
160
}
189
161
@@ -256,7 +228,7 @@ where
256
228
/// Get the max length of the frame
257
229
#[ inline]
258
230
pub fn max_len ( & self ) -> usize {
259
- N :: to_usize ( )
231
+ N
260
232
}
261
233
262
234
/// Checks if the frame is empty
@@ -281,13 +253,9 @@ where
281
253
}
282
254
}
283
255
284
- impl < N > AsSlice for DMAFrame < N >
285
- where
286
- N : ArrayLength < MaybeUninit < u8 > > ,
287
- {
288
- type Element = u8 ;
289
-
290
- fn as_slice ( & self ) -> & [ Self :: Element ] {
256
+ impl < const N : usize > AsRef < [ u8 ] > for DMAFrame < N > {
257
+ #[ inline]
258
+ fn as_ref ( & self ) -> & [ u8 ] {
291
259
self . read ( )
292
260
}
293
261
}
@@ -394,10 +362,7 @@ macro_rules! dma {
394
362
$(
395
363
pub mod $dmaX {
396
364
use core:: sync:: atomic:: { self , Ordering } ;
397
- use as_slice:: { AsSlice } ;
398
365
use crate :: stm32:: { $DMAX, dma1} ;
399
- use core:: mem:: MaybeUninit ;
400
- use generic_array:: ArrayLength ;
401
366
use core:: ops:: DerefMut ;
402
367
use core:: ptr;
403
368
use stable_deref_trait:: StableDeref ;
@@ -525,10 +490,9 @@ macro_rules! dma {
525
490
526
491
}
527
492
528
- impl <BUFFER , N > FrameSender <BUFFER , $CX, N >
493
+ impl <BUFFER , const N : usize > FrameSender <BUFFER , $CX, N >
529
494
where
530
495
BUFFER : Sized + StableDeref <Target = DMAFrame <N >> + DerefMut + ' static ,
531
- N : ArrayLength <MaybeUninit <u8 >>,
532
496
{
533
497
/// This method should be called in the transfer complete interrupt of the
534
498
/// DMA, will return the sent frame if the transfer was truly completed.
@@ -593,10 +557,9 @@ macro_rules! dma {
593
557
}
594
558
}
595
559
596
- impl <BUFFER , N > FrameReader <BUFFER , $CX, N >
560
+ impl <BUFFER , const N : usize > FrameReader <BUFFER , $CX, N >
597
561
where
598
562
BUFFER : Sized + StableDeref <Target = DMAFrame <N >> + DerefMut + ' static ,
599
- N : ArrayLength <MaybeUninit <u8 >>,
600
563
{
601
564
/// This function should be called from the transfer complete interrupt of
602
565
/// the corresponding DMA channel.
@@ -726,7 +689,7 @@ macro_rules! dma {
726
689
where
727
690
F : FnOnce ( & [ T ] , Half ) -> Result <( usize , R ) , ( ) >,
728
691
B : StableDeref <Target = [ H ; 2 ] > + ' static ,
729
- H : AsSlice < Element = T >,
692
+ H : AsRef < [ T ] >,
730
693
{
731
694
// this inverts expectation and returns the half being _written_
732
695
let buf = match self . readable_half {
@@ -737,7 +700,7 @@ macro_rules! dma {
737
700
// [ x x x x y y y y y z | z z z z z z z z z z ]
738
701
// ^- pending=11
739
702
let pending = self . channel. get_cndtr( ) as usize ; // available bytes in _whole_ buffer
740
- let slice = buf. as_slice ( ) ;
703
+ let slice = buf. as_ref ( ) ;
741
704
let capacity = slice. len( ) ; // capacity of _half_ a buffer
742
705
// <--- capacity=10 --->
743
706
// [ x x x x y y y y y z | z z z z z z z z z z ]
@@ -754,7 +717,7 @@ macro_rules! dma {
754
717
// ^- end=9
755
718
// ^- consumed_offset=4
756
719
// [y y y y y] <-- slice
757
- let slice = & buf. as_slice ( ) [ self . consumed_offset..end] ;
720
+ let slice = & buf. as_ref ( ) [ self . consumed_offset..end] ;
758
721
match f( slice, self . readable_half) {
759
722
Ok ( ( l, r) ) => { self . consumed_offset += l; Ok ( r) } ,
760
723
Err ( _) => Err ( Error :: BufferError ) ,
@@ -767,14 +730,14 @@ macro_rules! dma {
767
730
where
768
731
F : FnOnce ( & [ T ] , Half ) -> R ,
769
732
B : StableDeref <Target = [ H ; 2 ] > + ' static ,
770
- H : AsSlice < Element = T >,
733
+ H : AsRef < [ T ] >,
771
734
{
772
735
let half_being_read = self . readable_half( ) ?;
773
736
let buf = match half_being_read {
774
737
Half :: First => & self . buffer[ 0 ] ,
775
738
Half :: Second => & self . buffer[ 1 ] ,
776
739
} ;
777
- let slice = & buf. as_slice ( ) [ self . consumed_offset..] ;
740
+ let slice = & buf. as_ref ( ) [ self . consumed_offset..] ;
778
741
self . consumed_offset = 0 ;
779
742
Ok ( f( slice, half_being_read) )
780
743
}
@@ -844,13 +807,13 @@ macro_rules! dma {
844
807
impl <BUFFER , PAYLOAD > Transfer <W , & ' static mut BUFFER , $CX, PAYLOAD > {
845
808
pub fn peek<T >( & self ) -> & [ T ]
846
809
where
847
- BUFFER : AsSlice < Element = T > ,
810
+ BUFFER : AsRef < [ T ] >
848
811
{
849
812
let pending = self . channel. get_cndtr( ) as usize ;
850
813
851
- let capacity = self . buffer. as_slice ( ) . len( ) ;
814
+ let capacity = self . buffer. as_ref ( ) . len( ) ;
852
815
853
- & self . buffer. as_slice ( ) [ ..( capacity - pending) ]
816
+ & self . buffer. as_ref ( ) [ ..( capacity - pending) ]
854
817
}
855
818
}
856
819
) +
0 commit comments