-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3eecc7
commit 06e113c
Showing
5 changed files
with
573 additions
and
221 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/// worktable | ||
pub mod worktable; | ||
pub mod rworktable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.