Skip to content

Commit

Permalink
feat: very good ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
JakkuSakura committed May 20, 2024
1 parent b3eecc7 commit 06e113c
Show file tree
Hide file tree
Showing 5 changed files with 573 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
[package]
name = "worktable"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
authors = ["PathScale"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# error
eyre = "0.6.8"
# tokio
eyre = "0.6"
tokio = { version = "1", features = ["full"] }
# tracing
tracing = "0.1"
# serialize
serde = { version = "1", features = ["derive"] }
serde_json = "1"
csv = "1.3"
csv = "1"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/// worktable
pub mod worktable;
pub mod rworktable;
62 changes: 62 additions & 0 deletions src/rworktable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::future::Future;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RWorkTable<T> {
rows: Vec<T>,
}

impl<T> RWorkTable<T> {
pub fn with_capacity(capacity: usize) -> Self {
Self {
rows: Vec::with_capacity(capacity),
}
}
pub fn first<R>(&self, f: impl Fn(&T) -> R) -> Option<R> {
self.rows.first().map(f)
}
pub fn rows(&self) -> &Vec<T> {
&self.rows
}
pub fn into_rows(self) -> Vec<T> {
self.rows
}
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.rows.iter()
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub fn into_result(self) -> Option<T> {
self.rows.into_iter().next()
}
pub fn push(&mut self, row: T) {
self.rows.push(row);
}
pub fn map<R>(self, f: impl Fn(T) -> R) -> Vec<R> {
self.rows.into_iter().map(f).collect()
}
pub async fn map_async<R, F: Future<Output =eyre::Result<R>>>(
self,
f: impl Fn(T) -> F,
) -> eyre::Result<Vec<R>> {
let mut futures = Vec::with_capacity(self.rows.len());
for row in self.rows {
futures.push(f(row).await?);
}
Ok(futures)
}
}

impl<T> IntoIterator for RWorkTable<T> {
type Item = T;

type IntoIter = std::vec::IntoIter<Self::Item>;

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

0 comments on commit 06e113c

Please sign in to comment.