Skip to content

Commit

Permalink
Run miri, and unchecked mul
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtgs committed Jul 7, 2024
1 parent cb1ec08 commit 0098fb0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "heap-array"
version = "0.1.8"
version = "0.1.9"
edition = "2021"
authors = ["NightMare-Vortex"]
description = "An Implementation of a variable length array, with its main benefit over `Vec` is taking up less space"
Expand All @@ -20,5 +20,5 @@ allocator-api = []
[dependencies]
serde = { version = "1", optional = true, default-features = false, features=["alloc"] }
simd-json-derive = { version = "0.13.0", optional = true }
simd-json = { version = "0.13.9", optional = true }
simd-json = { version = "0.13.10", optional = true }
likely_stable = "0.1.2"
10 changes: 6 additions & 4 deletions src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ impl<T> Guard<T> {
macro_rules! drop_impl {
() => {
fn drop(&mut self) {
// we shouldn't create a guard for 0 elements
unsafe { assume!(self.len != 0) };

if mem::needs_drop::<T>() {
unsafe {
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(
Expand All @@ -72,12 +75,11 @@ macro_rules! drop_impl {
));
}
}
unsafe { assume!(self.len != 0) };


// size is always less than isize::MAX we checked that already
// By using Layout::array::<T> to allocate

// But.. unchecked mul is unstable, update when stable
let size = mem::size_of::<T>() * self.len;
let size = unsafe { mem::size_of::<T>().unchecked_mul(self.len) };
let align = mem::align_of::<T>();

unsafe {
Expand Down
19 changes: 9 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extern crate alloc;
use core::{
ptr::{self, NonNull},
fmt::{Debug, Formatter},
mem::{self, ManuallyDrop, MaybeUninit, forget},
mem::{self, ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut, ControlFlow},
cmp::Ordering,
slice::{Iter, IterMut},
Expand All @@ -62,8 +62,7 @@ use alloc::alloc::{Allocator, Global};
use core::slice::SliceIndex;
use core::ops::{Index, IndexMut};
use core::hash::{Hash, Hasher};

use likely_stable::{unlikely};
use likely_stable::unlikely;
use crate::guard::Guard;
use crate::try_me::{NeverShortCircuit, Try};

Expand Down Expand Up @@ -227,7 +226,7 @@ macro_rules! from_array_impl {
return unsafe { Self::from_raw_parts(NonNull::dangling(), $N) };
}

let mut ptr: NonNull<MaybeUninit<$T>> = match alloc_uninit($N $(, &$alloc)?) {
let ptr: NonNull<MaybeUninit<$T>> = match alloc_uninit($N $(, &$alloc)?) {
Some(ptr) => ptr,
None => {
#[cfg(feature = "allocator-api")]
Expand All @@ -238,8 +237,8 @@ macro_rules! from_array_impl {
}
};

unsafe { ptr::copy_nonoverlapping($array.as_ptr(), ptr.as_mut().as_mut_ptr(), $N) }
forget($array);
let array = ManuallyDrop::new($array);
unsafe { ptr::copy_nonoverlapping(array.as_ptr(), ptr.as_ptr() as *mut $T, $N) }

#[cfg(feature = "allocator-api")]
return unsafe { Self::from_raw_parts_in(ptr.cast(), $N, $($alloc)?) };
Expand Down Expand Up @@ -448,6 +447,7 @@ impl_heap_array! {
///
/// ```
/// # use heap_array::heap_array;
/// # if cfg!(miri) { std::process::exit(0) } // miri doesn't like the leak
/// let x = heap_array![1, 2, 3];
/// let static_ref: &'static mut [usize] = x.leak();
/// static_ref[0] += 1;
Expand All @@ -474,6 +474,7 @@ impl_heap_array! {
///
/// ```
/// # use heap_array::heap_array;
/// # if cfg!(miri) { std::process::exit(0) } // miri doesn't like the leak
/// let x = heap_array![1, 2, 3];
/// let static_ref: &'static mut [usize] = x.leak();
/// static_ref[0] += 1;
Expand Down Expand Up @@ -1001,7 +1002,7 @@ impl_heap_array! {
{
// We use vec![] rather than Self::from_fn(len, |_| element.clone())
// as it has specialization traits for manny things Such as zero initialization
// as well as avoid an extra copy (caused by not using element except for cloning)
// as well as avoid an extra clone (caused by not using element except for cloning)
vec::from_elem(element, len).into()
}

Expand Down Expand Up @@ -1048,9 +1049,7 @@ impl_heap_array! {
if self.len != 0 {
// size is always less than isize::MAX we checked that already
// By using Layout::array::<T> to allocate
// but unchecked math is unstable
// change when stable
let size = mem::size_of::<T>() * self.len;
let size = mem::size_of::<T>().unchecked_mul(self.len);
let align = mem::align_of::<T>();

let layout = Layout::from_size_align_unchecked(size, align);
Expand Down

0 comments on commit 0098fb0

Please sign in to comment.