Help via hlist?

I admit up front, my question here is not very clear. In part, I am not sure what I am asking, and in part, I may be abstracting too much.

I have a piece of code that looks like this:

pub struct Func1<T0> {
    i0: Rc<RefCell<T0>>,
    func: Box<dyn Fn(T0) -> u64>,
}

pub struct Func2<T0, T1> {
    i0: Rc<RefCell<T0>>,
    i1: Rc<RefCell<T1>>,
    func: Box<dyn Fn(T0, T1) -> u64>,
}

pub struct Func3<T0, T1, T2> {
    i0: Rc<RefCell<T0>>,
    i1: Rc<RefCell<T1>>,
    i2: Rc<RefCell<T2>>,
    func: Box<dyn Fn(T0, T1, T2) -> u64>,
}

pub struct Func4<T0, T1, T2, T3> {
    i0: Rc<RefCell<T0>>,
    i1: Rc<RefCell<T1>>,
    i2: Rc<RefCell<T2>>,
    i3: Rc<RefCell<T3>>,
    func: Box<dyn Fn(T0, T1, T2, T3) -> u64>,
}

I am trying to figure out if there is some way to abstract this via "heterogeneous list" ?

reason for "no": It is not uncommon to see Rust code of the form:

Foo1<T0>;
Foo2<T0, T1>;
Foo3<T0, T1, T2>;
...

reason for "yes"; it seems, in pseudocode, all we want is:

pub struct Foo<T: HList> {
  inputs: type_map (T, x -> Rc<RefCell<x>>),
  func: Box<dyn Fn(parts of T) -> u64>
}

Apologies in advance for the vaguely / confusingly formulated question. Part of the problem is that I'm not sure I have the right framework to frame this question.

If Rust had the built in functionality, it would be some sort of variadic generics. But it doesn't so far.

Apparently they can be built up to some usable extent by cons-ing type tuples or similar, but I'm unfamiliar with those crates and haven't played around with such myself. Perhaps someone else will have some more informed feedback.

Or if you're just looking to save your keyboard, you could consider a macro that turns a list of types into one of your Func structs.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.