ccbrown / iocraft

A Rust crate for beautiful, artisanally crafted CLIs, TUIs, and text-based IO.
https://crates.io/crates/iocraft
Apache License 2.0
339 stars 5 forks source link

It seems like the component macro doesn't account for generic type arguments well #32

Closed anussel5559 closed 2 weeks ago

anussel5559 commented 2 weeks ago

Trying to use the component macro on a component that takes in a props struct with a generic type argument doesn't work as I might expect: CleanShot 2024-10-30 at 17 19 59@2x CleanShot 2024-10-30 at 17 23 13@2x

It's possible I'm missing something, but when I remove the macro, the resulting function correctly correlates the type definition to the struct in its arguments.

ccbrown commented 2 weeks ago

Thanks for the report. You're not missing anything. This just isn't a use-case I had considered. It is one I'd like to support though.

It's actually possible with the current release to define generic components without using the #[component] macro, and the element! macro supports generic type parameters just fine.

Adding/fixing support for type and const parameters with the #[component] macro is easy enough: https://github.com/ccbrown/iocraft/pull/33

I've just merged that and assuming I don't find any issues it'll go out with the next release. Until then, you can try it via:

iocraft = { git = "https://github.com/ccbrown/iocraft.git", rev = "8b6d0141eeb96fad92482f3ec3420b136812e09e" }

Please let me know if that meets your needs.

There is a caveat: Whether using the #[component] macro or not, the type parameters must be 'static. So you'll need to declare your function like fn ListBox<T: 'static>. I don't see any obvious way around that unfortunately. I'd be interested to hear if that gets in the way of your or anyone else's use-case.

anussel5559 commented 2 weeks ago

oh awesome! thanks for the quick PR.

I grabbed that revision and it definitely meets my needs! the 'static requirement isn't a blocker for me.

ccbrown commented 2 weeks ago

Released in 0.4.0. Thanks again!