Skip to content

Commit

Permalink
Update fuzz tests, add nostd feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AljoschaMeyer committed Jul 6, 2024
1 parent d182e96 commit a660811
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ exclude = [
"fuzz/*",
".github/workflows/rust.yml",
]

[features]

default = ["std"]

# Provide functionality that relies on the std library. Enabled by default.
std = []

# Provide functionality that relies on dynamic memory allocation. Enabling `std` automatically enables all `alloc` features.
alloc = []
10 changes: 1 addition & 9 deletions fuzz/fuzz_targets/fixed_bulk.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![no_main]

use core::mem::MaybeUninit;
use core::slice;

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;

Expand All @@ -19,11 +16,6 @@ enum Operation<T> {
BulkDequeue(u8),
}

fn maybe_uninit_slice_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] {
let ptr = slice.as_mut_ptr().cast::<MaybeUninit<T>>();
unsafe { slice::from_raw_parts_mut(ptr, slice.len()) }
}

fuzz_target!(|data: (Vec<Operation<u8>>, usize)| {
let operations = data.0;
let capacity = data.1;
Expand Down Expand Up @@ -70,7 +62,7 @@ fuzz_target!(|data: (Vec<Operation<u8>>, usize)| {
let mut test_buffer = vec![];
test_buffer.resize(n, 0_u8);

let test_amount = test.bulk_dequeue(maybe_uninit_slice_mut(&mut test_buffer));
let test_amount = test.bulk_dequeue(&mut test_buffer);
for _ in 0..test_amount {
if let Some(item) = control.pop_front() {
control_buffer.push(item.clone());
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@
//!
//! ## Queue Implementations
//!
//! So far, there is only a single implementation: [`Fixed`], which is a heap-allocated ring-buffer of unchanging capacity.
//! So far, there is only a single implementation: [`Fixed`], which is a heap-allocated ring-buffer of unchanging capacity. It is gated behind the `std` or `alloc` feature, the prior of which is enabled by default.
//!
//! Future plans include a queue of static (known at compile-time) capacity that can be used in allocator-less environments, and an elastic queue that grows and shrinks its capacity within certain parameters, to free up memory under low load.
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(any(feature = "std", feature = "alloc"))]
mod fixed;
#[cfg(any(feature = "std", feature = "alloc"))]
pub use fixed::Fixed;

use core::cmp::min;
use core::mem::MaybeUninit;

pub use fixed::Fixed;

/// A first-in-first-out queue. Provides methods for bulk transfer of items similar to [ufotofu](https://crates.io/crates/ufotofu) [`BulkProducer`](https://docs.rs/ufotofu/0.1.0/ufotofu/sync/trait.BulkProducer.html)s and [`BulkConsumer`](https://docs.rs/ufotofu/0.1.0/ufotofu/sync/trait.BulkConsumer.html)s.
pub trait Queue {
Expand Down

0 comments on commit a660811

Please sign in to comment.