From a0f555439e01e1f4e8d5cea7ed0eaaa10583958c Mon Sep 17 00:00:00 2001 From: bendn Date: Wed, 4 Oct 2023 11:32:03 +0700 Subject: [PATCH] add Image::boxed --- benches/drawing.rs | 6 +++--- src/lib.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/benches/drawing.rs b/benches/drawing.rs index b0668d3..8375bf3 100644 --- a/benches/drawing.rs +++ b/benches/drawing.rs @@ -1,7 +1,7 @@ use fimg::*; fn tri() { - let mut i: Image<_, 4> = fimg::make!(4 channels 1000 x 1000); - i.as_mut() - .tri((0., 0.), (1000., 500.), (0., 999.), [255, 255, 255, 255]); + let mut i: Image<_, 4> = fimg::make!(4 channels 1000 x 1000).boxed(); + i.tri((0., 0.), (1000., 500.), (0., 999.), [255, 255, 255, 255]); + std::hint::black_box(i); } iai::main!(tri); diff --git a/src/lib.rs b/src/lib.rs index bb08357..40f3bbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -243,6 +243,22 @@ impl Image<&[u8], CHANNELS> { } } +impl Image<[u8; N], CHANNELS> { + /// Box this array image. + pub fn boxed(self) -> Image, CHANNELS> { + // SAFETY: ctor + unsafe { Image::new(self.width, self.height, Box::new(self.buffer)) } + } +} + +impl Image, CHANNELS> { + /// Box this owned image. + pub fn boxed(self) -> Image, CHANNELS> { + // SAFETY: ctor + unsafe { Image::new(self.width, self.height, self.buffer.into_boxed_slice()) } + } +} + #[macro_export] /// Create a [Image]<[[u8]; N], C> with ease. If your looking for a [Image]<&'static [[u8]]>, try [`Image::make`]. ///