frunk icon indicating copy to clipboard operation
frunk copied to clipboard

HList with trait bounded items

Open damooo opened this issue 2 years ago • 5 comments

Thanks for frunk!

Sorry if this is obvious, but i cannot figure out how to spell out an hlist with all items satisfying a trait bound. Could you help?

Thanks again for your work.

damooo avatar Nov 03 '22 14:11 damooo

Could you clarify a bit more what you're trying to do?

BGR360 avatar Nov 03 '22 16:11 BGR360

I want to construct a hlist of tokens, all satisfying certain trait called AdjectiveToken. So that i can keep that as a valu to list of adjective tokens. I want it to be HList, instead of Vec<Box<dyn AdjectiveToken>> as i should be able to specify adjective tokens as trait bounds whrere the struct is expected.

damooo avatar Nov 03 '22 17:11 damooo

Does this do what you want?

impl<Head, Tail> AdjectiveToken for HCons<Head, Tail>
where
    Head: AdjectiveToken,
    Tail: AdjectiveToken,
{ ... }

impl AdjectiveToken for HNil { ... }

fn function_which_needs_all_hlist_elements_to_be_tokens<Head, Tail>(hlist: HCons<Head, Tail>)
where
    HCons<Head, Tail>: AdjectiveToken
{ ... }

BGR360 avatar Nov 04 '22 00:11 BGR360

I assume, you meant following:

trait AdjectiveTokenList {}

impl<Head, Tail> AdjectiveTokenList for HCons<Head, Tail>
where
    Head: AdjectiveToken,
    Tail: AdjectiveTokenList,
{ ... }

impl AdjectiveTokenList for HNil { ... }

fn function_which_needs_all_hlist_elements_to_be_tokens<Head, Tail>(hlist: HCons<Head, Tail>)
where
    HCons<Head, Tail>: AdjectiveTokenList
{...}

If so, indeed that is what i mean. But issue is, such an AdjectiveTokenList cannot implement trait HList. As HList requires implementation to allow prepend any unconstrained item in it's trait methods.

damooo avatar Nov 04 '22 07:11 damooo

Please sketch out the pseudocode of what you're hoping to do in the end, I'm really having a hard time understanding.

BGR360 avatar Nov 04 '22 16:11 BGR360