Skip to content

Commit

Permalink
Minimize OwnedItem bounds (#499)
Browse files Browse the repository at this point in the history
* Remove all opinions on OwnedItem

* Remove BatchContainer::OwnedItem

* Remove PushInto bounds
  • Loading branch information
frankmcsherry authored May 26, 2024
1 parent 94edc75 commit 1f17e65
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 235 deletions.
1 change: 0 additions & 1 deletion src/trace/implementations/huffman_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl<B: Ord + Clone + 'static> PushInto<Vec<B>> for HuffmanContainer<B> {
}

impl<B: Ord + Clone + 'static> BatchContainer for HuffmanContainer<B> {
type OwnedItem = Vec<B>;
type ReadItem<'a> = Wrapped<'a, B>;

fn copy(&mut self, item: Self::ReadItem<'_>) {
Expand Down
63 changes: 22 additions & 41 deletions src/trace/implementations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub mod merge_batcher_col;
pub mod ord_neu;
pub mod rhh;
pub mod huffman_container;
pub mod option_container;

// Opinionated takes on default spines.
pub use self::ord_neu::OrdValSpine as ValSpine;
Expand Down Expand Up @@ -90,15 +89,16 @@ pub trait Layout {
/// The represented update.
type Target: Update + ?Sized;
/// Container for update keys.
type KeyContainer: BatchContainer<OwnedItem = <Self::Target as Update>::Key> + PushInto<<Self::Target as Update>::Key>;
// NB: The `PushInto` constraint is only required by `rhh.rs` to push default values.
type KeyContainer: BatchContainer + PushInto<<Self::Target as Update>::Key>;
/// Container for update vals.
type ValContainer: BatchContainer<OwnedItem = <Self::Target as Update>::Val> + PushInto<<Self::Target as Update>::Val>;
type ValContainer: BatchContainer;
/// Container for update vals.
type UpdContainer:
PushInto<(<Self::Target as Update>::Time, <Self::Target as Update>::Diff)> +
for<'a> BatchContainer<ReadItem<'a> = &'a (<Self::Target as Update>::Time, <Self::Target as Update>::Diff), OwnedItem = (<Self::Target as Update>::Time, <Self::Target as Update>::Diff)>;
for<'a> BatchContainer<ReadItem<'a> = &'a (<Self::Target as Update>::Time, <Self::Target as Update>::Diff)>;
/// Container for offsets.
type OffsetContainer: BatchContainer<OwnedItem = usize> + PushInto<usize>;
type OffsetContainer: for<'a> BatchContainer<ReadItem<'a> = usize>;
}

/// A layout that uses vectors
Expand Down Expand Up @@ -142,7 +142,7 @@ where
/// Examples include types that implement `Clone` who prefer
pub trait PreferredContainer : ToOwned {
/// The preferred container for the type.
type Container: BatchContainer<OwnedItem = Self::Owned> + PushInto<Self::Owned>;
type Container: BatchContainer + PushInto<Self::Owned>;
}

impl<T: Ord + Clone + 'static> PreferredContainer for T {
Expand Down Expand Up @@ -191,7 +191,6 @@ where
}

use std::convert::TryInto;
use std::ops::Deref;
use abomonation_derive::Abomonation;
use crate::trace::cursor::IntoOwned;

Expand Down Expand Up @@ -285,46 +284,32 @@ impl<'a> Iterator for OffsetListIter<'a> {
}
}

/// Helper struct to provide `IntoOwned` for `Copy` types.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
pub struct Wrapper<T: Copy>(T);

impl<T: Copy> Deref for Wrapper<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
impl PushInto<usize> for OffsetList {
fn push_into(&mut self, item: usize) {
self.push(item);
}
}

