clone_mem(); improve macro

master
Avril 5 years ago
parent 1e4d6dd5d6
commit 2b6e221808
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -186,7 +186,7 @@ macro_rules! heap {
let mut ha = $crate::HeapArray::new_uninit(num); let mut ha = $crate::HeapArray::new_uninit(num);
for x in 0..num { for x in 0..num {
ha[x] = $value; ha.replace_and_forget(x, $value);
} }
ha ha
@ -199,7 +199,7 @@ macro_rules! heap {
let fp = 0; let fp = 0;
$( $(
let fp = fp + 1; let fp = fp + 1;
ha[fp-1] = $n; ha.replace_and_forget(fp-1, $n);
)* )*
} }
ha ha
@ -405,6 +405,46 @@ impl<T> HeapArray<T>
std::mem::forget(self); std::mem::forget(self);
output 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.len());
unsafe {
ptr::put(self.as_ptr_mut().offset(index as isize), value);
}
}
pub fn clone(&self) -> 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<T, I> Index<I> for HeapArray<T> impl<T, I> Index<I> for HeapArray<T>

@ -33,6 +33,10 @@ pub unsafe fn take<T>(ptr: *mut T) -> T
{ {
mem::replace(&mut *ptr, MaybeUninit::zeroed().assume_init()) mem::replace(&mut *ptr, MaybeUninit::zeroed().assume_init())
} }
pub unsafe fn put<T>(ptr: *mut T, value: T)
{
mem::forget(mem::replace(&mut *ptr, value));
}
pub unsafe fn memcpy(dst: VoidPointer, src: ConstVoidPointer, size: usize) -> VoidPointer pub unsafe fn memcpy(dst: VoidPointer, src: ConstVoidPointer, size: usize) -> VoidPointer
{ {

Loading…
Cancel
Save