From c997bf1990b4b8298cdd49865a8464faec607c01 Mon Sep 17 00:00:00 2001 From: bendn Date: Wed, 6 Sep 2023 19:28:53 +0700 Subject: [PATCH] Image::to_owned for &mut [T] too --- Cargo.toml | 2 +- src/lib.rs | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cacaa4..f8d388d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fimg" -version = "0.3.2" +version = "0.3.3" authors = ["bend-n "] license = "MIT" edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index 57cfa24..492bfc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,6 +136,22 @@ impl Image { } } +impl Image<&[T], CHANNELS> { + /// Allocate a new `Image>` from this imageref. + pub fn to_owned(&self) -> Image, CHANNELS> { + // SAFETY: we have been constructed already, so must be valid + unsafe { Image::new(self.width, self.height, self.buffer.to_vec()) } + } +} + +impl Image<&mut [T], CHANNELS> { + /// Allocate a new `Image>` from this mutable imageref. + pub fn to_owned(&self) -> Image, CHANNELS> { + // SAFETY: we have been constructed already, so must be valid + unsafe { Image::new(self.width, self.height, self.buffer.to_vec()) } + } +} + impl Image<&[u8], CHANNELS> { #[inline] #[must_use] @@ -159,7 +175,6 @@ impl Image<&[u8], CHANNELS> { /// let img = Image::make::<5, 5>(); /// # let img: Image<_, 4> = img; /// ``` - pub const fn make<'a, const WIDTH: u32, const HEIGHT: u32>() -> Image<&'a [u8], CHANNELS> where [(); CHANNELS * WIDTH as usize * HEIGHT as usize]: Sized, @@ -170,12 +185,6 @@ impl Image<&[u8], CHANNELS> { buffer: &[0; CHANNELS * WIDTH as usize * HEIGHT as usize], } } - - /// Allocate a new Image>. - pub fn to_owned(&self) -> Image, CHANNELS> { - // SAFETY: we have been constructed already, so must be valid - unsafe { Image::new(self.width, self.height, self.buffer.to_vec()) } - } } impl, const CHANNELS: usize> Image { @@ -266,9 +275,7 @@ impl Image<&mut [u8], CHANNELS> { // SAFETY: we got constructed okay, parameters must be valid unsafe { Image::new(self.width, self.height, self.buffer) } } -} -impl Image<&mut [u8], CHANNELS> { /// Copy this ref image pub fn copy(&mut self) -> Image<&mut [u8], CHANNELS> { #[allow(clippy::undocumented_unsafe_blocks)]