Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <antiguru@gmail.com>
  • Loading branch information
antiguru committed Apr 28, 2024
1 parent 6ecb329 commit c07d3c2
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub trait PushContainer: Container {
/// chunked into individual containers, but is free to change the data representation to
/// better fit the properties of the container.
///
/// The owner extracts data in two ways. The opportunistic [`Self::extract`] method returns
/// any ready data, but doesn't need to produce partial outputs. In contrast, [`Self::finish`]
/// needs to produce all outputs, even partial ones.
///
/// For example, a consolidating builder can aggregate differences in-place, but it has
/// to ensure that it preserves the intended information.
pub trait ContainerBuilder: Default + 'static {
Expand All @@ -102,38 +106,6 @@ pub trait ContainerBuilder: Default + 'static {
fn finish(&mut self) -> impl Iterator<Item=Self::Container>;
}

impl<C: PushContainer> ContainerBuilder for C {
type Container = C;

#[inline]
fn push<T: PushInto<Self::Container>>(&mut self, item: T) where Self::Container: PushContainer {
// Ensure capacity
if self.capacity() < C::preferred_capacity() {
self.reserve(C::preferred_capacity() - self.len());
}

// Push data
item.push_into(self);

// We cannot flush because `C` doesn't have any storage, so let's hope someone
// calls `extract` soon!
}

#[inline]
fn extract(&mut self) -> impl Iterator<Item=Self::Container> {
if self.len() > 0 && self.len() == self.capacity() {
Some(std::mem::take(self)).into_iter()
} else {
None.into_iter()
}
}

#[inline]
fn finish(&mut self) -> impl Iterator<Item=Self::Container> {
std::iter::once(std::mem::take(self))
}
}

/// A default container builder that uses length and preferred capacity to chunk data.
#[derive(Default, Debug)]
pub struct CapacityContainerBuilder<C>{
Expand Down

0 comments on commit c07d3c2

Please sign in to comment.