Skip to content

storing & (re)using/restoring variadic tuples #114

@peter-lyons-kehl

Description

@peter-lyons-kehl

Hi/Hallo Lukas,

Thank you for keeping TLBORM alive.

A suggestion of a sub-topic/related to #17, potentially fitting in/next to https://lukaswirth.dev/tlborm/decl-macros/building-blocks/counting.html:

Problem/Goal:

  1. Accept a variable number of actual/passed arguments (valid expressions).
  2. Do something with them (or don't - instead, do something with other variable(s), or inject some other code in, etc.)
  3. Pass those arguments (or something generated from them) to function/method call(s) or similar, in the same order.

The main problem is not storing argument(s) in a tuple, but accessing the tuple fields - generating the tuple numerical indexes. I couldn't find a way to do so: An integer expression generated as per your page above fails - to access tuple field(s), we can't just use a non-negative integer expression, so not anything like my_tuple.(1+2), but only literal, like my_tuple.3)!

So, instead of a flat tuple, I use a "tuple tree" (part of https://github.com/prudent-rs/prudent/tree/tuple_tree)

See https://github.com/prudent-rs/prudent/blob/tuple_tree/src/lib.rs#L71 and then see add_three and add_four (simple_examples/fn_add_three/src/main.rs and simple_examples/fn_add_four/src/main.rs).

use prudent::unsafe_fn;
unsafe fn add_four(left: u64, middle_left:u64, middle_right:u64, right: u64) -> u64 {
    left + middle_left + middle_right + right
}
unsafe_fn!(add_four, 1, 2, 3, 4);

expands to:

use prudent::unsafe_fn;
unsafe fn add_four(left: u64, middle_left: u64, middle_right: u64, right: u64) -> u64 {
    left + middle_left + middle_right + right
}
// ...
{
     let (tuple_tree, fun) = ( (1, (2, (3, (4,)))), add_four );
     #[allow(unsafe_code)]
     unsafe {
         fun(
             tuple_tree.0,
             tuple_tree.1 .0,
             tuple_tree.1 .1 .0,
             tuple_tree.1 .1 .1 .0,
        )
    }
};
```rust

Unfortunately, I don't have capacity to describe it/write on it more. If anyone has capacity/interest, please extract/reuse my macros, especially `unsafe_fn_internal_build_accessors_and_call`. You can also run `cargo test`.

UPDATE:
- use the GIT tag `tuple_tree`. This `tuple_tree` approach and the above examples are being removed from `master` branch soon. While this approach is a good one (if you do need such a tuple field access), I've realized a simpler solution to my problem/goal, where I don't need to access the tuple field at all (I only need to set them - that's a compile-time check in an `if false {...}` branch, work in progress).
- I hope that the macros themselves and their capturing variables are documented and named well enough, but again, only on `tuple_tree` GIT tag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions