Skip to content

Commit

Permalink
Rename wrapper types and module
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Aug 17, 2023
1 parent c686f4a commit f2beced
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 48 deletions.
10 changes: 5 additions & 5 deletions src/alias.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(feature = "alloc")]
use super::storage::Heap;
use super::{
halves::{Cons, Prod},
rb::SharedRb,
storage::Static,
wrap::{CachingCons, CachingProd},
};
#[cfg(feature = "alloc")]
use alloc::sync::Arc;
Expand All @@ -14,19 +14,19 @@ use alloc::sync::Arc;
pub type StaticRb<T, const N: usize> = SharedRb<Static<T, N>>;

/// Alias for [`StaticRb`] producer.
pub type StaticProd<'a, T, const N: usize> = Prod<&'a StaticRb<T, N>>;
pub type StaticProd<'a, T, const N: usize> = CachingProd<&'a StaticRb<T, N>>;

/// Alias for [`StaticRb`] consumer.
pub type StaticCons<'a, T, const N: usize> = Cons<&'a StaticRb<T, N>>;
pub type StaticCons<'a, T, const N: usize> = CachingCons<&'a StaticRb<T, N>>;

/// Heap-allocated ring buffer.
#[cfg(feature = "alloc")]
pub type HeapRb<T> = SharedRb<Heap<T>>;

#[cfg(feature = "alloc")]
/// Alias for [`HeapRb`] producer.
pub type HeapProd<T> = Prod<Arc<HeapRb<T>>>;
pub type HeapProd<T> = CachingProd<Arc<HeapRb<T>>>;

#[cfg(feature = "alloc")]
/// Alias for [`HeapRb`] consumer.
pub type HeapCons<T> = Cons<Arc<HeapRb<T>>>;
pub type HeapCons<T> = CachingCons<Arc<HeapRb<T>>>;
7 changes: 0 additions & 7 deletions src/halves/mod.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ extern crate std;

/// Shortcuts for frequently used types.
mod alias;
/// Producer and consumer implementations.
pub mod halves;
/// Ring buffer implementations.
pub mod rb;
/// Storage types.
Expand All @@ -22,15 +20,17 @@ pub mod traits;
mod transfer;
/// Internal utilities.
mod utils;
/// Producer and consumer implementations.
pub mod wrap;

#[cfg(test)]
mod tests;

pub use alias::*;
pub use halves::{CachingCons, CachingProd, Cons, Obs, Prod};
pub use rb::{LocalRb, SharedRb};
pub use traits::{consumer, producer};
pub use transfer::transfer;
pub use wrap::{CachingCons, CachingProd, Cons, Obs, Prod};

#[cfg(feature = "bench")]
extern crate test;
Expand Down
2 changes: 1 addition & 1 deletion src/rb/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{macros::rb_impl_init, utils::ranges};
#[cfg(feature = "alloc")]
use crate::traits::Split;
use crate::{
halves::{Cons, Prod},
storage::{Shared, Static, Storage},
traits::{Consumer, Observer, Producer, RingBuffer, SplitRef},
wrap::{Cons, Prod},
};
#[cfg(feature = "alloc")]
use alloc::rc::Rc;
Expand Down
2 changes: 1 addition & 1 deletion src/rb/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{macros::rb_impl_init, utils::ranges};
#[cfg(feature = "alloc")]
use crate::traits::Split;
use crate::{
halves::{CachingCons, CachingProd},
storage::{Shared, Static, Storage},
traits::{Consumer, Observer, Producer, RingBuffer, SplitRef},
wrap::{CachingCons, CachingProd},
};
#[cfg(feature = "alloc")]
use alloc::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/frozen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::Rb;
use crate::{
halves::{FrozenCons, FrozenProd},
storage::Static,
traits::*,
wrap::{FrozenCons, FrozenProd},
};

