Remove lifetime from dma transfer handle (#712)

The lifetime here is pointless since we copy the data anyway. And this
makes a bunch of stuff not unsafe any more which is really nice.

- [x] Changelog updated / no changelog update needed
This commit is contained in:
Gwilym Inzani 2024-05-27 13:39:27 +01:00 committed by GitHub
commit dc06aabb19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 22 additions and 25 deletions

View file

@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `find_colour_index_16` and `find_colour_index_256` to the `VRamManager` to find where a colour is in a palette. - Added `find_colour_index_16` and `find_colour_index_256` to the `VRamManager` to find where a colour is in a palette.
### Changed
- `dma.hblank_transfer` is no longer `unsafe`.
## [0.20.2] - 2024/05/25 ## [0.20.2] - 2024/05/25
### Fixed ### Fixed

View file

@ -37,12 +37,10 @@ fn main(mut gba: agb::Gba) -> ! {
.expect("Should contain colour 0x732b"); .expect("Should contain colour 0x732b");
loop { loop {
let _background_color_transfer = unsafe { let _background_color_transfer = dma.hblank_transfer(
dma.hblank_transfer( &vram.background_palette_colour_dma(0, background_colour_index),
&vram.background_palette_colour_dma(0, background_colour_index), &colours,
&colours, );
)
};
vblank.wait_for_vblank(); vblank.wait_for_vblank();
} }

View file

@ -34,8 +34,7 @@ fn main(mut gba: agb::Gba) -> ! {
let mut frame = 0; let mut frame = 0;
loop { loop {
let _x_scroll_transfer = let _x_scroll_transfer = dma.hblank_transfer(&map.x_scroll_dma(), &offsets[frame..]);
unsafe { dma.hblank_transfer(&map.x_scroll_dma(), &offsets[frame..]) };
vblank.wait_for_vblank(); vblank.wait_for_vblank();
frame += 1; frame += 1;

View file

@ -90,7 +90,7 @@ fn main(mut gba: agb::Gba) -> ! {
window.commit(); window.commit();
let dma_controllable = window.win_in(WinIn::Win0).horizontal_position_dma(); let dma_controllable = window.win_in(WinIn::Win0).horizontal_position_dma();
let _transfer = unsafe { dmas.dma0.hblank_transfer(&dma_controllable, &circle_poses) }; let _transfer = dmas.dma0.hblank_transfer(&dma_controllable, &circle_poses);
vblank.wait_for_vblank(); vblank.wait_for_vblank();
} }

View file

@ -70,18 +70,14 @@ impl Dma {
/// drop the DmaTransferHandler return value until the next vblank interrupt to ensure that you /// drop the DmaTransferHandler return value until the next vblank interrupt to ensure that you
/// a continuous effect. /// a continuous effect.
/// ///
/// # Safety
///
/// While DmaTransferHandle is not dropped, the slice at `values` must not move in memory.
///
/// # Examples /// # Examples
/// ///
/// See the `dma_effect_*` examples in the repository to see some ways to use this. /// See the `dma_effect_*` examples in the repository to see some ways to use this.
pub unsafe fn hblank_transfer<'a, T>( pub fn hblank_transfer<T>(
&'a self, &self,
location: &DmaControllable<T>, location: &DmaControllable<T>,
values: &'a [T], values: &[T],
) -> DmaTransferHandle<'a, T> ) -> DmaTransferHandle<T>
where where
T: Copy, T: Copy,
{ {
@ -93,10 +89,13 @@ impl Dma {
let n_transfers = (size_of::<T>() / 2) as u32; let n_transfers = (size_of::<T>() / 2) as u32;
self.source_addr.set(handle.data.as_ptr().add(1) as u32); self.source_addr.set(handle.data[1..].as_ptr() as u32);
self.dest_addr.set(location.memory_location as u32); self.dest_addr.set(location.memory_location as u32);
location.memory_location.write_volatile(values[0]); // SAFETY: by construction it is safe to write to location.memory_location
unsafe {
location.memory_location.write_volatile(values[0]);
}
self.ctrl_addr.set( self.ctrl_addr.set(
(0b10 << 0x15) | // keep destination address fixed (0b10 << 0x15) | // keep destination address fixed
@ -127,17 +126,15 @@ impl<Item> DmaControllable<Item> {
} }
} }
pub struct DmaTransferHandle<'a, T> pub struct DmaTransferHandle<T>
where where
T: Copy, T: Copy,
{ {
number: usize, number: usize,
data: Pin<Box<[T]>>, data: Pin<Box<[T]>>,
phantom: PhantomData<&'a ()>,
} }
impl<'a, T> DmaTransferHandle<'a, T> impl<T> DmaTransferHandle<T>
where where
T: Copy, T: Copy,
{ {
@ -145,12 +142,11 @@ where
Self { Self {
number, number,
data: Box::into_pin(data.into()), data: Box::into_pin(data.into()),
phantom: PhantomData,
} }
} }
} }
impl<'a, T> Drop for DmaTransferHandle<'a, T> impl<T> Drop for DmaTransferHandle<T>
where where
T: Copy, T: Copy,
{ {