From 271a56606a36c68d80c870333bf5b4c6f0357efa Mon Sep 17 00:00:00 2001 From: Amjad Alsharafi <26300843+Amjad50@users.noreply.github.com> Date: Thu, 22 Feb 2024 23:39:17 +0800 Subject: [PATCH] Remove unneeded const generic for `alloc` Since its allocated anyway at runtime, there is no need for the extra syntax, and this makes it harder to initialize. --- src/alloc.rs | 61 ++++++++++++++++++++++++++-------------------------- src/lib.rs | 4 ++-- src/tests.rs | 32 +++++++++++++-------------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/alloc.rs b/src/alloc.rs index 0a5dadb..5b519ec 100644 --- a/src/alloc.rs +++ b/src/alloc.rs @@ -23,7 +23,7 @@ //! # #[cfg(not(loom))] //! # { //! use blinkcast::alloc::channel; -//! let (sender, mut receiver) = channel::(); +//! let (sender, mut receiver) = channel::(4); //! sender.send(1); //! assert_eq!(receiver.recv(), Some(1)); //! @@ -51,15 +51,17 @@ use alloc::sync::Arc; #[cfg(loom)] use loom::sync::Arc; -struct InnerChannel { +struct InnerChannel { buffer: Box<[Node]>, head: AtomicUsize, } -impl InnerChannel { - fn new() -> Self { - let mut buffer = Vec::with_capacity(N); - for _ in 0..N { +impl InnerChannel { + 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(); @@ -91,7 +93,7 @@ impl InnerChannel { /// # { /// use blinkcast::alloc::channel; /// -/// let (sender, mut receiver) = channel::(); +/// let (sender, mut receiver) = channel::(4); /// /// sender.send(1); /// let sender2 = sender.clone(); @@ -108,7 +110,7 @@ impl InnerChannel { /// # { /// use blinkcast::alloc::Sender; /// -/// let sender = Sender::::new(); +/// let sender = Sender::::new(4); /// /// let mut receiver = sender.new_receiver(); /// @@ -119,14 +121,14 @@ impl InnerChannel { /// assert_eq!(receiver.recv(), None); /// # } /// ``` -pub struct Sender { - queue: Arc>, +pub struct Sender { + queue: Arc>, } -unsafe impl Send for Sender {} -unsafe impl Sync for Sender {} +unsafe impl Send for Sender {} +unsafe impl Sync for Sender {} -impl Sender { +impl Sender { /// 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. @@ -136,12 +138,9 @@ impl Sender { /// 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::::new()), + queue: Arc::new(InnerChannel::::new(size)), } } @@ -153,7 +152,7 @@ impl Sender { /// # { /// use blinkcast::alloc::Sender; /// - /// let sender = Sender::::new(); + /// let sender = Sender::::new(4); /// /// sender.send(1); /// @@ -165,7 +164,7 @@ impl Sender { /// assert_eq!(receiver.recv(), None); /// # } /// ``` - pub fn new_receiver(&self) -> Receiver { + pub fn new_receiver(&self) -> Receiver { let head = self.queue.head.load(Ordering::Relaxed); let (lap, index) = unpack_data_index(head); @@ -176,7 +175,7 @@ impl Sender { } } -impl Clone for Sender { +impl Clone for Sender { fn clone(&self) -> Self { Self { queue: self.queue.clone(), @@ -198,7 +197,7 @@ impl Clone for Sender { /// # #[cfg(not(loom))] /// # { /// use blinkcast::alloc::channel; -/// let (sender, mut receiver) = channel::(); +/// let (sender, mut receiver) = channel::(4); /// sender.send(1); /// assert_eq!(receiver.recv(), Some(1)); /// @@ -215,15 +214,15 @@ impl Clone for Sender { /// assert_eq!(receiver2.recv(), None); /// # } /// ``` -pub struct Receiver { - queue: Arc>, +pub struct Receiver { + queue: Arc>, reader: ReaderData, } -unsafe impl Send for Receiver {} -unsafe impl Sync for Receiver {} +unsafe impl Send for Receiver {} +unsafe impl Sync for Receiver {} -impl Receiver { +impl Receiver { /// Receives a message from the channel. /// /// If there is no message available, this method will return `None`. @@ -232,7 +231,7 @@ impl Receiver { } } -impl Clone for Receiver { +impl Clone for Receiver { fn clone(&self) -> Self { Self { queue: self.queue.clone(), @@ -252,7 +251,7 @@ impl Clone for Receiver { /// # #[cfg(not(loom))] /// # { /// use blinkcast::alloc::channel; -/// let (sender, mut receiver) = channel::(); +/// let (sender, mut receiver) = channel::(4); /// /// sender.send(1); /// sender.send(2); @@ -271,8 +270,8 @@ impl Clone for Receiver { /// assert_eq!(receiver2.recv(), None); /// # } /// ``` -pub fn channel() -> (Sender, Receiver) { - let sender = Sender::::new(); +pub fn channel(size: usize) -> (Sender, Receiver) { + let sender = Sender::::new(size); let receiver = sender.new_receiver(); (sender, receiver) } diff --git a/src/lib.rs b/src/lib.rs index 88bcbc0..9ba0089 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ //! # { //! use blinkcast::alloc::channel; //! -//! let (sender, mut receiver1) = channel::(); +//! let (sender, mut receiver1) = channel::(4); //! sender.send(1); //! sender.send(2); //! @@ -48,7 +48,7 @@ //! # { //! use blinkcast::alloc::channel; //! use std::thread; -//! let (sender1, mut receiver1) = channel::(); +//! let (sender1, mut receiver1) = channel::(100); //! let sender2 = sender1.clone(); //! //! let t1 = thread::spawn(move || { diff --git a/src/tests.rs b/src/tests.rs index 69ef2d7..892765c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -21,14 +21,14 @@ macro_rules! loom { #[should_panic] fn test_channel_too_large() { loom!({ - let _ = alloc_impl::channel::(); + let _ = alloc_impl::channel::(MAX_LEN + 1); }); } #[test] fn test_push_pop() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(1); sender.send(2); @@ -46,7 +46,7 @@ fn test_push_pop() { #[test] fn test_more_push_pop() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(1); sender.send(2); @@ -70,7 +70,7 @@ fn test_more_push_pop() { #[test] fn test_clone_send() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(6); sender.send(1); sender.send(2); @@ -95,7 +95,7 @@ fn test_clone_send() { #[test] fn test_clone_recv() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(1); sender.send(2); @@ -120,7 +120,7 @@ fn test_clone_recv() { #[test] fn test_middle_clone() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(1); sender.send(2); @@ -151,7 +151,7 @@ fn test_middle_clone() { #[test] fn test_overflow() { loom!({ - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(1); sender.send(2); @@ -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::(); + let (sender, mut receiver) = alloc_impl::channel::(4); for i in 0..100 { sender.send(i); @@ -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::(); + let (sender, receiver) = alloc_impl::channel::(4); let barrier = Arc::new(std::sync::Barrier::new(2)); @@ -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::(); + let (sender, mut receiver) = alloc_impl::channel::(4); let barrier = Arc::new(std::sync::Barrier::new(8)); @@ -291,7 +291,7 @@ fn test_drop() { } } - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); sender.send(DropCount); sender.send(DropCount); @@ -321,7 +321,7 @@ fn test_drop() { #[test] #[cfg(not(loom))] fn stress_test() { - let (sender, receiver) = alloc_impl::channel::(); + let (sender, receiver) = alloc_impl::channel::(40); for _ in 0..4 { for i in 0..10 { @@ -355,7 +355,7 @@ mod bench { #[bench] fn bench_push_pop(b: &mut Bencher) { - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); b.iter(|| { sender.send(1); @@ -389,7 +389,7 @@ mod bench { #[bench] fn bench_push_pop_threaded(b: &mut Bencher) { - let (sender, receiver) = alloc_impl::channel::(); + let (sender, receiver) = alloc_impl::channel::(4); b.iter(|| { let sender = sender.clone(); @@ -416,7 +416,7 @@ mod bench { #[bench] fn bench_overflow(b: &mut Bencher) { - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); b.iter(|| { sender.send(1); @@ -447,7 +447,7 @@ mod bench { #[bench] fn bench_always_overflow(b: &mut Bencher) { - let (sender, mut receiver) = alloc_impl::channel::(); + let (sender, mut receiver) = alloc_impl::channel::(4); b.iter(|| { for i in 0..50 {