Change Rect's expand/trunc to more useful methods. #107
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I added the
expand
/trunc
methods to various shapes back in #93. They've been quite useful, especially forSize
in my personal usage. However the implementations forRect
were naive. Although their behavior was correctly documented and so not a bug per se, I think the behavior is questionable. That is to say, it's not theRect
itself that gets expanded or truncated, its the individual coordinates.While working on DPI improvements in druid I found a need for methods that
expand
theRect
itself, not its coordinates. I also found an existing usage ofRect::expand
in the GTK druid-shell code. That specific usage expects theRect
itself to be expanded and so is buggy right now.I also found usage of
Rect::expand
in the blurred rects in piet, here and here. I haven't really analyzed the blurred rects code to know if it expects the coordinates to be expanded or theRect
itself. I would imagine theRect
itself, but perhaps @raphlinus can chime in here.This PR changes the behavior of
Rect::expand
andRect::trunc
to what I think are less surprising and more useful methods. They perform their action on theRect
itself.I made the following illustration to show the change in behavior:
These new functions also support a reversed orientation
Rect
, e.g.Rect::new(10, 10, 5, 5)
. That is supported via twoif
statements. As a pleasant surprise LLVM seems to optimize the branches away. I made three different implementations of the functions, two of them branchless. Benchmarks showed no improvement over the much simplerif
version and after briefly inspecting the assembly it seemed like an optimization success story indeed. I kept the bench code in the PR, but I can delete it if we don't expect future optimization attempts and want to keep the repository clean.