fast image operations
add assume init
bendn 2025-01-31
parent b23372e · commit 8e2f747
-rw-r--r--src/indexed.rs15
-rw-r--r--src/lib.rs1
2 files changed, 15 insertions, 1 deletions
diff --git a/src/indexed.rs b/src/indexed.rs
index dfa00ac..426565d 100644
--- a/src/indexed.rs
+++ b/src/indexed.rs
@@ -2,6 +2,8 @@
#![allow(private_bounds)]
mod builder;
+use std::mem::MaybeUninit;
+
use crate::Image;
#[allow(non_camel_case_types)]
@@ -40,7 +42,8 @@ impl<I, P> IndexedImage<I, P> {
self.buffer.map(|x| {
x.as_ref()
.iter()
- .flat_map(|x| self.palette.as_ref()[x.nat()])
+ // SAFETY: invariant.
+ .flat_map(|x| *self.palette.as_ref().get_unchecked(x.nat()))
.collect()
})
}
@@ -104,3 +107,13 @@ impl<I, P> IndexedImage<I, P> {
.ok_or("not all indexes are in palette")
}
}
+impl<P, I: uint> IndexedImage<Box<[MaybeUninit<I>]>, P> {
+ /// Assumes this MU image is, in fact, initialized. You must be very sure that it is. Also, none of the pixels can be out of bounds.
+ pub unsafe fn assume_init(self) -> IndexedImage<Box<[I]>, P> {
+ IndexedImage {
+ // SAFETY: it really isnt.
+ buffer: unsafe { self.buffer.mapped(|x| x.assume_init()) },
+ ..self
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 14ae1e5..bbc83c4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -51,6 +51,7 @@
//! - `default`: \[`save`, `scale`\].
#![cfg_attr(all(feature = "term", windows), windows_subsystem = "console")]
#![feature(
+ type_changing_struct_update,
maybe_uninit_write_slice,
hint_assert_unchecked,
slice_swap_unchecked,