diff --git a/Cargo.toml b/Cargo.toml index a705078..434eebe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ license = "MIT" [dev-dependencies] criterion = { version = "0.4.0", features = ["html_reports"] } -compiletest_rs = "0.10.0" [features] default = ["alloc"] diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index c64ecbd..3b73270 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -321,9 +321,16 @@ impl AllocRingBuffer { #[must_use] pub fn new(capacity: usize) -> Self { assert_ne!(capacity, 0, "Capacity must be greater than 0"); - let size = capacity.next_power_of_two(); - let layout = alloc::alloc::Layout::array::(size).unwrap(); - let buf = unsafe { alloc::alloc::alloc(layout).cast() }; + let size = capacity + .checked_next_power_of_two() + .expect("Capacity is too large"); + let layout = alloc::alloc::Layout::array::(size).expect("Capacity is too large"); + #[cfg(target_pointer_width = "64")] + assert!(layout.size() <= (1usize << 40), "Capacity is too large"); + let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() }; + if buf.is_null() { + alloc::alloc::handle_alloc_error(layout); + } Self { buf, size, diff --git a/tests/compile-fail/test_const_generic_array_zero_length.rs b/tests/compile-fail/test_const_generic_array_zero_length.rs deleted file mode 100644 index 3b69f1a..0000000 --- a/tests/compile-fail/test_const_generic_array_zero_length.rs +++ /dev/null @@ -1,9 +0,0 @@ -extern crate ringbuffer; - -use ringbuffer::ConstGenericRingBuffer; - -fn main() { - let _ = ConstGenericRingBuffer::::new(); - //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` - // ringbuffer can't be zero length -} diff --git a/tests/compile-fail/test_const_generic_array_zero_length_new.rs b/tests/compile-fail/test_const_generic_array_zero_length_new.rs deleted file mode 100644 index bdaddbf..0000000 --- a/tests/compile-fail/test_const_generic_array_zero_length_new.rs +++ /dev/null @@ -1,10 +0,0 @@ -extern crate ringbuffer; - -use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; - -fn main() { - let mut buf = ConstGenericRingBuffer::new::<0>(); - //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` - // ringbuffer can't be zero length -let _ = buf.enqueue(5); -} diff --git a/tests/compiletests.rs b/tests/compiletests.rs deleted file mode 100644 index f48163e..0000000 --- a/tests/compiletests.rs +++ /dev/null @@ -1,23 +0,0 @@ -extern crate compiletest_rs as compiletest; - -use std::path::PathBuf; - -#[cfg(test)] -mod conversions; - -fn run_mode(mode: &'static str) { - let mut config = compiletest::Config::default(); - - config.mode = mode.parse().expect("Invalid mode"); - config.src_base = PathBuf::from(format!("tests/{}", mode)); - config.link_deps(); // Populate config.target_rustcflags with dependencies on the path - config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464 - - compiletest::run_tests(&config); -} - -#[test] -#[cfg_attr(miri, ignore)] -fn compile_test() { - run_mode("compile-fail"); -}