lloydmeta / frunk

Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
https://beachape.com/frunk/
MIT License
1.24k stars 56 forks source link

HList with trait bounded items #207

Open damooo opened 1 year ago

damooo commented 1 year ago

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.

BGR360 commented 1 year ago

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

damooo commented 1 year ago

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.

BGR360 commented 1 year ago

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
{ ... }
damooo commented 1 year ago

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.

BGR360 commented 1 year ago

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