From b29b0b652dd04baa4bde8930a8723dab22137f28 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. --- Cargo.lock | 46 +++++++++++++++++--------------------- Cargo.toml | 4 ++-- src/scripting/functions.rs | 16 ++++++++++++- src/scripting/mod.rs | 1 + 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c8c4ba..b1cc011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,7 +292,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim 0.11.0", + "strsim", ] [[package]] @@ -428,9 +428,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54e36fcd13ed84ffdfda6f5be89b31287cbb80c439841fe69e04841435464391" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" dependencies = [ "darling_core", "darling_macro", @@ -438,23 +438,23 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c2cf1c23a687a1feeb728783b993c4e1ad83d99f351801977dd809b48d0a70f" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" dependencies = [ "fnv", "ident_case", "proc-macro2", "quote", - "strsim 0.10.0", + "strsim", "syn 2.0.50", ] [[package]] name = "darling_macro" -version = "0.20.8" +version = "0.20.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", @@ -1808,9 +1808,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "scylla" -version = "0.13.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9439d92eea9f86c07175c819c3a129ca28b02477b47df26db354a1f4ea7ee276" +checksum = "b20b46cf4ea921ba41121ba9ddf933185cd830cbe2c4fa6272a6e274a6b7368d" dependencies = [ "arc-swap", "async-trait", @@ -1841,9 +1841,9 @@ dependencies = [ [[package]] name = "scylla-cql" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64037fb9d9c59ae15137fff9a56c4d528908dfd38d09e75b5f8e56e3894966dd" +checksum = "27ea3cd3ff5bf9d7db7a6d65c54cecf52f7c40b8e3e32c8c2d6da84d23776ea4" dependencies = [ "async-trait", "byteorder", @@ -1858,9 +1858,9 @@ dependencies = [ [[package]] name = "scylla-macros" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e5fe1d389adebe6a1a27bce18b81a65ff18c25d58a795de490e18b0e7a27b9f" +checksum = "e50f3e2aec7ea9f495e029fb783eb34c64d26a8f2055e1d6b43d00e04d2fbda6" dependencies = [ "darling", "proc-macro2", @@ -2014,15 +2014,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - -[[package]] -name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "strum" @@ -2152,9 +2146,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.38.1" +version = "1.37.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb2caba9f80616f438e09748d5acda951967e1ea58508ef53d9c6402485a46df" +checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" dependencies = [ "backtrace", "bytes", @@ -2171,9 +2165,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.3.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index b955e23..1017a3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/src/scripting/functions.rs b/src/scripting/functions.rs index 6b716ae..9330a05 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); @@ -122,6 +126,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 6be62c8..b9501c5 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -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)?;