Add correct lifetimes for Blend

This commit is contained in:
Gwilym Kuiper 2023-02-23 20:39:23 +00:00
parent 9757f7ed54
commit ce5641f5ef
2 changed files with 12 additions and 8 deletions

View file

@ -15,6 +15,8 @@
//! ``` //! ```
//! where `gba` is a mutable [Gba][crate::Gba] struct. //! where `gba` is a mutable [Gba][crate::Gba] struct.
use core::marker::PhantomData;
use crate::{fixnum::Num, memory_mapped::set_bits}; use crate::{fixnum::Num, memory_mapped::set_bits};
use super::tiled::BackgroundID; use super::tiled::BackgroundID;
@ -43,21 +45,22 @@ pub enum BlendMode {
/// Manages the blending, won't cause anything to change unless [Blend::commit] /// Manages the blending, won't cause anything to change unless [Blend::commit]
/// is called. /// is called.
pub struct Blend { pub struct Blend<'a> {
targets: u16, targets: u16,
blend_weights: u16, blend_weights: u16,
fade_weight: u16, fade_weight: u16,
phantom: PhantomData<&'a ()>,
} }
/// When making many modifications to a layer, it is convenient to operate on /// When making many modifications to a layer, it is convenient to operate on
/// that layer directly. This is created by the [Blend::layer] function and /// that layer directly. This is created by the [Blend::layer] function and
/// operates on that layer. /// operates on that layer.
pub struct BlendLayer<'blend> { pub struct BlendLayer<'blend, 'a> {
blend: &'blend mut Blend, blend: &'blend mut Blend<'a>,
layer: Layer, layer: Layer,
} }
impl BlendLayer<'_> { impl<'a> BlendLayer<'_, 'a> {
/// Set whether a background is enabled for blending on this layer. /// Set whether a background is enabled for blending on this layer.
pub fn set_background_enable(&mut self, background: BackgroundID, enable: bool) -> &mut Self { pub fn set_background_enable(&mut self, background: BackgroundID, enable: bool) -> &mut Self {
self.blend self.blend
@ -95,12 +98,13 @@ const BLEND_ALPHAS: *mut u16 = 0x0400_0052 as *mut _;
const BLEND_FADES: *mut u16 = 0x0400_0054 as *mut _; const BLEND_FADES: *mut u16 = 0x0400_0054 as *mut _;
impl Blend { impl<'a> Blend<'a> {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let blend = Self { let blend = Self {
targets: 0, targets: 0,
blend_weights: 0, blend_weights: 0,
fade_weight: 0, fade_weight: 0,
phantom: PhantomData,
}; };
blend.commit(); blend.commit();
@ -137,7 +141,7 @@ impl Blend {
/// Creates a layer object whose functions work only on that layer, /// Creates a layer object whose functions work only on that layer,
/// convenient when performing multiple operations on that layer without the /// convenient when performing multiple operations on that layer without the
/// need of specifying the layer every time. /// need of specifying the layer every time.
pub fn layer(&mut self, layer: Layer) -> BlendLayer { pub fn layer(&mut self, layer: Layer) -> BlendLayer<'_, 'a> {
BlendLayer { blend: self, layer } BlendLayer { blend: self, layer }
} }
@ -209,7 +213,7 @@ impl Blend {
} }
} }
impl Drop for Blend { impl Drop for Blend<'_> {
fn drop(&mut self) { fn drop(&mut self) {
self.reset().commit(); self.reset().commit();
} }

View file

@ -97,7 +97,7 @@ impl WindowDist {
pub struct BlendDist; pub struct BlendDist;
impl BlendDist { impl BlendDist {
pub fn get(&mut self) -> Blend { pub fn get(&mut self) -> Blend<'_> {
Blend::new() Blend::new()
} }
} }