Skip to content

Commit

Permalink
Implement DerefMut for heap array types.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAreAllTheseTaken committed Jan 25, 2024
1 parent d9b6a26 commit 5714b12
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern crate alloc;
#[cfg(feature = "rayon")]
use rayon::prelude::*;

use core::{ptr::{NonNull, self}, alloc::Layout, mem::MaybeUninit, ops::{Index, IndexMut, Deref}, borrow::{Borrow, BorrowMut}};
use core::{ptr::{NonNull, self}, alloc::Layout, mem::MaybeUninit, ops::{Index, IndexMut, Deref, DerefMut}, borrow::{Borrow, BorrowMut}};
use alloc::{boxed::Box, alloc::{alloc, handle_alloc_error}};

/// A heap allocated contiguous one dimensional array.
Expand Down Expand Up @@ -206,6 +206,12 @@ impl <T, const N: usize> Deref for HeapArray<T, N> {
}
}

impl <T, const N: usize> DerefMut for HeapArray<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}

impl <T: Default, const N: usize> Default for HeapArray<T, N> {
fn default() -> Self {
Self::from_fn(|_| T::default())
Expand Down Expand Up @@ -464,6 +470,12 @@ impl <T, const M: usize, const N: usize> Deref for HeapArray2D<T, M, N> {
}
}

impl <T, const M: usize, const N: usize> DerefMut for HeapArray2D<T, M, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}

impl <T: Default, const M: usize, const N: usize> Default for HeapArray2D<T, M, N> {
fn default() -> Self {
Self::from_fn(|_, _| T::default())
Expand Down Expand Up @@ -734,6 +746,12 @@ impl <T, const L: usize, const M: usize, const N: usize> Deref for HeapArray3D<T
}
}

impl <T, const L: usize, const M: usize, const N: usize> DerefMut for HeapArray3D<T, L, M, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}

impl <T: Default, const L: usize, const M: usize, const N: usize> Default for HeapArray3D<T, L, M, N> {
fn default() -> Self {
Self::from_fn(|_, _, _| T::default())
Expand Down

0 comments on commit 5714b12

Please sign in to comment.