fdehau / tui-rs

Build terminal user interfaces and dashboards using Rust
MIT License
10.84k stars 487 forks source link

Style can no longer be created as const #511

Closed SergeyKasmy closed 1 year ago

SergeyKasmy commented 3 years ago

Description

I was looking for a way to create a const Style at the root of the file, so it would be easier to share styles between different UI elements and so I would only need to change it just in one place to have it apply everywhere. I found this issue #223 and its corresponding PR #226 which shows it has already been implemented in commit 808a5c9. But when I looked into style.rs these changes where nowhere to be found. I bisected this change to commit 0ffea49. My questions are: was that intended? Is there any reason for these changed to be removed? I would submit a PR to make them const again but it was probably removed for a reason after all, so I decided to ask about it first.

Example

const STYLE_NORMAL: Style = Style::new().bg(Color::Gray).fg(Color::White);
const STYLE_HIGHLIGHTED: Style = Style::new().bg(Color::White).fg(Color::Black);
fdehau commented 3 years ago

I removed the const modifier because it was not possible to apply it to all methods of Styleadd_modifier and remove_modifier are operating on bitflags structs which has no support for const as far as I understood. I thought that providing only half the API as const would be more confusing. We can maybe revisit this when const support lands in bitflags, WDYT ?

sigmaSd commented 2 years ago

You can inline the implementations to make them const:

https://github.com/fdehau/tui-rs/compare/master...sigmaSd:const?expand=1

Also I noticed that running cargo clippy -- -D clippy::missing_const_for_fn shows that a lot more can be const

sigmaSd commented 2 years ago

Actually tests are failing, I'll have to check more

sigmaSd commented 2 years ago

Was a typo, fixed now