Sha256: c45d5ffba6df85d6d06d27df42f3238fe55fe1ea08999238698598d077df05b1

Contents?: true

Size: 1.51 KB

Versions: 19

Compression:

Stored size: 1.51 KB

Contents

use magnus::{
    block::Proc,
    error::Error,
    method,
    scan_args::{get_kwargs, scan_args},
    value::Value,
    RArray, RHash, Ruby, Symbol,
};

fn example(ruby: &Ruby, _rb_self: Value, args: &[Value]) -> Result<RArray, Error> {
    let args = scan_args(args)?;
    let (a,): (String,) = args.required;
    let (b,): (Option<String>,) = args.optional;
    let splat: RArray = args.splat;
    let (c,): (Symbol,) = args.trailing;
    let kw = get_kwargs::<_, (usize,), (Option<usize>, Option<usize>, Option<usize>), RHash>(
        args.keywords,
        &["d"],
        &["e", "f", "g"],
    )?;
    let (d,) = kw.required;
    let (e, f, g) = kw.optional;
    let kw_splat = kw.splat;
    let _: Option<Proc> = args.block;

    let res = ruby.ary_new_capa(7);
    res.push(a)?;
    res.push(b)?;
    res.push(splat)?;
    res.push(c)?;
    res.push(d)?;
    if let Some(e) = e {
        res.push(e)?;
    }
    res.push(f)?;
    if let Some(g) = g {
        res.push(g)?;
    }
    res.push(kw_splat)?;
    Ok(res)
}

#[test]
fn it_scans_args() {
    let ruby = unsafe { magnus::embed::init() };

    ruby.define_global_function("example", method!(example, -1));

    let res = ruby.eval::<bool>(r#"
        example("a", "b", "splat1", "splat2", :c, d: 1, f: 2, h: 3) == ["a", "b", ["splat1", "splat2"], :c, 1, 2, {h: 3}]
    "#).unwrap();
    assert!(res);

    let res = ruby
        .eval::<bool>(
            r#"
        example("a", :c, d: 1) == ["a", nil, [], :c, 1, nil, {}]
    "#,
        )
        .unwrap();
    assert!(res);
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/scan_args.rs
wasmtime-28.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/scan_args.rs
wasmtime-27.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/scan_args.rs
wasmtime-26.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/scan_args.rs
wasmtime-25.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-25.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-25.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-24.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-23.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-22.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-21.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-20.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-20.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/scan_args.rs
wasmtime-18.0.3 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs
wasmtime-17.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs
wasmtime-17.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs
wasmtime-16.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs
wasmtime-15.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs
wasmtime-15.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/scan_args.rs