diff --git a/src/lib.rs b/src/lib.rs index 70e880a..55d0d01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,7 +186,7 @@ macro_rules! heap { let mut ha = $crate::HeapArray::new_uninit(num); for x in 0..num { - ha[x] = $value; + ha.replace_and_forget(x, $value); } ha @@ -199,7 +199,7 @@ macro_rules! heap { let fp = 0; $( let fp = fp + 1; - ha[fp-1] = $n; + ha.replace_and_forget(fp-1, $n); )* } ha @@ -405,6 +405,46 @@ impl HeapArray std::mem::forget(self); output } + + pub fn iter<'a>(&'a self) -> slice::Iter<'a, T> + { + self.as_slice().iter() + } + pub fn iter_mut<'a>(&'a mut self) -> slice::IterMut<'a, T> + { + self.as_slice_mut().iter_mut() + } + + pub fn replace_and_forget(&mut self, index: usize, value: T) + { + assert!(index Self + where T: Clone + { + let mut output = Self::new_uninit(self.len()); + output.drop_check = self.drop_check; + + unsafe { + for (i,x) in (0..self.len()).zip(self.iter()) + { + ptr::put(output.as_ptr_mut().offset(i as isize), x.clone()); + } + } + output + } + pub unsafe fn clone_mem(&self) -> Self + { + let mut output = Self::new_uninit(self.len()); + output.drop_check = self.drop_check; + ptr::memcpy(output.ptr as VoidPointer, self.ptr as VoidPointer, self.len_bytes()); + + output + } } impl Index for HeapArray diff --git a/src/ptr.rs b/src/ptr.rs index e4124bf..7dba74e 100644 --- a/src/ptr.rs +++ b/src/ptr.rs @@ -33,6 +33,10 @@ pub unsafe fn take(ptr: *mut T) -> T { mem::replace(&mut *ptr, MaybeUninit::zeroed().assume_init()) } +pub unsafe fn put(ptr: *mut T, value: T) +{ + mem::forget(mem::replace(&mut *ptr, value)); +} pub unsafe fn memcpy(dst: VoidPointer, src: ConstVoidPointer, size: usize) -> VoidPointer {