Skip to content

Conversation

@REALERvolker1
Copy link

@REALERvolker1 REALERvolker1 commented Oct 17, 2025

Thank you for helping out with embedded-graphics development! Please:

  • Check that you've added passing tests and documentation
  • Add a CHANGELOG.md entry in the Unreleased section under the appropriate heading (Added, Fixed, etc) if your changes affect the public API
  • Run rustfmt on the project
  • Run just build (Linux/macOS only) and make sure it passes. If you use Windows, check that CI passes once you've opened the PR.

PR description

This PR makes it possible to do several common-sense geometry functions in const-eval. It does not use any nightly features, instead I added some polyfills that could easily be removed if const traits were ever to theoretically be stabilized. I tried to keep it simple, and since it is going from non-const to const in the same MSRV, it is most likely not a breaking change. However, I also decided to make the Size::from_bounding_box function public, because I have some code in other projects that can be greatly simplified with that, and it made sense to do. I am most likely not alone in that situation, as many small displays use caset/paset commands that use values like those.

Copy link
Member

@rfuest rfuest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, more const functions are always nice to have.

The MSRV CI test fails at the moment, but it should be possible to refactor that code without having to raise the MSRV.


/// Creates a size from two corner points of a bounding box.
pub(crate) const fn from_bounding_box(corner_1: Point, corner_2: Point) -> Self {
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples should have a # Examples header:

Suggested change
///
///
/// # Examples
///

Edit: I just noticed that the other examples in this file are also missing this header, but let's at least do it right in the new code.

Comment on lines +11 to +31
- Made the following methods `const`:
- `AnchorPoint::from_xy`
- `AnchorPoint::x`
- `AnchorPoint::y`
- `Point::component_min`
- `Point::component_max`
- `Size::component_min`
- `Size::component_max`
- `Rectangle::with_corners`
- `Rectangle::center`
- `Rectangle::bottom_right`
- `Rectangle::contains`
- `Rectangle::intersection`
- `Rectangle::envelope`
- `Rectangle::resized`
- `Rectangle::resized_width`
- `Rectangle::resized_height`
- `Rectangle::offset`
- `Rectangle::anchor_point`
- `Rectangle::anchor_x`
- `Rectangle::anchor_y`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these into the Changed section and add a link to the PR.

## [Unreleased] - ReleaseDate

### Added
- Made `Size::from_bounding_box` public
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CHANGELOG should reflect API changes and from a user perspective it doesn't matter that this function has been available internally before:

Suggested change
- Made `Size::from_bounding_box` public
- [#794](https://github.com/embedded-graphics/embedded-graphics/pull/794) Added `from_bounding_box` constructor to `Size`.


## [Unreleased] - ReleaseDate

### Added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already an Added section below.

///
/// assert_eq!(bounded, Size::new(310, 230));
/// ```
pub const fn from_bounding_box(corner_1: Point, corner_2: Point) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has been some discussion about the name of this method when it was added, but at that time we didn't really care because it was internal only. I don't like the current name, because I wouldn't expect a function that is called from_BOUNDING_BOX take points as parameters and not a Rectangle. But I cannot really think of anything better at the moment and perhaps I'm overthinking this.

Another option would be to not make this method public and instead to make Rectangle::with_corners const. In my opinion Rectangle::with_corners(top_left, bottom_right).size is more readable than Size::from_bounding_box(top_left, bottom_right). And in case it is used in a const context the possible overhead from also calculating Rectangle::top_left doesn't matter.


## [Unreleased] - ReleaseDate

### Added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All changes in this PR also affect embedded-graphics-core, which has a separate changelog that also needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants