From 5139fa82f787b368e4c22a576a947adce38cdffb Mon Sep 17 00:00:00 2001 From: bendn Date: Wed, 6 Sep 2023 18:36:44 +0700 Subject: [PATCH] add make() --- Cargo.toml | 2 +- src/lib.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60b0735..5a3e147 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fimg" -version = "0.3.0" +version = "0.3.1" authors = ["bend-n "] license = "MIT" edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index f0a7dc7..79eb558 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,11 @@ //! Provides fast image operations, such as rotation, flipping, and overlaying. #![feature( slice_swap_unchecked, + generic_const_exprs, slice_as_chunks, unchecked_math, portable_simd, + const_option, array_chunks, test )] @@ -19,7 +21,7 @@ clippy::dbg_macro, missing_docs )] -#![allow(clippy::zero_prefixed_literal)] +#![allow(clippy::zero_prefixed_literal, incomplete_features)] use std::{num::NonZeroU32, slice::SliceIndex}; @@ -140,6 +142,29 @@ impl Image<&[u8], CHANNELS> { buffer: self.buffer, } } + + /// Create a new immutable image of width x, y. + /// + /// # Panics + /// + /// if width || height == 0 + /// + /// ``` + /// # use fimg::Image; + /// 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, + { + Image { + width: NonZeroU32::new(WIDTH).expect("passed zero width to builder"), + height: NonZeroU32::new(HEIGHT).expect("passed zero height to builder"), + buffer: &[0; CHANNELS * WIDTH as usize * HEIGHT as usize], + } + } } impl, const CHANNELS: usize> Image {