Skip to content

Commit

Permalink
Add latte::vector to generate arbitrary vectors
Browse files Browse the repository at this point in the history
Easy generation of lists, sets and maps with an arbitrary item generator function.
  • Loading branch information
pkolaczk committed Aug 12, 2024
1 parent 4f61ebd commit b29b0b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
46 changes: 20 additions & 26 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ num_cpus = "1.13.0"
openssl = "0.10.38"
parse_duration = "2.1.1"
pin-project = "1.1"
plotters = { version = "0.3", default-features = false, features = ["line_series", "svg_backend", "full_palette"]}
plotters = { version = "0.3", default-features = false, features = ["line_series", "svg_backend", "full_palette"] }
rand = { version = "0.8", default-features = false, features = ["small_rng", "std"] }
rand_distr = "0.4"
regex = "1.5"
Expand All @@ -53,7 +53,7 @@ tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "parking_l
tokio-stream = "0.1"
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
try-lock = "0.2.3"
uuid = { version = "1.1", features = ["v4"] }
walkdir = "2"
Expand Down
16 changes: 15 additions & 1 deletion src/scripting/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rand::prelude::StdRng;
use rand::{Rng, SeedableRng};
use rune::macros::{quote, MacroContext, TokenStream};
use rune::parse::Parser;
use rune::runtime::{Mut, Ref, VmError, VmResult};
use rune::runtime::{Function, Mut, Ref, VmError, VmResult};
use rune::{ast, vm_try, Value};
use statrs::distribution::{Normal, Uniform};
use std::collections::HashMap;
Expand Down Expand Up @@ -71,6 +71,10 @@ pub fn hash(i: i64) -> i64 {
/// Computes hash of two integer values.
#[rune::function]
pub fn hash2(a: i64, b: i64) -> i64 {
hash2_inner(a, b)
}

fn hash2_inner(a: i64, b: i64) -> i64 {
let mut hash = MetroHash64::new();
a.hash(&mut hash);
b.hash(&mut hash);
Expand Down Expand Up @@ -122,6 +126,16 @@ pub fn text(seed: i64, len: usize) -> String {
.collect()
}

#[rune::function]
pub fn vector(len: usize, generator: Function) -> VmResult<Vec<Value>> {
let mut result = Vec::with_capacity(len);
for i in 0..len {
let value = vm_try!(generator.call((i,)));
result.push(value);
}
VmResult::Ok(result)
}

/// Generates 'now' timestamp
#[rune::function]
pub fn now_timestamp() -> i64 {
Expand Down
1 change: 1 addition & 0 deletions src/scripting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn try_install(

latte_module.function_meta(functions::blob)?;
latte_module.function_meta(functions::text)?;
latte_module.function_meta(functions::vector)?;
latte_module.function_meta(functions::now_timestamp)?;
latte_module.function_meta(functions::hash)?;
latte_module.function_meta(functions::hash2)?;
Expand Down

0 comments on commit b29b0b6

Please sign in to comment.