Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CopyOnto requirement for ReadItem #21

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/impls/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub struct CodecRegion<C: Codec, R = CopyRegion<u8>> {
impl<C: Codec, R> Region for CodecRegion<C, R>
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R>,
{
type ReadItem<'a> = &'a [u8]
where
Expand Down
3 changes: 2 additions & 1 deletion src/impls/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ where
}
}

impl<'a, R> CopyOnto<ColumnsRegion<R>> for ReadColumns<'a, R>
impl<R> CopyOnto<ColumnsRegion<R>> for ReadColumns<'_, R>
where
R: Region,
for<'a> R::ReadItem<'a>: CopyOnto<R>,
{
fn copy_onto(self, target: &mut ColumnsRegion<R>) -> <ColumnsRegion<R> as Region>::Index {
// Ensure all required regions exist.
Expand Down
6 changes: 1 addition & 5 deletions src/impls/deduplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ impl<R: Region> Default for CollapseSequence<R> {
}
}

impl<R: Region> Region for CollapseSequence<R>
where
for<'a, 'b> R::ReadItem<'a>: PartialEq<R::ReadItem<'b>>,
{
impl<R: Region> Region for CollapseSequence<R> {
type ReadItem<'a> = R::ReadItem<'a> where Self: 'a;
type Index = R::Index;

Expand Down Expand Up @@ -78,7 +75,6 @@ where
impl<R: Region, T: CopyOnto<R>> CopyOnto<CollapseSequence<R>> for T
where
for<'a> T: PartialEq<R::ReadItem<'a>>,
for<'a, 'b> R::ReadItem<'a>: PartialEq<R::ReadItem<'b>>,
{
fn copy_onto(self, target: &mut CollapseSequence<R>) -> <CollapseSequence<R> as Region>::Index {
if let Some(last_index) = target.last_index {
Expand Down
12 changes: 7 additions & 5 deletions src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use crate::{Containerized, CopyOnto, Region, ReserveItems};
/// Delegates to a region `R` to store `u8` slices. By default, it uses a [`CopyRegion`], but a
/// different region can be provided, as long as it absorbs and reads items as `&[u8]`.
///
/// Note that all implementations of `CopyOnto<StringRegion>` must only accept valid utf-8 data
/// because the region does not validate the contents when indexing.
///
/// # Examples
///
/// We fill some data into a string region and use extract it later.
Expand All @@ -32,15 +35,13 @@ use crate::{Containerized, CopyOnto, Region, ReserveItems};
pub struct StringRegion<R = CopyRegion<u8>>
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R>,
{
inner: R,
}

impl<R> Region for StringRegion<R>
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R>,
{
type ReadItem<'a> = &'a str where Self: 'a ;
type Index = R::Index;
Expand All @@ -56,6 +57,7 @@ where

#[inline]
fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
// SAFETY: All CopyOnto implementations only accept correct utf8 data
unsafe { std::str::from_utf8_unchecked(self.inner.index(index)) }
}

Expand Down Expand Up @@ -110,7 +112,7 @@ where
impl<R> ReserveItems<StringRegion<R>> for &String
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R> + ReserveItems<R>,
for<'a> &'a [u8]: ReserveItems<R>,
{
fn reserve_items<I>(target: &mut StringRegion<R>, items: I)
where
Expand Down Expand Up @@ -145,7 +147,7 @@ where
impl<R> ReserveItems<StringRegion<R>> for &str
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R> + ReserveItems<R>,
for<'a> &'a [u8]: ReserveItems<R>,
{
fn reserve_items<I>(target: &mut StringRegion<R>, items: I)
where
Expand All @@ -158,7 +160,7 @@ where
impl<R> ReserveItems<StringRegion<R>> for &&str
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
for<'a> &'a [u8]: CopyOnto<R> + ReserveItems<R>,
for<'a> &'a [u8]: ReserveItems<R>,
{
fn reserve_items<I>(target: &mut StringRegion<R>, items: I)
where
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T: Copy> Index for T {}
/// Implement the [`CopyOnto`] trait for all types that can be copied into a region.
pub trait Region: Default {
/// The type of the data that one gets out of the container.
type ReadItem<'a>: CopyOnto<Self>
type ReadItem<'a>
where
Self: 'a;

Expand Down Expand Up @@ -287,7 +287,10 @@ impl<R: Region, T: CopyOnto<R>> FromIterator<T> for FlatStack<R> {
}
}

impl<R: Region> Clone for FlatStack<R> {
impl<R: Region> Clone for FlatStack<R>
where
for<'a> R::ReadItem<'a>: CopyOnto<R>,
{
fn clone(&self) -> Self {
let mut clone = Self::merge_capacity(std::iter::once(self));
clone.extend(self.iter());
Expand Down Expand Up @@ -514,7 +517,7 @@ mod tests {
where
T: CopyOnto<R>,
// Make sure that types are debug, even if we don't use this in the test.
for<'a> R::ReadItem<'a>: Debug,
for<'a> R::ReadItem<'a>: Debug + CopyOnto<R>,
{
let mut c = FlatStack::default();
c.copy(t);
Expand Down