Skip to content

Commit 25dda1b

Browse files
committed
dma: Improve wording of Safety notes
1 parent 0f1d6a3 commit 25dda1b

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/dma.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
5252
B: WriteBuffer + 'static,
5353
T: OnChannel<C>,
5454
{
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.
5759
let (ptr, len) = unsafe { buffer.write_buffer() };
5860
let len = u16(len).expect("buffer is too large");
5961

@@ -75,8 +77,10 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
7577
B: ReadBuffer + 'static,
7678
T: OnChannel<C>,
7779
{
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.
8084
let (ptr, len) = unsafe { buffer.read_buffer() };
8185
let len = u16(len).expect("buffer is too large");
8286

@@ -168,11 +172,11 @@ impl<B, C: Channel, T: Target> TransferInner<B, C, T> {
168172
/// The implementing type must be safe to use for DMA reads. This means:
169173
///
170174
/// - 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
173177
/// 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.
176180
pub unsafe trait ReadBuffer {
177181
type Word;
178182

@@ -186,7 +190,7 @@ pub unsafe trait ReadBuffer {
186190
/// # Safety
187191
///
188192
/// 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).
190194
unsafe fn read_buffer(&self) -> (*const Self::Word, usize);
191195
}
192196

@@ -198,11 +202,12 @@ pub unsafe trait ReadBuffer {
198202
///
199203
/// - It must be a pointer that references the actual buffer.
200204
/// - `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
203208
/// 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.
206211
pub unsafe trait WriteBuffer {
207212
type Word;
208213

@@ -216,7 +221,8 @@ pub unsafe trait WriteBuffer {
216221
/// # Safety
217222
///
218223
/// 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).
220226
unsafe fn write_buffer(&mut self) -> (*mut Self::Word, usize);
221227
}
222228

0 commit comments

Comments
 (0)