#[test]
Expand Down
22 changes: 10 additions & 12 deletions src/halves/cached.rs → src/wrap/caching.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{direct::Obs, frozen::FrozenWrap};
use super::{direct::Obs, frozen::Frozen};
use crate::{
rb::traits::{RbRef, ToRbRef},
traits::{Consumer, Observer, Producer},
Expand All @@ -8,33 +8,31 @@ use core::{fmt, mem::MaybeUninit, num::NonZeroUsize};
use std::io;

/// Caching wrapper of ring buffer.
pub struct CachingWrap<R: RbRef, const P: bool, const C: bool> {
frozen: FrozenWrap<R, P, C>,
pub struct Caching<R: RbRef, const P: bool, const C: bool> {
frozen: Frozen<R, P, C>,
}

pub type CachingProd<R> = CachingWrap<R, true, false>;
pub type CachingCons<R> = CachingWrap<R, false, true>;
pub type CachingProd<R> = Caching<R, true, false>;
pub type CachingCons<R> = Caching<R, false, true>;

impl<R: RbRef, const P: bool, const C: bool> CachingWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Caching<R, P, C> {
/// # Safety
///
/// There must be no more than one consumer wrapper.
pub unsafe fn new(rb: R) -> Self {
Self {
frozen: FrozenWrap::new(rb),
}
Self { frozen: Frozen::new(rb) }
}

pub fn observe(&self) -> Obs<R> {
self.frozen.observe()
}

pub fn freeze(self) -> FrozenWrap<R, P, C> {
pub fn freeze(self) -> Frozen<R, P, C> {
self.frozen
}
}

impl<R: RbRef, const P: bool, const C: bool> ToRbRef for CachingWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> ToRbRef for Caching<R, P, C> {
type RbRef = R;

fn rb_ref(&self) -> &R {
Expand All @@ -45,7 +43,7 @@ impl<R: RbRef, const P: bool, const C: bool> ToRbRef for CachingWrap<R, P, C> {
}
}

impl<R: RbRef, const P: bool, const C: bool> Observer for CachingWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Observer for Caching<R, P, C> {
type Item = <R::Target as Observer>::Item;

#[inline]
Expand Down
21 changes: 11 additions & 10 deletions src/halves/direct.rs → src/wrap/direct.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::frozen::FrozenWrap;
use super::frozen::Frozen;
use crate::{
rb::traits::{RbRef, ToRbRef},
traits::{consumer::Consumer, producer::Producer, Observer},
Expand All @@ -7,24 +7,24 @@ use core::{fmt, mem::MaybeUninit, num::NonZeroUsize};
#[cfg(feature = "std")]
use std::io;

pub struct Wrap<R: RbRef, const P: bool, const C: bool> {
pub struct Direct<R: RbRef, const P: bool, const C: bool> {
rb: R,
}

/// Observer of ring buffer.
pub type Obs<R> = Wrap<R, false, false>;
pub type Obs<R> = Direct<R, false, false>;
/// Producer of ring buffer.
pub type Prod<R> = Wrap<R, true, false>;
pub type Prod<R> = Direct<R, true, false>;
/// Consumer of ring buffer.
pub type Cons<R> = Wrap<R, false, true>;
pub type Cons<R> = Direct<R, false, true>;

impl<R: RbRef> Clone for Obs<R> {
fn clone(&self) -> Self {
Self { rb: self.rb.clone() }
}
}

impl<R: RbRef, const P: bool, const C: bool> Wrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Direct<R, P, C> {
/// # Safety
///
/// There must be no more than one wrapper with the same parameter being `true`.
Expand All @@ -35,12 +35,13 @@ impl<R: RbRef, const P: bool, const C: bool> Wrap<R, P, C> {
pub fn observe(&self) -> Obs<R> {
Obs { rb: self.rb.clone() }
}
pub fn freeze(self) -> FrozenWrap<R, P, C> {
unsafe { FrozenWrap::new(self.rb) }

pub fn freeze(self) -> Frozen<R, P, C> {
unsafe { Frozen::new(self.rb) }
}
}

impl<R: RbRef, const P: bool, const C: bool> ToRbRef for Wrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> ToRbRef for Direct<R, P, C> {
type RbRef = R;
fn rb_ref(&self) -> &R {
&self.rb
Expand All @@ -50,7 +51,7 @@ impl<R: RbRef, const P: bool, const C: bool> ToRbRef for Wrap<R, P, C> {
}
}

impl<R: RbRef, const P: bool, const C: bool> Observer for Wrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Observer for Direct<R, P, C> {
type Item = <R::Target as Observer>::Item;

#[inline]
Expand Down
16 changes: 8 additions & 8 deletions src/halves/frozen.rs → src/wrap/frozen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use core::{
#[cfg(feature = "std")]
use std::io;

pub struct FrozenWrap<R: RbRef, const P: bool, const C: bool> {
pub struct Frozen<R: RbRef, const P: bool, const C: bool> {
rb: R,
read: Cell<usize>,
write: Cell<usize>,
Expand All @@ -23,15 +23,15 @@ pub struct FrozenWrap<R: RbRef, const P: bool, const C: bool> {
///
/// Inserted items is not visible for an opposite write end until [`Self::commit`]/[`Self::sync`] is called or `Self` is dropped.
/// A free space of items removed by an opposite write end is not visible for `Self` until [`Self::sync`] is called.
pub type FrozenProd<R> = FrozenWrap<R, true, false>;
pub type FrozenProd<R> = Frozen<R, true, false>;

/// Frozen read end of some ring buffer.
///
/// A free space of removed items is not visible for an opposite write end until [`Self::commit`]/[`Self::sync`] is called or `Self` is dropped.
/// Items inserted by an opposite write end is not visible for `Self` until [`Self::sync`] is called.
pub type FrozenCons<R> = FrozenWrap<R, false, true>;
pub type FrozenCons<R> = Frozen<R, false, true>;

impl<R: RbRef, const P: bool, const C: bool> FrozenWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Frozen<R, P, C> {
/// Create new ring buffer cache.
///
/// # Safety
Expand All @@ -50,7 +50,7 @@ impl<R: RbRef, const P: bool, const C: bool> FrozenWrap<R, P, C> {
}
}

impl<R: RbRef, const P: bool, const C: bool> ToRbRef for FrozenWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> ToRbRef for Frozen<R, P, C> {
type RbRef = R;

fn rb_ref(&self) -> &R {
Expand All @@ -63,7 +63,7 @@ impl<R: RbRef, const P: bool, const C: bool> ToRbRef for FrozenWrap<R, P, C> {
}
}

impl<R: RbRef, const P: bool, const C: bool> FrozenWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Frozen<R, P, C> {
/// Commit changes to the ring buffer.
pub fn commit(&self) {
unsafe {
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<R: RbRef> FrozenProd<R> {
}
}

impl<R: RbRef, const P: bool, const C: bool> Observer for FrozenWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Observer for Frozen<R, P, C> {
type Item = <R::Target as Observer>::Item;

#[inline]
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<R: RbRef> Consumer for FrozenCons<R> {
}
}

impl<R: RbRef, const P: bool, const C: bool> Drop for FrozenWrap<R, P, C> {
impl<R: RbRef, const P: bool, const C: bool> Drop for Frozen<R, P, C> {
fn drop(&mut self) {
self.commit();
}
Expand Down
7 changes: 7 additions & 0 deletions src/wrap/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod caching;
pub mod direct;
pub mod frozen;

pub use caching::{CachingCons, CachingProd};
pub use direct::{Cons, Obs, Prod};
pub use frozen::{FrozenCons, FrozenProd};

0 comments on commit f2beced

Please sign in to comment.