@@ -39,7 +39,9 @@ impl<B, C: Channel, T> Transfer<B, C, T> {
39
39
B : WriteBuffer + ' static ,
40
40
T : Target < C > ,
41
41
{
42
- let ( ptr, len) = buffer. write_buffer ( ) ;
42
+ // NOTE(unsafe) cannot call `&mut self` methods on `buffer` because its
43
+ // concrete type is unknown here
44
+ let ( ptr, len) = unsafe { buffer. write_buffer ( ) } ;
43
45
44
46
channel. set_memory_address ( ptr as u32 , Increment :: Enable ) ;
45
47
channel. set_transfer_length ( len) ;
@@ -55,7 +57,9 @@ impl<B, C: Channel, T> Transfer<B, C, T> {
55
57
B : ReadBuffer + ' static ,
56
58
T : Target < C > ,
57
59
{
58
- let ( ptr, len) = buffer. read_buffer ( ) ;
60
+ // NOTE(unsafe) cannot call `&mut self` methods on `buffer` because its
61
+ // concrete type is unknown here
62
+ let ( ptr, len) = unsafe { buffer. read_buffer ( ) } ;
59
63
60
64
channel. set_memory_address ( ptr as u32 , Increment :: Enable ) ;
61
65
channel. set_transfer_length ( len) ;
@@ -142,7 +146,11 @@ impl<B, C: Channel, T> TransferInner<B, C, T> {
142
146
/// The implementing type must be safe to use for DMA reads. This means:
143
147
///
144
148
/// - It must be a pointer that references the actual buffer.
145
- /// - The requirements documented on `read_buffer` must be fulfilled.
149
+ /// - The following requirements must be fulfilled by `read_buffer`:
150
+ /// - The function must always return the same values, if called multiple
151
+ /// times.
152
+ /// - The memory specified by the returned pointer and size must not be
153
+ /// freed as long as `self` is not dropped.
146
154
pub unsafe trait ReadBuffer {
147
155
type Word ;
148
156
@@ -155,11 +163,9 @@ pub unsafe trait ReadBuffer {
155
163
///
156
164
/// # Safety
157
165
///
158
- /// - This function must always return the same values, if called multiple
159
- /// times.
160
- /// - The memory specified by the returned pointer and size must not be
161
- /// freed as long as `self` is not dropped.
162
- fn read_buffer ( & self ) -> ( * const Self :: Word , usize ) ;
166
+ /// Once this method has been called, it is unsafe to call any `&mut self`
167
+ /// methods on this object as long as the returned value is used (for DMA).
168
+ unsafe fn read_buffer ( & self ) -> ( * const Self :: Word , usize ) ;
163
169
}
164
170
165
171
/// Trait for buffers that can be given to DMA for writing.
@@ -170,7 +176,11 @@ pub unsafe trait ReadBuffer {
170
176
///
171
177
/// - It must be a pointer that references the actual buffer.
172
178
/// - `Target` must be a type that is valid for any possible byte pattern.
173
- /// - The requirements documented on `write_buffer` must be fulfilled.
179
+ /// - The following requirements must be fulfilled by `write_buffer`:
180
+ /// - The function must always return the same values, if called multiple
181
+ /// times.
182
+ /// - The memory specified by the returned pointer and size must not be
183
+ /// freed as long as `self` is not dropped.
174
184
pub unsafe trait WriteBuffer {
175
185
type Word ;
176
186
@@ -183,11 +193,9 @@ pub unsafe trait WriteBuffer {
183
193
///
184
194
/// # Safety
185
195
///
186
- /// - This function must always return the same values, if called multiple
187
- /// times.
188
- /// - The memory specified by the returned pointer and size must not be
189
- /// freed as long as `self` is not dropped.
190
- fn write_buffer ( & mut self ) -> ( * mut Self :: Word , usize ) ;
196
+ /// Once this method has been called, it is unsafe to call any `&mut self`
197
+ /// methods on this object as long as the returned value is used (for DMA)..
198
+ unsafe fn write_buffer ( & mut self ) -> ( * mut Self :: Word , usize ) ;
191
199
}
192
200
193
201
// Blanked implementations for common DMA buffer types.
@@ -199,7 +207,7 @@ where
199
207
{
200
208
type Word = T :: Word ;
201
209
202
- fn read_buffer ( & self ) -> ( * const Self :: Word , usize ) {
210
+ unsafe fn read_buffer ( & self ) -> ( * const Self :: Word , usize ) {
203
211
self . as_read_buffer ( )
204
212
}
205
213
}
@@ -211,7 +219,7 @@ where
211
219
{
212
220
type Word = T :: Word ;
213
221
214
- fn write_buffer ( & mut self ) -> ( * mut Self :: Word , usize ) {
222
+ unsafe fn write_buffer ( & mut self ) -> ( * mut Self :: Word , usize ) {
215
223
self . as_write_buffer ( )
216
224
}
217
225
}
0 commit comments