Skip to content

Commit

Permalink
Rewrite TextConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
AljoschaMeyer committed Jul 21, 2024
1 parent 1c3ff35 commit f569625
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 162 deletions.
3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ mod common_macros;
pub mod consumer;
pub mod errors;
pub mod producer;

#[cfg(all(feature = "dev", feature = "alloc"))]
pub(crate) mod test_yielder;
4 changes: 2 additions & 2 deletions src/common/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod scramble;
#[cfg(feature = "dev")]
pub use scramble::{ConsumeOperations, Scramble_ as Scramble};

#[cfg(feature = "dev")]
#[cfg(all(feature = "dev", feature = "alloc"))]
mod test_consumer;
#[cfg(feature = "dev")]
#[cfg(all(feature = "dev", feature = "alloc"))]
pub use test_consumer::TestConsumer_ as TestConsumer;
22 changes: 22 additions & 0 deletions src/common/consumer/into_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,28 @@ impl<T> Default for IntoVec_<T> {
}

impl<T> IntoVec_<T> {
/// Create a new consumer that collects data into a Vec.
pub fn new() -> IntoVec_<T> {
let invariant = Invariant::new(IntoVec(Vec::new()));

IntoVec_(invariant)
}

/// Convert `self` into the vector of all consumed items.
pub fn into_vec(self) -> Vec<T> {
let inner = self.0.into_inner();
inner.into_inner()
}

/// Return the remaining capacity of the vector, i.e., how many more items it can consume without reallocating.
pub fn remaining_capacity(&self) -> usize {
self.0.as_ref().remaining_capacity()
}

/// Allocate capacity for at least `additional` more items. See [`std::vec::Vec::reserve`].
pub fn reserve(&mut self, additional: usize) {
self.0.as_mut().reserve(additional)
}
}

impl<T, A: Allocator> IntoVec_<T, A> {
Expand All @@ -62,6 +74,16 @@ invarianted_impl_bulk_consumer_sync_and_local_nb!(IntoVec_<T: Copy, A: Allocator
#[derive(Debug)]
struct IntoVec<T, A: Allocator = Global>(Vec<T, A>);

impl<T, A: Allocator> IntoVec<T, A> {
fn remaining_capacity(&self) -> usize {
self.0.capacity() - self.0.len()
}

fn reserve(&mut self, additional: usize) {
self.0.reserve(additional)
}
}

impl<T, A: Allocator> AsRef<Vec<T, A>> for IntoVec<T, A> {
fn as_ref(&self) -> &Vec<T, A> {
&self.0
Expand Down
Loading

0 comments on commit f569625

Please sign in to comment.