Skip to content

Commit d811eb1

Browse files
authored
Merge pull request #2 from ModProg/rect-corners
Accessors for Rect corners
2 parents b2491ab + a682eaa commit d811eb1

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Added
11+
12+
- Functions to get the four corners of a `Rect`.
13+
814
## v0.5.0 (2024-11-19)
915

1016
### Breaking Changes

src/rect.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<Unit> Rect<Unit> {
9595
/// # Errors
9696
///
9797
/// Returns `<NewUnit as TryFrom>::Error` when the inner type cannot be
98-
/// converted. For this crate's types, this genenerally will be
98+
/// converted. For this crate's types, this generally will be
9999
pub fn try_cast<NewUnit>(self) -> Result<Rect<NewUnit>, NewUnit::Error>
100100
where
101101
NewUnit: TryFrom<Unit>,
@@ -191,24 +191,53 @@ impl<Unit> Rect<Unit> {
191191
/// Returns the non-origin point.
192192
pub fn extent(&self) -> Point<Unit>
193193
where
194-
Unit: crate::Unit,
194+
Unit: Add<Output = Unit> + Copy,
195195
{
196196
self.origin + self.size
197197
}
198198
}
199199

200200
impl<Unit> Rect<Unit>
201201
where
202+
// alternatively we could reduce the traits for `extent()`
202203
Unit: Add<Output = Unit> + Ord + Copy,
203204
{
204205
/// Returns the top-left and bottom-right points of this rectangle.
205206
///
206-
/// The first point returned will always be the top-right point, even if the size of the rectangle is negative.
207+
/// The first point returned will always be the top-left point, even if the size of the rectangle is negative.
207208
pub fn extents(&self) -> (Point<Unit>, Point<Unit>) {
208-
let extent = self.origin + self.size;
209-
(
210-
Point::new(self.origin.x.min(extent.x), self.origin.y.min(extent.y)),
211-
Point::new(self.origin.x.max(extent.x), self.origin.y.max(extent.y)),
209+
(self.top_left(), self.bottom_right())
210+
}
211+
212+
/// Returns the top-left corner of this rectangle.
213+
pub fn top_left(&self) -> Point<Unit> {
214+
Point::new(
215+
self.origin.x.min(self.extent().x),
216+
self.origin.y.min(self.extent().y),
217+
)
218+
}
219+
220+
/// Returns the top-right corner of this rectangle.
221+
pub fn top_right(&self) -> Point<Unit> {
222+
Point::new(
223+
self.origin.x.max(self.extent().x),
224+
self.origin.y.min(self.extent().y),
225+
)
226+
}
227+
228+
/// Returns the bottom-left corner of this rectangle.
229+
pub fn bottom_left(&self) -> Point<Unit> {
230+
Point::new(
231+
self.origin.x.min(self.extent().x),
232+
self.origin.y.max(self.extent().y),
233+
)
234+
}
235+
236+
/// Returns the bottom-right corner of this rectangle.
237+
pub fn bottom_right(&self) -> Point<Unit> {
238+
Point::new(
239+
self.origin.x.max(self.extent().x),
240+
self.origin.y.max(self.extent().y),
212241
)
213242
}
214243
}

src/traits.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::Fraction;
88

99
/// Converts a type to its floating point representation.
1010
///
11-
/// This trait exists because there is no trait in Rust to peform `x as f32`.
11+
/// This trait exists because there is no trait in Rust to perform `x as f32`.
1212
pub trait FloatConversion {
1313
/// The type that represents this type in floating point form.
1414
type Float;
@@ -332,7 +332,7 @@ pub trait ScreenScale {
332332
/// Converts this value from its current unit into device independent pixels
333333
/// ([`Lp`](crate::units::Lp)) using the provided `scale` factor.
334334
fn into_lp(self, scale: Fraction) -> Self::Lp;
335-
/// Converts from Lp into this type, using the provided `scale` factor.
335+
/// Converts from [`Lp`](crate::units::Lp) into this type, using the provided `scale` factor.
336336
fn from_lp(lp: Self::Lp, scale: Fraction) -> Self;
337337
}
338338

@@ -427,12 +427,16 @@ pub trait Unit:
427427
/// available as traits.
428428
pub trait StdNumOps {
429429
/// Adds `self` and `other`, saturating instead of overflowing.
430+
#[must_use]
430431
fn saturating_add(self, other: Self) -> Self;
431432
/// Multiplies `self` and `other`, saturating instead of overflowing.
433+
#[must_use]
432434
fn saturating_mul(self, other: Self) -> Self;
433435
/// Divides `self` by `other`, saturating instead of overflowing.
436+
#[must_use]
434437
fn saturating_div(self, other: Self) -> Self;
435438
/// Subtracts `other` from `self`, saturating instead of overflowing.
439+
#[must_use]
436440
fn saturating_sub(self, other: Self) -> Self;
437441
}
438442

@@ -556,7 +560,7 @@ impl PixelScaling for Lp {
556560

557561
/// Information about scaling for a numerical unit type.
558562
pub trait UnscaledUnit {
559-
/// The internal reprsentation used by this type.
563+
/// The internal representation used by this type.
560564
type Representation: CastInto<i32>;
561565

562566
/// Returns a new instance using the unscaled representation.

0 commit comments

Comments
 (0)