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.

(cherry picked from commit b29b0b6)
  • Loading branch information
pkolaczk authored and vponomaryov committed Oct 29, 2024
1 parent d17b48f commit cf0e6b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -54,7 +54,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 @@ -127,6 +131,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 @@ -46,6 +46,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 cf0e6b4

Please sign in to comment.