Add lifetime to SliceLayerElement

This commit is contained in:
Connor Slade
2024-11-23 18:45:32 -05:00
parent fc2054b7ca
commit 903f8b8876
2 changed files with 11 additions and 10 deletions

View File

@@ -76,7 +76,7 @@
- [ ] Merge goo_format changes into goo crate - [ ] Merge goo_format changes into goo crate
- [ ] Implement .ctb format (see <https://github.com/cbiffle/catibo/blob/master/doc/cbddlp-ctb.adoc>) - [ ] Implement .ctb format (see <https://github.com/cbiffle/catibo/blob/master/doc/cbddlp-ctb.adoc>)
- [x] Generic format system - [x] Generic format system
- [ ] Fix plugins / post processes - [x] Fix plugins / post processes
- [ ] Undo / Redo - [ ] Undo / Redo
- [x] Close file menu if button clicked - [x] Close file menu if button clicked
- [x] Allow dragging in project to load them - [x] Allow dragging in project to load them

View File

@@ -1,6 +1,7 @@
// yes, i know this is jank as hell // yes, i know this is jank as hell
use std::{ use std::{
marker::PhantomData,
mem, mem,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
}; };
@@ -15,15 +16,17 @@ pub struct SliceLayerIterator<'a> {
pub(crate) layers: usize, pub(crate) layers: usize,
} }
pub struct SliceLayerElement { pub struct SliceLayerElement<'a> {
image: GrayImage, image: GrayImage,
file: *mut FormatSliceFile, file: *mut FormatSliceFile,
layer: usize, layer: usize,
_lifetime: PhantomData<&'a ()>,
} }
impl<'a> Iterator for SliceLayerIterator<'a> { impl<'a> Iterator for SliceLayerIterator<'a> {
type Item = SliceLayerElement; type Item = SliceLayerElement<'a>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.layer >= self.layers { if self.layer >= self.layers {
@@ -37,24 +40,22 @@ impl<'a> Iterator for SliceLayerIterator<'a> {
image, image,
file: self.file as *mut _, file: self.file as *mut _,
layer: self.layer - 1, layer: self.layer - 1,
_lifetime: PhantomData,
}) })
} }
} }
impl Drop for SliceLayerElement { impl<'a> Drop for SliceLayerElement<'a> {
fn drop(&mut self) { fn drop(&mut self) {
// SAFETY: it's not... But the idea is that each SliceLayerElement will // SAFETY: it's not... But the idea is that each SliceLayerElement will
// only be writing to one layer each, meaning the same memory will only // only be writing to one layer each, meaning the same memory will only
// be mutably borrowed once. // be mutably borrowed once.
//
// You could easily keep one of these objects alive after the slice
// layer iter is dropped, but don't please.
let file = unsafe { &mut *self.file }; let file = unsafe { &mut *self.file };
file.overwrite_layer(self.layer, mem::take(&mut self.image)); file.overwrite_layer(self.layer, mem::take(&mut self.image));
} }
} }
impl Deref for SliceLayerElement { impl<'a> Deref for SliceLayerElement<'a> {
type Target = GrayImage; type Target = GrayImage;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@@ -62,10 +63,10 @@ impl Deref for SliceLayerElement {
} }
} }
impl DerefMut for SliceLayerElement { impl<'a> DerefMut for SliceLayerElement<'a> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.image &mut self.image
} }
} }
unsafe impl Send for SliceLayerElement {} unsafe impl<'a> Send for SliceLayerElement<'a> {}