Skip to content

Commit 3b4daf2

Browse files
committed
Moved Transfer's non-drop extraction to its own function
1 parent 5b9a63c commit 3b4daf2

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/dma.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ where
374374
}
375375
}
376376

377+
impl<MODE, BUFFER, PAYLOAD> Transfer<MODE, BUFFER, PAYLOAD>
378+
where
379+
PAYLOAD: TransferPayload,
380+
{
381+
pub(crate) fn extract_inner_without_drop(self) -> (BUFFER, PAYLOAD) {
382+
// `Transfer` needs to have a `Drop` implementation, because we accept
383+
// managed buffers that can free their memory on drop. Because of that
384+
// we can't move out of the `Transfer`'s fields, so we use `ptr::read`
385+
// and `mem::forget`.
386+
//
387+
// NOTE(unsafe) There is no panic branch between getting the resources
388+
// and forgetting `self`.
389+
unsafe {
390+
// We cannot use mem::replace as we do not have valid objects to replace with
391+
let buffer = ptr::read(&self.buffer);
392+
let payload = ptr::read(&self.payload);
393+
core::mem::forget(self);
394+
(buffer, payload)
395+
}
396+
}
397+
}
398+
377399
/// Read transfer
378400
pub struct R;
379401

@@ -426,19 +448,10 @@ macro_rules! rx_tx_channel_mapping {
426448
// before the previous statement, which marks the DMA transfer as done
427449
atomic::compiler_fence(Ordering::SeqCst);
428450

429-
// `Transfer` needs to have a `Drop` implementation, because we accept
451+
// `Transfer` has a `Drop` implementation because we accept
430452
// managed buffers that can free their memory on drop. Because of that
431-
// we can't move out of the `Transfer`'s fields, so we use `ptr::read`
432-
// and `mem::forget`.
433-
//
434-
// NOTE(unsafe) There is no panic branch between getting the resources
435-
// and forgetting `self`.
436-
unsafe {
437-
let buffer = ptr::read(&self.buffer);
438-
let payload = ptr::read(&self.payload);
439-
core::mem::forget(self);
440-
(buffer, payload)
441-
}
453+
// we can't move out of the `Transfer`'s fields directly.
454+
self.extract_inner_without_drop()
442455
}
443456
}
444457

@@ -937,19 +950,10 @@ macro_rules! dma {
937950
// before the previous statement, which marks the DMA transfer as done
938951
atomic::compiler_fence(Ordering::SeqCst);
939952

940-
// `Transfer` needs to have a `Drop` implementation, because we accept
953+
// `Transfer` has a `Drop` implementation because we accept
941954
// managed buffers that can free their memory on drop. Because of that
942-
// we can't move out of the `Transfer`'s fields, so we use `ptr::read`
943-
// and `mem::forget`.
944-
//
945-
// NOTE(unsafe) There is no panic branch between getting the resources
946-
// and forgetting `self`.
947-
unsafe {
948-
let buffer = ptr::read(&self.buffer);
949-
let payload = ptr::read(&self.payload);
950-
core::mem::forget(self);
951-
(buffer, payload)
952-
}
955+
// we can't move out of the `Transfer`'s fields directly.
956+
self.extract_inner_without_drop()
953957
}
954958
}
955959

@@ -975,19 +979,10 @@ macro_rules! dma {
975979
// before the previous statement, which marks the DMA transfer as done
976980
atomic::compiler_fence(Ordering::SeqCst);
977981

978-
// `Transfer` needs to have a `Drop` implementation, because we accept
982+
// `Transfer` has a `Drop` implementation because we accept
979983
// managed buffers that can free their memory on drop. Because of that
980-
// we can't move out of the `Transfer`'s fields, so we use `ptr::read`
981-
// and `mem::forget`.
982-
//
983-
// NOTE(unsafe) There is no panic branch between getting the resources
984-
// and forgetting `self`.
985-
unsafe {
986-
let buffer = ptr::read(&self.buffer);
987-
let payload = ptr::read(&self.payload);
988-
core::mem::forget(self);
989-
(buffer, payload)
990-
}
984+
// we can't move out of the `Transfer`'s fields directly.
985+
self.extract_inner_without_drop()
991986
}
992987
}
993988

0 commit comments

Comments
 (0)