Skip to content

Commit

Permalink
Add check to make sure we don't exceed maximum allows size
Browse files Browse the repository at this point in the history
  • Loading branch information
Amjad50 committed Feb 22, 2024
1 parent 5b6fb48 commit d753319
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ use core::{

#[cfg(loom)]
use loom::sync::{
atomic::{AtomicU64, AtomicUsize, Ordering},
atomic::{AtomicUsize, Ordering},
Arc,
};

Expand Down Expand Up @@ -483,6 +483,13 @@ impl<T: Clone, const N: usize> Clone for Receiver<T, N> {
/// # });
/// ```
pub fn channel<T: Clone, const N: usize>() -> (Sender<T, N>, Receiver<T, N>) {
// TODO: replace with compile time assert
assert!(
N <= MAX_LEN,
"The buffer size must be less than {}",
MAX_LEN
);

let queue = Arc::new(InnerChannel::<T, N>::new());
(
Sender {
Expand Down
8 changes: 8 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ macro_rules! loom {
};
}

#[test]
#[should_panic]
fn test_channel_too_large() {
loom!({
let _ = channel::<i32, { MAX_LEN + 1 }>();
});
}

#[test]
fn test_push_pop() {
loom!({
Expand Down

0 comments on commit d753319

Please sign in to comment.