//! Wrappers around `Box<[u8; LEN]>` and similar types with a custom `bincode::Decode` //! implementation that deserializes directly into heap memory. //! //! This exists because the implementation that `#[derive(Decode)]` generates for `Box<[u8; LEN]>` //! deserializes into stack memory and then moves to the heap, which is problematic when deserializing //! large arrays (particularly on Windows). //! //! For types other than `u8`, these implementations also feature significantly more efficient //! `bincode::Encode` and `bincode::Decode` implementations than what bincode's derive macros //! would produce. use crate::frontend::Color; use bincode::de::read::Reader; use bincode::de::{BorrowDecoder, Decoder}; use bincode::enc::Encoder; use bincode::enc::write::Writer; use bincode::error::{DecodeError, EncodeError}; use bincode::{BorrowDecode, Decode, Encode}; use bytemuck::Pod; use rand::distr::StandardUniform; use rand::prelude::Distribution; use std::fmt::Debug; use std::ops::{Deref, DerefMut}; #[derive(Debug, Clone)] pub struct BoxedArray(Box<[T; LEN]>); impl Default for BoxedArray { fn default() -> Self { Self::new() } } impl BoxedArray { #[must_use] #[allow(clippy::missing_panics_doc)] pub fn new() -> Self { Self(vec![T::default(); LEN].into_boxed_slice().try_into().unwrap()) } } impl BoxedArray where StandardUniform: Distribution, { #[must_use] pub fn new_random() -> Self { let mut array = Self::new(); array.fill_with(rand::random); array } } impl From> for BoxedArray { fn from(value: Box<[T; LEN]>) -> Self { Self(value) } } impl From> for Box<[T; LEN]> { fn from(value: BoxedArray) -> Self { value.0 } } impl Deref for BoxedArray { type Target = Box<[T; LEN]>; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for BoxedArray { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Encode for BoxedArray { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { // This is _significantly_ faster than the #[derive(Encode)] implementation for non-u8 types let bytes = bytemuck::cast_slice(self.0.as_slice()); encoder.writer().write(bytes) } } impl Decode for BoxedArray { fn decode>(decoder: &mut D) -> Result { // Similarly, this is _significantly_ faster than the #[derive(Decode)] implementation for non-u8 types let mut array = vec![T::default(); LEN]; decoder.reader().read(bytemuck::cast_slice_mut(&mut array))?; Ok(Self(array.into_boxed_slice().try_into().unwrap())) } } impl<'de, T: Debug + Default + Copy + Pod, Context, const LEN: usize> BorrowDecode<'de, Context> for BoxedArray { fn borrow_decode>( decoder: &mut D, ) -> Result { Self::decode(decoder) } } pub type BoxedByteArray = BoxedArray; pub type BoxedWordArray = BoxedArray; pub type BoxedColorArray = BoxedArray; #[derive(Debug, Clone)] pub struct Boxed2DArray(Box<[[T; COLS]; ROWS]>); impl Default for Boxed2DArray { fn default() -> Self { Self::new() } } impl Boxed2DArray { #[must_use] pub fn new() -> Self { // SAFETY: Memory is fully filled with T::default() before calling assume_init() // Total allocation length is (ROWS * COLS * size_of::()), and it is only accessed through // a *mut T with offset strictly less than (ROWS * COLS) unsafe { let mut array = Box::<[[T; COLS]; ROWS]>::new_uninit(); let ptr = array.as_mut_ptr().cast::(); for i in 0..ROWS * COLS { ptr.add(i).write(T::default()); } Self(array.assume_init()) } } } impl From> for Boxed2DArray { fn from(value: Box<[[T; COLS]; ROWS]>) -> Self { Self(value) } } impl From> for Box<[[T; COLS]; ROWS]> { fn from(value: Boxed2DArray) -> Self { value.0 } } impl Deref for Boxed2DArray { type Target = Box<[[T; COLS]; ROWS]>; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Boxed2DArray { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Encode for Boxed2DArray { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { let writer = encoder.writer(); for row in self.as_slice() { writer.write(bytemuck::cast_slice(row))?; } Ok(()) } } impl Decode for Boxed2DArray { fn decode>(decoder: &mut D) -> Result { let mut array = Self::new(); let reader = decoder.reader(); for row in array.as_mut_slice() { reader.read(bytemuck::cast_slice_mut(row))?; } Ok(array) } } impl<'de, T: Debug + Default + Copy + Pod, Context, const ROWS: usize, const COLS: usize> BorrowDecode<'de, Context> for Boxed2DArray { fn borrow_decode>( decoder: &mut D, ) -> Result { Self::decode(decoder) } } pub type Boxed2DWordArray = Boxed2DArray; #[cfg(test)] mod tests { use super::*; // Test should be run with miri: // $ cargo +nightly miri test -p jgenesis-common #[test] fn new_boxed_2d_array() { let array: Boxed2DArray = Boxed2DArray::new(); for row in array.as_slice() { for &color in row { assert_eq!(color, Color::default()); } } } }