-
-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(const): Mark low level fns const #275
Conversation
Codecov Report
@@ Coverage Diff @@
## main #275 +/- ##
==========================================
+ Coverage 82.50% 82.73% +0.22%
==========================================
Files 36 36
Lines 7301 7360 +59
==========================================
+ Hits 6024 6089 +65
+ Misses 1277 1271 -6
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add unit tests? These help document the intent of this change and prevent the accidental removal of the const keyword later. These can be simple one liners that check that we can use these in a constant context. Something like:
fn layout_can_be_const() {
const layout: Layout = Layout::default()
.margin(1)
.horizontal_margin(1)
...
// or
const layout: Layout = Layout::default().margin(1);
const layout: Layout = Layout::default().horizontal_margin(1);
...
}
For a commit message that targets the changelog, this is not a refactor as it really enables some extra functionality (the ability to use the layout and block in a constant context. So perhaps:
feat(misc): allow layout and block to be used in constant context
Block can now be used like:
`const BORDER_BLOCK: Block = Block::default().borders(...`
Layout can now be used like:
`const MAIN_LAYOUT: Layout = Layout::default().margin(...`
@joshka, thank you for the review!
Agreed! I went for consistency here initially, but I changed the commit message now. I added unit tests in the vein you were suggesting. |
This allows the following types to be used in a constant context: - `Layout` - `Rect` - `Style` - `BorderType` - `Padding` - `Block` Also adds several missing `new()` functions to the above types. Blocks can now be used in the following way: ``` const DEFAULT_BLOCK: Block = Block::new() .title_style(Style::new()) .title_alignment(Alignment::Left) .title_position(Position::Top) .borders(Borders::ALL) .border_style(Style::new()) .style(Style::reset()) .padding(Padding::uniform(1)); ``` Layouts can now be used in the following way: `` const DEFAULT_LAYOUT: Layout = Layout::new() .direction(Direction::Horizontal) .margin(1) .expand_to_fill(false); ``` Rects can now be used in the following way: ``` const RECT: Rect = Rect { x: 0, y: 0, width: 10, height: 10, }; ```
I rebased on main and rewrote the commit message a little. |
Mark functions of
structs
that suit themselves to be composed with multiple other widgets as const, namely:Block
Layout
Rect
Continuation of: #115
This allows with some limitations to share blocks, layouts, through many widgets and with some luck also to remove
lazy_static!
from some of those use cases.