Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const generics for arrays. #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/mpmc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn intrusive_local_chan_bounded_variable_tx_single_thread(producers: usize) {
let elems_per_producer = ELEMS_TO_SEND / producers;

block_on(async {
let rx = LocalChannel::<i32, [i32; CHANNEL_BUFFER_SIZE]>::new();
let rx = LocalChannel::<i32, CHANNEL_BUFFER_SIZE>::new();
let produce_done = join_all((0..producers).into_iter().map(|_| {
Box::pin(async {
for _i in 0..elems_per_producer {
Expand Down
3 changes: 0 additions & 3 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Buffer types

mod real_array;
pub use real_array::RealArray;

mod ring_buffer;
pub use ring_buffer::{ArrayBuf, RingBuf};

Expand Down
62 changes: 0 additions & 62 deletions src/buffer/real_array.rs

This file was deleted.

37 changes: 9 additions & 28 deletions src/buffer/ring_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use super::RealArray;
use core::marker::PhantomData;
use core::mem::MaybeUninit;

/// A Ring Buffer of items
Expand Down Expand Up @@ -44,24 +42,17 @@ pub trait RingBuf {
/// ```
/// use futures_intrusive::buffer::{ArrayBuf, RingBuf};
///
/// type Buffer5 = ArrayBuf<i32, [i32; 5]>;
/// type Buffer5 = ArrayBuf<i32, 5>;
/// let buffer = Buffer5::new();
/// ```
pub struct ArrayBuf<T, A>
where
A: core::convert::AsMut<[T]> + core::convert::AsRef<[T]> + RealArray<T>,
{
buffer: MaybeUninit<A>,
pub struct ArrayBuf<T, const N: usize> {
buffer: MaybeUninit<[T; N]>,
size: usize,
recv_idx: usize,
send_idx: usize,
_phantom: PhantomData<T>,
}

impl<T, A> core::fmt::Debug for ArrayBuf<T, A>
where
A: core::convert::AsMut<[T]> + core::convert::AsRef<[T]> + RealArray<T>,
{
impl<T, const N: usize> core::fmt::Debug for ArrayBuf<T, N> {
fn fmt(
&self,
f: &mut core::fmt::Formatter,
Expand All @@ -73,10 +64,7 @@ where
}
}

impl<T, A> ArrayBuf<T, A>
where
A: core::convert::AsMut<[T]> + core::convert::AsRef<[T]> + RealArray<T>,
{
impl<T, const N: usize> ArrayBuf<T, N> {
fn next_idx(&mut self, last_idx: usize) -> usize {
if last_idx + 1 == self.capacity() {
return 0;
Expand All @@ -85,10 +73,7 @@ where
}
}

impl<T, A> RingBuf for ArrayBuf<T, A>
where
A: core::convert::AsMut<[T]> + core::convert::AsRef<[T]> + RealArray<T>,
{
impl<T, const N: usize> RingBuf for ArrayBuf<T, N> {
type Item = T;

fn new() -> Self {
Expand All @@ -97,7 +82,6 @@ where
send_idx: 0,
recv_idx: 0,
size: 0,
_phantom: PhantomData,
}
}

Expand All @@ -109,7 +93,7 @@ where

#[inline]
fn capacity(&self) -> usize {
A::LEN
N
}

#[inline]
Expand Down Expand Up @@ -150,10 +134,7 @@ where
}
}

impl<T, A> Drop for ArrayBuf<T, A>
where
A: core::convert::AsMut<[T]> + core::convert::AsRef<[T]> + RealArray<T>,
{
impl<T, const N: usize> Drop for ArrayBuf<T, N> {
fn drop(&mut self) {
// Drop all elements which are still stored inside the buffer
while self.size > 0 {
Expand Down Expand Up @@ -361,7 +342,7 @@ mod tests {

#[test]
fn test_array_ring_buf() {
let buf = ArrayBuf::<u32, [u32; 5]>::new();
let buf = ArrayBuf::<u32, 5>::new();
test_ring_buf(buf);
}

Expand Down
13 changes: 7 additions & 6 deletions src/channel/mpmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ where
///
/// ```
/// # use futures_intrusive::channel::LocalChannel;
/// let channel: LocalChannel<i32, [i32; 4]> = LocalChannel::new();
/// let channel: LocalChannel<i32, 4> = LocalChannel::new();
/// ```
///
/// Tasks can receive values from the channel through the `receive` method.
Expand Down Expand Up @@ -643,10 +643,11 @@ where
// Export a non thread-safe version using NoopLock

/// A [`GenericChannel`] implementation which is not thread-safe.
pub type LocalChannel<T, A> = GenericChannel<NoopLock, T, ArrayBuf<T, A>>;
pub type LocalChannel<T, const N: usize> =
GenericChannel<NoopLock, T, ArrayBuf<T, N>>;

/// An unbuffered [`GenericChannel`] implementation which is not thread-safe.
pub type LocalUnbufferedChannel<T> = LocalChannel<T, [T; 0]>;
pub type LocalUnbufferedChannel<T> = LocalChannel<T, 0>;

#[cfg(feature = "std")]
mod if_std {
Expand All @@ -664,11 +665,11 @@ mod if_std {
// `with_capacity()` is meaningful.

/// A [`GenericChannel`] implementation backed by [`parking_lot`].
pub type Channel<T, A> =
GenericChannel<parking_lot::RawMutex, T, ArrayBuf<T, A>>;
pub type Channel<T, const N: usize> =
GenericChannel<parking_lot::RawMutex, T, ArrayBuf<T, N>>;

/// An unbuffered [`GenericChannel`] implementation backed by [`parking_lot`].
pub type UnbufferedChannel<T> = Channel<T, [T; 0]>;
pub type UnbufferedChannel<T> = Channel<T, 0>;
}

#[cfg(feature = "std")]
Expand Down
10 changes: 4 additions & 6 deletions tests/mpmc_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ macro_rules! gen_mpmc_tests {
mod $mod_name {
use super::*;

type ChannelType = $channel_type<i32, [i32; 3]>;
type ChannelType = $channel_type<i32, 3>;
type UnbufferedChannelType = $unbuffered_channel_type<i32>;

fn assert_send(
Expand Down Expand Up @@ -707,8 +707,7 @@ macro_rules! gen_mpmc_tests {
let elem3 = CountedElem::new(3, drop_counter.clone());

{
let channel =
$channel_type::<CountedElem, [CountedElem; 3]>::new();
let channel = $channel_type::<CountedElem, 3>::new();

// Fill the channel
let fut1 = channel.send(elem1.clone());
Expand Down Expand Up @@ -740,8 +739,7 @@ macro_rules! gen_mpmc_tests {
drop_counter.clear();

{
let channel =
$channel_type::<CountedElem, [CountedElem; 3]>::new();
let channel = $channel_type::<CountedElem, 3>::new();

// Fill the channel
let fut1 = channel.send(elem1.clone());
Expand Down Expand Up @@ -1103,7 +1101,7 @@ mod if_std {

#[test]
fn channel_futures_are_send() {
let channel = Channel::<i32, [i32; 3]>::new();
let channel = Channel::<i32, 3>::new();
is_sync(&channel);
{
let recv_fut = channel.receive();
Expand Down