Skip to content

Commit

Permalink
Remove unneeded const generic for alloc
Browse files Browse the repository at this point in the history
Since its allocated anyway at runtime, there is no need for the extra syntax, and this makes it harder to initialize.
  • Loading branch information
Amjad50 committed Feb 22, 2024
1 parent 2469fe9 commit 271a566
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 49 deletions.
61 changes: 30 additions & 31 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! # #[cfg(not(loom))]
//! # {
//! use blinkcast::alloc::channel;
//! let (sender, mut receiver) = channel::<i32, 4>();
//! let (sender, mut receiver) = channel::<i32>(4);
//! sender.send(1);
//! assert_eq!(receiver.recv(), Some(1));
//!
Expand Down Expand Up @@ -51,15 +51,17 @@ use alloc::sync::Arc;
#[cfg(loom)]
use loom::sync::Arc;

struct InnerChannel<T, const N: usize> {
struct InnerChannel<T> {
buffer: Box<[Node<T>]>,
head: AtomicUsize,
}

impl<T: Clone, const N: usize> InnerChannel<T, N> {
fn new() -> Self {
let mut buffer = Vec::with_capacity(N);
for _ in 0..N {
impl<T: Clone> InnerChannel<T> {
fn new(size: usize) -> Self {
assert!(size <= MAX_LEN, "Exceeded the maximum length");

let mut buffer = Vec::with_capacity(size);
for _ in 0..size {
buffer.push(Default::default());
}
let buffer = buffer.into_boxed_slice();
Expand Down Expand Up @@ -91,7 +93,7 @@ impl<T: Clone, const N: usize> InnerChannel<T, N> {
/// # {
/// use blinkcast::alloc::channel;
///
/// let (sender, mut receiver) = channel::<i32, 4>();
/// let (sender, mut receiver) = channel::<i32>(4);
///
/// sender.send(1);
/// let sender2 = sender.clone();
Expand All @@ -108,7 +110,7 @@ impl<T: Clone, const N: usize> InnerChannel<T, N> {
/// # {
/// use blinkcast::alloc::Sender;
///
/// let sender = Sender::<i32, 4>::new();
/// let sender = Sender::<i32>::new(4);
///
/// let mut receiver = sender.new_receiver();
///
Expand All @@ -119,14 +121,14 @@ impl<T: Clone, const N: usize> InnerChannel<T, N> {
/// assert_eq!(receiver.recv(), None);
/// # }
/// ```
pub struct Sender<T, const N: usize> {
queue: Arc<InnerChannel<T, N>>,
pub struct Sender<T> {
queue: Arc<InnerChannel<T>>,
}

unsafe impl<T: Clone + Send, const N: usize> Send for Sender<T, N> {}
unsafe impl<T: Clone + Send, const N: usize> Sync for Sender<T, N> {}
unsafe impl<T: Clone + Send> Send for Sender<T> {}
unsafe impl<T: Clone + Send> Sync for Sender<T> {}

impl<T: Clone, const N: usize> Sender<T, N> {
impl<T: Clone> Sender<T> {
/// Sends a message to the channel.
/// If the channel is full, the oldest message will be overwritten.
/// So the receiver must be quick or it will lose the old data.
Expand All @@ -136,12 +138,9 @@ impl<T: Clone, const N: usize> Sender<T, N> {

/// Creates a new channel with a buffer of size `N`.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
// TODO: use const_assert to check if N is a power of 2
assert!(N <= MAX_LEN, "Exceeded the maximum length");

pub fn new(size: usize) -> Self {
Self {
queue: Arc::new(InnerChannel::<T, N>::new()),
queue: Arc::new(InnerChannel::<T>::new(size)),
}
}

Expand All @@ -153,7 +152,7 @@ impl<T: Clone, const N: usize> Sender<T, N> {
/// # {
/// use blinkcast::alloc::Sender;
///
/// let sender = Sender::<i32, 4>::new();
/// let sender = Sender::<i32>::new(4);
///
/// sender.send(1);
///
Expand All @@ -165,7 +164,7 @@ impl<T: Clone, const N: usize> Sender<T, N> {
/// assert_eq!(receiver.recv(), None);
/// # }
/// ```
pub fn new_receiver(&self) -> Receiver<T, N> {
pub fn new_receiver(&self) -> Receiver<T> {
let head = self.queue.head.load(Ordering::Relaxed);
let (lap, index) = unpack_data_index(head);

Expand All @@ -176,7 +175,7 @@ impl<T: Clone, const N: usize> Sender<T, N> {
}
}

impl<T, const N: usize> Clone for Sender<T, N> {
impl<T> Clone for Sender<T> {
fn clone(&self) -> Self {
Self {
queue: self.queue.clone(),
Expand All @@ -198,7 +197,7 @@ impl<T, const N: usize> Clone for Sender<T, N> {
/// # #[cfg(not(loom))]
/// # {
/// use blinkcast::alloc::channel;
/// let (sender, mut receiver) = channel::<i32, 4>();
/// let (sender, mut receiver) = channel::<i32>(4);
/// sender.send(1);
/// assert_eq!(receiver.recv(), Some(1));
///
Expand All @@ -215,15 +214,15 @@ impl<T, const N: usize> Clone for Sender<T, N> {
/// assert_eq!(receiver2.recv(), None);
/// # }
/// ```
pub struct Receiver<T, const N: usize> {
queue: Arc<InnerChannel<T, N>>,
pub struct Receiver<T> {
queue: Arc<InnerChannel<T>>,
reader: ReaderData,
}

unsafe impl<T: Clone + Send, const N: usize> Send for Receiver<T, N> {}
unsafe impl<T: Clone + Send, const N: usize> Sync for Receiver<T, N> {}
unsafe impl<T: Clone + Send> Send for Receiver<T> {}
unsafe impl<T: Clone + Send> Sync for Receiver<T> {}

impl<T: Clone, const N: usize> Receiver<T, N> {
impl<T: Clone> Receiver<T> {
/// Receives a message from the channel.
///
/// If there is no message available, this method will return `None`.
Expand All @@ -232,7 +231,7 @@ impl<T: Clone, const N: usize> Receiver<T, N> {
}
}

impl<T: Clone, const N: usize> Clone for Receiver<T, N> {
impl<T: Clone> Clone for Receiver<T> {
fn clone(&self) -> Self {
Self {
queue: self.queue.clone(),
Expand All @@ -252,7 +251,7 @@ impl<T: Clone, const N: usize> Clone for Receiver<T, N> {
/// # #[cfg(not(loom))]
/// # {
/// use blinkcast::alloc::channel;
/// let (sender, mut receiver) = channel::<i32, 4>();
/// let (sender, mut receiver) = channel::<i32>(4);
///
/// sender.send(1);
/// sender.send(2);
Expand All @@ -271,8 +270,8 @@ impl<T: Clone, const N: usize> Clone for Receiver<T, N> {
/// assert_eq!(receiver2.recv(), None);
/// # }
/// ```
pub fn channel<T: Clone, const N: usize>() -> (Sender<T, N>, Receiver<T, N>) {
let sender = Sender::<T, N>::new();
pub fn channel<T: Clone>(size: usize) -> (Sender<T>, Receiver<T>) {
let sender = Sender::<T>::new(size);
let receiver = sender.new_receiver();
(sender, receiver)
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! # {
//! use blinkcast::alloc::channel;
//!
//! let (sender, mut receiver1) = channel::<i32, 4>();
//! let (sender, mut receiver1) = channel::<i32>(4);
//! sender.send(1);
//! sender.send(2);
//!
Expand All @@ -48,7 +48,7 @@
//! # {
//! use blinkcast::alloc::channel;
//! use std::thread;
//! let (sender1, mut receiver1) = channel::<i32, 100>();
//! let (sender1, mut receiver1) = channel::<i32>(100);
//! let sender2 = sender1.clone();
//!
//! let t1 = thread::spawn(move || {
Expand Down
32 changes: 16 additions & 16 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ macro_rules! loom {
#[should_panic]
fn test_channel_too_large() {
loom!({
let _ = alloc_impl::channel::<i32, { MAX_LEN + 1 }>();
let _ = alloc_impl::channel::<i32>(MAX_LEN + 1);
});
}

#[test]
fn test_push_pop() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

sender.send(1);
sender.send(2);
Expand All @@ -46,7 +46,7 @@ fn test_push_pop() {
#[test]
fn test_more_push_pop() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

sender.send(1);
sender.send(2);
Expand All @@ -70,7 +70,7 @@ fn test_more_push_pop() {
#[test]
fn test_clone_send() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 6>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(6);

sender.send(1);
sender.send(2);
Expand All @@ -95,7 +95,7 @@ fn test_clone_send() {
#[test]
fn test_clone_recv() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

sender.send(1);
sender.send(2);
Expand All @@ -120,7 +120,7 @@ fn test_clone_recv() {
#[test]
fn test_middle_clone() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

sender.send(1);
sender.send(2);
Expand Down Expand Up @@ -151,7 +151,7 @@ fn test_middle_clone() {
#[test]
fn test_overflow() {
loom!({
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

sender.send(1);
sender.send(2);
Expand Down Expand Up @@ -183,7 +183,7 @@ fn test_overflow() {
// FIXME: spin panic on loom
#[cfg(not(loom))]
fn test_always_overflow() {
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

for i in 0..100 {
sender.send(i);
Expand All @@ -199,7 +199,7 @@ fn test_always_overflow() {
// FIXME: spin panic on loom
#[cfg(not(loom))]
fn test_sender_receiver_conflict() {
let (sender, receiver) = alloc_impl::channel::<i32, 4>();
let (sender, receiver) = alloc_impl::channel::<i32>(4);

let barrier = Arc::new(std::sync::Barrier::new(2));

Expand Down Expand Up @@ -241,7 +241,7 @@ fn test_sender_receiver_conflict() {
// FIXME: too long on loom
#[cfg(not(loom))]
fn test_multiple_sender_conflict() {
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

let barrier = Arc::new(std::sync::Barrier::new(8));

Expand Down Expand Up @@ -291,7 +291,7 @@ fn test_drop() {
}
}

let (sender, mut receiver) = alloc_impl::channel::<DropCount, 4>();
let (sender, mut receiver) = alloc_impl::channel::<DropCount>(4);

sender.send(DropCount);
sender.send(DropCount);
Expand Down Expand Up @@ -321,7 +321,7 @@ fn test_drop() {
#[test]
#[cfg(not(loom))]
fn stress_test() {
let (sender, receiver) = alloc_impl::channel::<i32, 40>();
let (sender, receiver) = alloc_impl::channel::<i32>(40);

for _ in 0..4 {
for i in 0..10 {
Expand Down Expand Up @@ -355,7 +355,7 @@ mod bench {

#[bench]
fn bench_push_pop(b: &mut Bencher) {
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

b.iter(|| {
sender.send(1);
Expand Down Expand Up @@ -389,7 +389,7 @@ mod bench {

#[bench]
fn bench_push_pop_threaded(b: &mut Bencher) {
let (sender, receiver) = alloc_impl::channel::<i32, 4>();
let (sender, receiver) = alloc_impl::channel::<i32>(4);

b.iter(|| {
let sender = sender.clone();
Expand All @@ -416,7 +416,7 @@ mod bench {

#[bench]
fn bench_overflow(b: &mut Bencher) {
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

b.iter(|| {
sender.send(1);
Expand Down Expand Up @@ -447,7 +447,7 @@ mod bench {

#[bench]
fn bench_always_overflow(b: &mut Bencher) {
let (sender, mut receiver) = alloc_impl::channel::<i32, 4>();
let (sender, mut receiver) = alloc_impl::channel::<i32>(4);

b.iter(|| {
for i in 0..50 {
Expand Down

0 comments on commit 271a566

Please sign in to comment.