Skip to content

Commit

Permalink
fix: fix miri tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Nov 12, 2023
1 parent 0ff2cfd commit 04d0860
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ use swap_buffer_queue::{Queue, write_vectored::WriteVectoredVecBuffer};
let queue: Queue<WriteVectoredVecBuffer<Vec<u8>>, Vec<u8>> = Queue::with_capacity(100);
queue.try_enqueue([vec![0; 256]]).unwrap();
queue.try_enqueue([vec![42; 42]]).unwrap();
let mut total_size = 0u16.to_be_bytes();
let mut slice = queue.try_dequeue().unwrap();
// Adds a header with the total size of the slices
let total_size = (slice.total_size() as u16).to_be_bytes();
let mut frame = slice.frame(.., &total_size, None);
total_size.copy_from_slice(&(slice.total_size() as u16).to_be_bytes());
let mut frame = slice.frame(.., Some(&total_size), None);
// Let's pretend we have a writer
let mut writer: Vec<u8> = Default::default();
assert_eq!(writer.write_vectored(&mut frame).unwrap(), 300);
// In this example, because `total_size` header has a shorter lifetime than `slice`,
// `slice` must be dropped before `total_size`.
drop(slice);
```

## How it works
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ unsafe impl<T> Drain for VecBuffer<T> {
#[inline]
unsafe fn remove(&mut self, index: usize) -> Self::Value {
// SAFETY: function contract guarantees that the index has been inserted and is then initialized
unsafe { self.0[index].replace(MaybeUninit::uninit()).assume_init() }
unsafe { self.0[index].get_mut().assume_init_read() }
}
}
29 changes: 16 additions & 13 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,18 +622,20 @@ where
/// let queue: Queue<VecBuffer<usize>> = Queue::with_capacity(42);
/// queue.try_enqueue([0]).unwrap();
/// queue.try_enqueue([1]).unwrap();
/// let slice = queue.try_dequeue().unwrap();
/// assert_eq!(slice.deref(), &[0, 1]);
/// // dequeuing cannot be done concurrently (`slice` is still in scope)
/// assert_eq!(queue.try_dequeue().unwrap_err(), TryDequeueError::Conflict);
/// drop(slice);
/// {
/// let slice = queue.try_dequeue().unwrap();
/// assert_eq!(slice.deref(), &[0, 1]);
/// // dequeuing cannot be done concurrently (`slice` is still in scope)
/// assert_eq!(queue.try_dequeue().unwrap_err(), TryDequeueError::Conflict);
/// }
/// // let's close the queue
/// queue.try_enqueue([2]).unwrap();
/// queue.close();
/// // queue can be dequeued while closed when not empty
/// let slice = queue.try_dequeue().unwrap();
/// assert_eq!(slice.deref(), &[2]);
/// drop(slice);
/// {
/// let slice = queue.try_dequeue().unwrap();
/// assert_eq!(slice.deref(), &[2]);
/// }
/// assert_eq!(queue.try_dequeue().unwrap_err(), TryDequeueError::Closed)
/// ```
pub fn try_dequeue(&self) -> Result<BufferSlice<B, N>, TryDequeueError> {
Expand Down Expand Up @@ -699,11 +701,12 @@ where
/// Err(TryEnqueueError::InsufficientCapacity([1]))
/// );
/// // dequeue and resize, inserting elements before the buffer is available
/// let slice = queue
/// .try_dequeue_and_resize(3, Some(|| std::iter::once([42])))
/// .unwrap();
/// assert_eq!(slice.deref(), &[0]);
/// drop(slice);
/// {
/// let slice = queue
/// .try_dequeue_and_resize(3, Some(|| std::iter::once([42])))
/// .unwrap();
/// assert_eq!(slice.deref(), &[0]);
/// }
/// // capacity has been increased
/// queue.try_enqueue([1]).unwrap();
/// queue.try_enqueue([2]).unwrap();
Expand Down
8 changes: 3 additions & 5 deletions src/write_vectored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
//! let queue: Queue<WriteVectoredVecBuffer<Vec<u8>>> = Queue::with_capacity(100);
//! queue.try_enqueue([vec![0; 256]]).unwrap();
//! queue.try_enqueue([vec![42; 42]]).unwrap();
//! // Adds a header with the total size of the slices
//! let mut total_size = 0u16.to_be_bytes();
//! let mut slice = queue.try_dequeue().unwrap();
//! let total_size = (slice.total_size() as u16).to_be_bytes();
//! // Adds a header with the total size of the slices
//! total_size.copy_from_slice(&(slice.total_size() as u16).to_be_bytes());
//! let mut frame = slice.frame(.., Some(&total_size), None);
//! // Let's pretend we have a writer
//! let mut writer: Vec<u8> = Default::default();
//! assert_eq!(writer.write_vectored(&mut frame).unwrap(), 300);
//! // In this example, because `total_size` header has a shorter lifetime than `slice`,
//! // `slice` must be dropped before `total_size`.
//! drop(slice);
//! ```
use std::{
Expand Down

0 comments on commit 04d0860

Please sign in to comment.