From 248f71dbfa5c534215d89889c02b755b38457298 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Mon, 27 May 2024 13:20:13 +0100 Subject: [PATCH 1/4] Remove lifetime from DMA transfer handle --- agb/src/dma.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/agb/src/dma.rs b/agb/src/dma.rs index a16fc021..b1a3aac9 100644 --- a/agb/src/dma.rs +++ b/agb/src/dma.rs @@ -77,11 +77,11 @@ impl Dma { /// # Examples /// /// See the `dma_effect_*` examples in the repository to see some ways to use this. - pub unsafe fn hblank_transfer<'a, T>( - &'a self, + pub unsafe fn hblank_transfer( + &self, location: &DmaControllable, - values: &'a [T], - ) -> DmaTransferHandle<'a, T> + values: &[T], + ) -> DmaTransferHandle where T: Copy, { @@ -127,17 +127,15 @@ impl DmaControllable { } } -pub struct DmaTransferHandle<'a, T> +pub struct DmaTransferHandle where T: Copy, { number: usize, data: Pin>, - - phantom: PhantomData<&'a ()>, } -impl<'a, T> DmaTransferHandle<'a, T> +impl DmaTransferHandle where T: Copy, { @@ -145,12 +143,11 @@ where Self { number, data: Box::into_pin(data.into()), - phantom: PhantomData, } } } -impl<'a, T> Drop for DmaTransferHandle<'a, T> +impl Drop for DmaTransferHandle where T: Copy, { From e9ce8cd2ad7904357102c2503dc1e03880377b6f Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Mon, 27 May 2024 13:23:49 +0100 Subject: [PATCH 2/4] hblack_transfer doesn't need to be unsafe --- agb/src/dma.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/agb/src/dma.rs b/agb/src/dma.rs index b1a3aac9..48e870db 100644 --- a/agb/src/dma.rs +++ b/agb/src/dma.rs @@ -70,14 +70,10 @@ impl Dma { /// drop the DmaTransferHandler return value until the next vblank interrupt to ensure that you /// a continuous effect. /// - /// # Safety - /// - /// While DmaTransferHandle is not dropped, the slice at `values` must not move in memory. - /// /// # Examples /// /// See the `dma_effect_*` examples in the repository to see some ways to use this. - pub unsafe fn hblank_transfer( + pub fn hblank_transfer( &self, location: &DmaControllable, values: &[T], @@ -93,10 +89,13 @@ impl Dma { let n_transfers = (size_of::() / 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); - 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( (0b10 << 0x15) | // keep destination address fixed From c4719a5ce0e1bcf15bd7adc9f89ebb234df3e59d Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Mon, 27 May 2024 13:24:58 +0100 Subject: [PATCH 3/4] Remove unsafe from examples --- agb/examples/dma_effect_background_colour.rs | 10 ++++------ agb/examples/dma_effect_background_scroll.rs | 3 +-- agb/examples/dma_effect_circular_window.rs | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/agb/examples/dma_effect_background_colour.rs b/agb/examples/dma_effect_background_colour.rs index f7386c59..7d1cfed1 100644 --- a/agb/examples/dma_effect_background_colour.rs +++ b/agb/examples/dma_effect_background_colour.rs @@ -37,12 +37,10 @@ fn main(mut gba: agb::Gba) -> ! { .expect("Should contain colour 0x732b"); loop { - let _background_color_transfer = unsafe { - dma.hblank_transfer( - &vram.background_palette_colour_dma(0, background_colour_index), - &colours, - ) - }; + let _background_color_transfer = dma.hblank_transfer( + &vram.background_palette_colour_dma(0, background_colour_index), + &colours, + ); vblank.wait_for_vblank(); } diff --git a/agb/examples/dma_effect_background_scroll.rs b/agb/examples/dma_effect_background_scroll.rs index eb5cdfaa..a74243ce 100644 --- a/agb/examples/dma_effect_background_scroll.rs +++ b/agb/examples/dma_effect_background_scroll.rs @@ -34,8 +34,7 @@ fn main(mut gba: agb::Gba) -> ! { let mut frame = 0; loop { - let _x_scroll_transfer = - unsafe { dma.hblank_transfer(&map.x_scroll_dma(), &offsets[frame..]) }; + let _x_scroll_transfer = dma.hblank_transfer(&map.x_scroll_dma(), &offsets[frame..]); vblank.wait_for_vblank(); frame += 1; diff --git a/agb/examples/dma_effect_circular_window.rs b/agb/examples/dma_effect_circular_window.rs index d98e6f60..cbdf8285 100644 --- a/agb/examples/dma_effect_circular_window.rs +++ b/agb/examples/dma_effect_circular_window.rs @@ -90,7 +90,7 @@ fn main(mut gba: agb::Gba) -> ! { window.commit(); 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(); } From e91e7a84e797beb4aa283a3697570aa325dbe8cd Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Mon, 27 May 2024 13:25:52 +0100 Subject: [PATCH 4/4] Add changelog entry for less unsafe --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 095a6e51..0378cbfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. +### Changed + +- `dma.hblank_transfer` is no longer `unsafe`. + ## [0.20.2] - 2024/05/25 ### Fixed