From 59bf6362365e217b9af75a07a8bd588600cc7977 Mon Sep 17 00:00:00 2001 From: bendn Date: Fri, 27 Oct 2023 12:32:04 +0700 Subject: [PATCH] add a leak method --- Cargo.toml | 2 +- src/lib.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0de3b77..f21479a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fimg" -version = "0.4.17" +version = "0.4.19" authors = ["bend-n "] license = "MIT" edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index cd82ba8..56fd2a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -475,6 +475,22 @@ impl Image, CHANNELS> { buffer: vec![0; CHANNELS * width as usize * height as usize], } } + + /// Consumes and leaks this image, returning a reference to the image. + #[must_use = "not using the returned reference is a memory leak"] + pub fn leak(self) -> Image<&'static mut [u8], CHANNELS> { + // SAFETY: ctor + unsafe { Image::new(self.width, self.height, self.buffer.leak()) } + } +} + +impl Image, CHANNELS> { + /// Consumes and leaks this image, returning a reference to the image. + #[must_use = "not using the returned reference is a memory leak"] + pub fn leak(self) -> Image<&'static mut T, CHANNELS> { + // SAFETY: ctor + unsafe { Image::new(self.width, self.height, Box::leak(self.buffer)) } + } } /// helper macro for defining the save() method.