From cf0e6b4d040f3636e1c53d7a0b6095a15dc996de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ko=C5=82aczkowski?= Date: Tue, 25 Jun 2024 07:46:21 +0200 Subject: [PATCH] Add latte::vector to generate arbitrary vectors Easy generation of lists, sets and maps with an arbitrary item generator function. (cherry picked from commit b29b0b652dd04baa4bde8930a8723dab22137f28) --- Cargo.toml | 4 ++-- src/scripting/functions.rs | 16 +++++++++++++++- src/scripting/mod.rs | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80418db..659ad8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/src/scripting/functions.rs b/src/scripting/functions.rs index 0c79007..e1941f2 100644 --- a/src/scripting/functions.rs +++ b/src/scripting/functions.rs @@ -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; @@ -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); @@ -127,6 +131,16 @@ pub fn text(seed: i64, len: usize) -> String { .collect() } +#[rune::function] +pub fn vector(len: usize, generator: Function) -> VmResult> { + 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 { diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index 57c7ef4..d68358e 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -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)?;