impl<'a, T: Copy + Ord> IntoOwned<'a> for Wrapper<T> {
type Owned = T;

impl<'a> IntoOwned<'a> for usize {
type Owned = usize;
fn into_owned(self) -> Self::Owned {
self.0
self
}

fn clone_onto(&self, other: &mut Self::Owned) {
*other = self.0;
*other = *self;
}

fn borrow_as(owned: &'a Self::Owned) -> Self {
Self(*owned)
}
}

impl PushInto<usize> for OffsetList {
fn push_into(&mut self, item: usize) {
self.push(item);
*owned
}
}

impl BatchContainer for OffsetList {
type OwnedItem = usize;
type ReadItem<'a> = Wrapper<usize>;
type ReadItem<'a> = usize;

fn copy(&mut self, item: Self::ReadItem<'_>) {
self.push(item.0);
self.push(item);
}

fn copy_range(&mut self, other: &Self, start: usize, end: usize) {
Expand All @@ -342,7 +327,7 @@ impl BatchContainer for OffsetList {
}

fn index(&self, index: usize) -> Self::ReadItem<'_> {
Wrapper(self.index(index))
self.index(index)
}

fn len(&self) -> usize {
Expand Down Expand Up @@ -425,8 +410,10 @@ impl<K,V,T,R> BuilderInput<Preferred<K, V, T, R>> for TimelyStack<((<K as ToOwne
where
K: Ord+ToOwned+PreferredContainer + ?Sized,
K::Owned: Columnation + Ord+Clone+'static,
for<'a> <<K as PreferredContainer>::Container as BatchContainer>::ReadItem<'a> : IntoOwned<'a, Owned = K::Owned>,
V: Ord+ToOwned+PreferredContainer + ?Sized,
V::Owned: Columnation + Ord+Clone+'static,
for<'a> <<V as PreferredContainer>::Container as BatchContainer>::ReadItem<'a> : IntoOwned<'a, Owned = V::Owned>,
T: Columnation + Ord+Lattice+Timestamp+Clone,
R: Columnation + Semigroup+Clone,
{
Expand Down Expand Up @@ -457,14 +444,11 @@ pub mod containers {
use timely::container::PushInto;

use std::borrow::ToOwned;
use crate::trace::IntoOwned;

/// A general-purpose container resembling `Vec<T>`.
pub trait BatchContainer: 'static {
/// An type that all `Self::ReadItem<'_>` can be converted into.
type OwnedItem;
/// The type that can be read back out of the container.
type ReadItem<'a>: Copy + IntoOwned<'a, Owned = Self::OwnedItem> + Ord + for<'b> PartialOrd<Self::ReadItem<'b>>;
type ReadItem<'a>: Copy + Ord + for<'b> PartialOrd<Self::ReadItem<'b>>;

/// Push an item into this container
fn push<D>(&mut self, item: D) where Self: PushInto<D> {
Expand Down Expand Up @@ -547,8 +531,7 @@ pub mod containers {
// All `T: Clone` also implement `ToOwned<Owned = T>`, but without the constraint Rust
// struggles to understand why the owned type must be `T` (i.e. the one blanket impl).
impl<T: Ord + Clone + 'static> BatchContainer for Vec<T> {
type OwnedItem = T;
type ReadItem<'a> = &'a Self::OwnedItem;
type ReadItem<'a> = &'a T;

fn copy(&mut self, item: &T) {
self.push(item.clone());
Expand All @@ -573,8 +556,7 @@ pub mod containers {
// The `ToOwned` requirement exists to satisfy `self.reserve_items`, who must for now
// be presented with the actual contained type, rather than a type that borrows into it.
impl<T: Ord + Columnation + ToOwned<Owned = T> + 'static> BatchContainer for TimelyStack<T> {
type OwnedItem = T;
type ReadItem<'a> = &'a Self::OwnedItem;
type ReadItem<'a> = &'a T;

fn copy(&mut self, item: &T) {
self.copy(item);
Expand Down Expand Up @@ -638,7 +620,6 @@ pub mod containers {
where
B: Ord + Clone + Sized + 'static,
{
type OwnedItem = Vec<B>;
type ReadItem<'a> = &'a [B];

fn copy(&mut self, item: Self::ReadItem<'_>) {
Expand Down
152 changes: 0 additions & 152 deletions src/trace/implementations/option_container.rs

This file was deleted.

Loading

0 comments on commit 1f17e65

Please sign in to comment.