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

chore: Add stdutil crate #3388

Merged
merged 1 commit into from
Jan 8, 2025
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
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rayexec_execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ rayexec_proto = { path = "../rayexec_proto" }
rayexec_parser = { path = "../rayexec_parser" }
# rayexec_bullet = { path = "../rayexec_bullet" }
rayexec_io = { path = "../rayexec_io" }
stdutil = { path = "../stdutil" }
fmtutil = { path = "../fmtutil" }
# stackutil = { path = "../stackutil" } TODO: psm hash issues when compiling to wasm on macos

Expand Down
20 changes: 11 additions & 9 deletions crates/rayexec_execution/src/functions/aggregate/states.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::fmt;
use std::any::Any;
use std::fmt::Debug;
use std::marker::PhantomData;

use rayexec_error::{RayexecError, Result};
use stdutil::marker::PhantomCovariant;

use super::ChunkGroupAddressIter;
use crate::arrays::array::{Array, ArrayData};
Expand All @@ -26,8 +26,10 @@ pub struct TypedAggregateGroupStates<State, Input, Output, StateInit, StateUpdat
state_update: StateUpdate,
state_finalize: StateFinalize,

_input: PhantomData<Input>,
_output: PhantomData<Output>,
// Note that these don't use `PhantomData` since we'll want to allow unsized
// types for the output type.
_input: PhantomCovariant<Input>,
_output: PhantomCovariant<Output>,
}

impl<State, Input, Output, StateInit, StateUpdate, StateFinalize>
Expand All @@ -43,8 +45,8 @@ impl<State, Input, Output, StateInit, StateUpdate, StateFinalize>
state_init,
state_update,
state_finalize,
_input: PhantomData,
_output: PhantomData,
_input: PhantomCovariant::new(),
_output: PhantomCovariant::new(),
}
}
}
Expand All @@ -71,8 +73,8 @@ where
state_init,
state_update: unary_update::<State, Storage, Output>,
state_finalize,
_input: PhantomData,
_output: PhantomData,
_input: PhantomCovariant::new(),
_output: PhantomCovariant::new(),
})
}

Expand All @@ -97,8 +99,8 @@ where
state_init,
state_update: binary_update::<State, Storage1, Storage2, Output>,
state_finalize,
_input: PhantomData,
_output: PhantomData,
_input: PhantomCovariant::new(),
_output: PhantomCovariant::new(),
})
}

Expand Down
6 changes: 6 additions & 0 deletions crates/stdutil/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "stdutil"
version.workspace = true
edition.workspace = true

[dependencies]
35 changes: 35 additions & 0 deletions crates/stdutil/src/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Similar to `IntoIterator`, but for an iterator with an exact size.
pub trait IntoExactSizeIterator {
type Item;
type IntoIter: ExactSizeIterator<Item = Self::Item>;

/// Converts self into the `ExactSizeIteror`.
fn into_iter(self) -> Self::IntoIter;
}

/// Auto-implement for any exact size iterator.
impl<I> IntoExactSizeIterator for I
where
I: IntoIterator,
I::IntoIter: ExactSizeIterator,
{
type Item = I::Item;
type IntoIter = I::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.into_iter()
}
}

pub trait FromExactSizeIterator<A>: Sized {
/// Create Self from an exact size iterator.
fn from_iter<T: IntoExactSizeIterator<Item = A>>(iter: T) -> Self;
}

pub trait TryFromExactSizeIterator<A>: Sized {
/// Error type that will be returned.
type Error;

/// Try to create Self from an exact size iterator.
fn try_from_iter<T: IntoExactSizeIterator<Item = A>>(iter: T) -> Result<Self, Self::Error>;
}
4 changes: 4 additions & 0 deletions crates/stdutil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Utilities that are closely related to items found in std.

pub mod iter;
pub mod marker;
42 changes: 42 additions & 0 deletions crates/stdutil/src/marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::marker::PhantomData;

/// Marker type that indicates covariance of `T` but does not inherit the bounds
/// of `T`.
///
/// Has all the same properties of `PhantomData` minus the inherited trait
/// bounds. This lets us make structs and other types covariant to `T` but
/// without the potential inheritence of `?Sized` (or other undesired traits) in
/// the outer type.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PhantomCovariant<T>(PhantomData<fn() -> T>)
where
T: ?Sized;

impl<T> PhantomCovariant<T>
where
T: ?Sized,
{
pub const fn new() -> Self {
PhantomCovariant(PhantomData)
}
}

impl<T> Clone for PhantomCovariant<T>
where
T: ?Sized,
{
fn clone(&self) -> Self {
*self
}
}

impl<T> Copy for PhantomCovariant<T> where T: ?Sized {}

impl<T> Default for PhantomCovariant<T>
where
T: ?Sized,
{
fn default() -> Self {
Self::new()
}
}
Loading