From 7f84acfc5b930de1b43765b7b5926802db143dfd Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Wed, 8 Jan 2025 09:39:23 -0600 Subject: [PATCH] chore: Add stdutil crate --- Cargo.lock | 5 +++ crates/rayexec_execution/Cargo.toml | 1 + .../src/functions/aggregate/states.rs | 20 +++++---- crates/stdutil/Cargo.toml | 6 +++ crates/stdutil/src/iter.rs | 35 ++++++++++++++++ crates/stdutil/src/lib.rs | 4 ++ crates/stdutil/src/marker.rs | 42 +++++++++++++++++++ 7 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 crates/stdutil/Cargo.toml create mode 100644 crates/stdutil/src/iter.rs create mode 100644 crates/stdutil/src/lib.rs create mode 100644 crates/stdutil/src/marker.rs diff --git a/Cargo.lock b/Cargo.lock index 605a6db2f..2f7d8207a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2283,6 +2283,7 @@ dependencies = [ "serde_json", "similar-asserts", "smallvec", + "stdutil", "strsim", "textwrap", "tokio", @@ -3031,6 +3032,10 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "stdutil" +version = "0.0.94" + [[package]] name = "stringprep" version = "0.1.5" diff --git a/crates/rayexec_execution/Cargo.toml b/crates/rayexec_execution/Cargo.toml index cf3426671..6eb4ab75b 100644 --- a/crates/rayexec_execution/Cargo.toml +++ b/crates/rayexec_execution/Cargo.toml @@ -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 diff --git a/crates/rayexec_execution/src/functions/aggregate/states.rs b/crates/rayexec_execution/src/functions/aggregate/states.rs index c3926aa98..a6d2a0f5e 100644 --- a/crates/rayexec_execution/src/functions/aggregate/states.rs +++ b/crates/rayexec_execution/src/functions/aggregate/states.rs @@ -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}; @@ -26,8 +26,10 @@ pub struct TypedAggregateGroupStates, - _output: PhantomData, + // Note that these don't use `PhantomData` since we'll want to allow unsized + // types for the output type. + _input: PhantomCovariant, + _output: PhantomCovariant, } impl @@ -43,8 +45,8 @@ impl state_init, state_update, state_finalize, - _input: PhantomData, - _output: PhantomData, + _input: PhantomCovariant::new(), + _output: PhantomCovariant::new(), } } } @@ -71,8 +73,8 @@ where state_init, state_update: unary_update::, state_finalize, - _input: PhantomData, - _output: PhantomData, + _input: PhantomCovariant::new(), + _output: PhantomCovariant::new(), }) } @@ -97,8 +99,8 @@ where state_init, state_update: binary_update::, state_finalize, - _input: PhantomData, - _output: PhantomData, + _input: PhantomCovariant::new(), + _output: PhantomCovariant::new(), }) } diff --git a/crates/stdutil/Cargo.toml b/crates/stdutil/Cargo.toml new file mode 100644 index 000000000..7cb3e1417 --- /dev/null +++ b/crates/stdutil/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "stdutil" +version.workspace = true +edition.workspace = true + +[dependencies] diff --git a/crates/stdutil/src/iter.rs b/crates/stdutil/src/iter.rs new file mode 100644 index 000000000..f77a6fda8 --- /dev/null +++ b/crates/stdutil/src/iter.rs @@ -0,0 +1,35 @@ +/// Similar to `IntoIterator`, but for an iterator with an exact size. +pub trait IntoExactSizeIterator { + type Item; + type IntoIter: ExactSizeIterator; + + /// Converts self into the `ExactSizeIteror`. + fn into_iter(self) -> Self::IntoIter; +} + +/// Auto-implement for any exact size iterator. +impl 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: Sized { + /// Create Self from an exact size iterator. + fn from_iter>(iter: T) -> Self; +} + +pub trait TryFromExactSizeIterator: Sized { + /// Error type that will be returned. + type Error; + + /// Try to create Self from an exact size iterator. + fn try_from_iter>(iter: T) -> Result; +} diff --git a/crates/stdutil/src/lib.rs b/crates/stdutil/src/lib.rs new file mode 100644 index 000000000..854431330 --- /dev/null +++ b/crates/stdutil/src/lib.rs @@ -0,0 +1,4 @@ +//! Utilities that are closely related to items found in std. + +pub mod iter; +pub mod marker; diff --git a/crates/stdutil/src/marker.rs b/crates/stdutil/src/marker.rs new file mode 100644 index 000000000..b835146fb --- /dev/null +++ b/crates/stdutil/src/marker.rs @@ -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(PhantomData T>) +where + T: ?Sized; + +impl PhantomCovariant +where + T: ?Sized, +{ + pub const fn new() -> Self { + PhantomCovariant(PhantomData) + } +} + +impl Clone for PhantomCovariant +where + T: ?Sized, +{ + fn clone(&self) -> Self { + *self + } +} + +impl Copy for PhantomCovariant where T: ?Sized {} + +impl Default for PhantomCovariant +where + T: ?Sized, +{ + fn default() -> Self { + Self::new() + } +}