@@ -52,8 +52,10 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
52
52
B : WriteBuffer + ' static ,
53
53
T : OnChannel < C > ,
54
54
{
55
- // NOTE(unsafe) cannot call `&mut self` methods on `buffer` because its
56
- // concrete type is unknown here
55
+ // NOTE(unsafe) We don't know the concrete type of `buffer` here, all
56
+ // we can use are its `WriteBuffer` methods. Hence the only `&mut self`
57
+ // method we can call is `write_buffer`, which is allowed by
58
+ // `WriteBuffer`'s safety requirements.
57
59
let ( ptr, len) = unsafe { buffer. write_buffer ( ) } ;
58
60
let len = u16 ( len) . expect ( "buffer is too large" ) ;
59
61
@@ -75,8 +77,10 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
75
77
B : ReadBuffer + ' static ,
76
78
T : OnChannel < C > ,
77
79
{
78
- // NOTE(unsafe) cannot call `&mut self` methods on `buffer` because its
79
- // concrete type is unknown here
80
+ // NOTE(unsafe) We don't know the concrete type of `buffer` here, all
81
+ // we can use are its `ReadBuffer` methods. Hence there are no
82
+ // `&mut self` methods we can call, so we are safe according to
83
+ // `ReadBuffer`'s safety requirements.
80
84
let ( ptr, len) = unsafe { buffer. read_buffer ( ) } ;
81
85
let len = u16 ( len) . expect ( "buffer is too large" ) ;
82
86
@@ -168,11 +172,11 @@ impl<B, C: Channel, T: Target> TransferInner<B, C, T> {
168
172
/// The implementing type must be safe to use for DMA reads. This means:
169
173
///
170
174
/// - It must be a pointer that references the actual buffer.
171
- /// - The following requirements must be fulfilled by `read_buffer` :
172
- /// - The function must always return the same values , if called multiple
175
+ /// - As long as no `&mut self` method is called on the implementing object :
176
+ /// - `read_buffer` must always return the same value , if called multiple
173
177
/// times.
174
- /// - The memory specified by the returned pointer and size must not be
175
- /// freed as long as `self` is not dropped.
178
+ /// - The memory specified by the pointer and size returned by `read_buffer`
179
+ /// must not be freed as long as `self` is not dropped.
176
180
pub unsafe trait ReadBuffer {
177
181
type Word ;
178
182
@@ -186,7 +190,7 @@ pub unsafe trait ReadBuffer {
186
190
/// # Safety
187
191
///
188
192
/// Once this method has been called, it is unsafe to call any `&mut self`
189
- /// methods on this object as long as the returned value is used (for DMA).
193
+ /// methods on this object as long as the returned value is in use (by DMA).
190
194
unsafe fn read_buffer ( & self ) -> ( * const Self :: Word , usize ) ;
191
195
}
192
196
@@ -198,11 +202,12 @@ pub unsafe trait ReadBuffer {
198
202
///
199
203
/// - It must be a pointer that references the actual buffer.
200
204
/// - `Target` must be a type that is valid for any possible byte pattern.
201
- /// - The following requirements must be fulfilled by `write_buffer`:
202
- /// - The function must always return the same values, if called multiple
205
+ /// - As long as no `&mut self` method, except for `write_buffer`, is called on
206
+ /// the implementing object:
207
+ /// - `write_buffer` must always return the same value, if called multiple
203
208
/// times.
204
- /// - The memory specified by the returned pointer and size must not be
205
- /// freed as long as `self` is not dropped.
209
+ /// - The memory specified by the pointer and size returned by `write_buffer`
210
+ /// must not be freed as long as `self` is not dropped.
206
211
pub unsafe trait WriteBuffer {
207
212
type Word ;
208
213
@@ -216,7 +221,8 @@ pub unsafe trait WriteBuffer {
216
221
/// # Safety
217
222
///
218
223
/// Once this method has been called, it is unsafe to call any `&mut self`
219
- /// methods on this object as long as the returned value is used (for DMA)..
224
+ /// methods, except for `write_buffer`, on this object as long as the
225
+ /// returned value is in use (by DMA).
220
226
unsafe fn write_buffer ( & mut self ) -> ( * mut Self :: Word , usize ) ;
221
227
}
222
228
0 commit comments