Skip to content

Commit

Permalink
Don't expose the "buffer" field
Browse files Browse the repository at this point in the history
... and provide a buffer() method instead.

Fixes #26.
  • Loading branch information
mgeier committed Nov 18, 2020
1 parent b7d7283 commit 3abfc4f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<T> RingBuffer<T> {
/// RingBuffer::reset(&mut p, &mut c);
///
/// // The full capacity is now available for writing:
/// if let Ok(mut chunk) = p.write_chunk(p.buffer.capacity()) {
/// if let Ok(mut chunk) = p.write_chunk(p.buffer().capacity()) {
/// let (first, second) = chunk.as_mut_slices();
/// // The first slice is now guaranteed to span the whole buffer:
/// first[0] = 20;
Expand Down Expand Up @@ -470,7 +470,7 @@ impl<T> PartialEq for RingBuffer<T> {
/// use rtrb::RingBuffer;
///
/// let (p, c) = RingBuffer::<f32>::new(1000).split();
/// assert_eq!(p.buffer, c.buffer);
/// assert_eq!(p.buffer(), c.buffer());
///
/// let rb1 = RingBuffer::<f32>::new(1000);
/// let rb2 = RingBuffer::<f32>::new(1000);
Expand Down Expand Up @@ -502,8 +502,8 @@ impl<T> Eq for RingBuffer<T> {}
/// ```
#[derive(Debug)]
pub struct Producer<T> {
/// A read-only reference to the ring buffer.
pub buffer: Arc<RingBuffer<T>>,
/// A reference to the ring buffer.
buffer: Arc<RingBuffer<T>>,

/// A copy of `buffer.head` for quick access.
///
Expand Down Expand Up @@ -663,6 +663,11 @@ impl<T> Producer<T> {
self.next_tail().is_none()
}

/// Returns a read-only reference to the ring buffer.
pub fn buffer(&self) -> &RingBuffer<T> {
&self.buffer
}

/// Get the tail position for writing the next slot, if available.
///
/// This is a strict subset of the functionality implemented in write_chunk_uninit().
Expand Down Expand Up @@ -703,8 +708,8 @@ impl<T> Producer<T> {
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct Consumer<T> {
/// A read-only reference to the ring buffer.
pub buffer: Arc<RingBuffer<T>>,
/// A reference to the ring buffer.
buffer: Arc<RingBuffer<T>>,

/// A copy of `buffer.head` for quick access.
///
Expand Down Expand Up @@ -912,6 +917,11 @@ impl<T> Consumer<T> {
self.next_head().is_none()
}

/// Returns a read-only reference to the ring buffer.
pub fn buffer(&self) -> &RingBuffer<T> {
&self.buffer
}

/// Get the head position for reading the next slot, if available.
///
/// This is a strict subset of the functionality implemented in read_chunk().
Expand Down
6 changes: 3 additions & 3 deletions tests/push_and_pop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn smoke() {
fn capacity() {
for i in 1..10 {
let (p, c) = RingBuffer::<i32>::new(i).split();
assert_eq!(p.buffer.capacity(), i);
assert_eq!(c.buffer.capacity(), i);
assert_eq!(p.buffer().capacity(), i);
assert_eq!(c.buffer().capacity(), i);
}
}

Expand Down Expand Up @@ -66,7 +66,7 @@ fn zero_sized_type() {
assert_eq!(std::mem::size_of::<ZeroSized>(), 0);

let (mut p, mut c) = RingBuffer::new(1).split();
assert_eq!(p.buffer.capacity(), 1);
assert_eq!(p.buffer().capacity(), 1);
assert_eq!(p.slots(), 1);
assert_eq!(c.slots(), 0);
assert!(p.push(ZeroSized).is_ok());
Expand Down

0 comments on commit 3abfc4f

Please sign in to comment.