Mut::array

This commit is contained in:
Intege-rs
2025-04-11 13:39:23 -04:00
parent 11352fc0ad
commit e0165ab294

View File

@@ -31,7 +31,7 @@ unsafe impl<T> Sync for Mut<T> {}
unsafe impl<T> Send for Mut<T> {} unsafe impl<T> Send for Mut<T> {}
impl<T: Sized> Mut<T> { impl<T: Sized> Mut<T> {
#[inline(always)]#[inline(always)] #[inline(always)]
pub const fn new(value: T) -> Self { pub const fn new(value: T) -> Self {
Self(core::cell::UnsafeCell::new(value)) Self(core::cell::UnsafeCell::new(value))
} }
@@ -41,6 +41,25 @@ impl<T: Sized> Mut<T> {
} }
} }
impl<T: Sized + Copy> Mut<T> {
#[inline(always)]
pub const fn array<const S: usize>(value: T) -> [Self;S] {
unsafe {
let mut a = MaybeUninit::<[Mut<T>;S]>::uninit();
let mut i = 0usize;
while i < S {
a.as_mut_ptr()
.cast::<Mut<T>>()
.add(i)
.write(Mut::new(value));
i+= 1;
}
a.assume_init()
}
}
}
impl<T: Pod> Mut<T> { impl<T: Pod> Mut<T> {
#[inline(always)] pub unsafe fn set(&self, value: T) { unsafe { core::ptr::write(self.as_ptr(), value) } } #[inline(always)] pub unsafe fn set(&self, value: T) { unsafe { core::ptr::write(self.as_ptr(), value) } }
#[inline(always)] pub fn get(&self) -> T { unsafe { core::ptr::read(self.as_ptr()) } } #[inline(always)] pub fn get(&self) -> T { unsafe { core::ptr::read(self.as_ptr()) } }