Make most geometry functions const

This commit is contained in:
expectocode 2019-12-31 02:05:53 +00:00
parent ba572f9af3
commit ade8859701
1 changed files with 21 additions and 21 deletions

View File

@ -14,43 +14,43 @@ pub struct Rectangle {
}
impl Point {
pub fn new(x: u32, y: u32) -> Self {
pub const fn new(x: u32, y: u32) -> Self {
Self { x, y }
}
pub fn origin() -> Self {
pub const fn origin() -> Self {
Self { x: 0, y: 0 }
}
pub fn with_x(&self, x: u32) -> Self {
pub const fn with_x(&self, x: u32) -> Self {
Self { x, y: self.y }
}
pub fn with_y(&self, y: u32) -> Self {
pub const fn with_y(&self, y: u32) -> Self {
Self { x: self.x, y }
}
pub fn x(&self) -> u32 {
pub const fn x(&self) -> u32 {
self.x
}
pub fn y(&self) -> u32 {
pub const fn y(&self) -> u32 {
self.y
}
pub fn left_of(&self, other: &Self) -> bool {
pub const fn left_of(&self, other: &Self) -> bool {
self.x < other.x
}
pub fn right_of(&self, other: &Self) -> bool {
pub const fn right_of(&self, other: &Self) -> bool {
self.x > other.x
}
pub fn above(&self, other: &Self) -> bool {
pub const fn above(&self, other: &Self) -> bool {
self.y < other.y
}
pub fn below(&self, other: &Self) -> bool {
pub const fn below(&self, other: &Self) -> bool {
self.y > other.y
}
}
@ -77,7 +77,7 @@ impl PartialEq for Point {
impl Eq for Point {}
impl Rectangle {
pub fn from_corner_width_height(top_left: Point, width: u32, height: u32) -> Rectangle {
pub const fn from_corner_width_height(top_left: Point, width: u32, height: u32) -> Rectangle {
Rectangle {
top_left,
width,
@ -95,19 +95,19 @@ impl Rectangle {
Rectangle::from_corner_width_height(Point::new(left, top), width, height)
}
pub fn at_origin_with_size(width: u32, height: u32) -> Rectangle {
pub const fn at_origin_with_size(width: u32, height: u32) -> Rectangle {
Rectangle::from_corner_width_height(Point::origin(), width, height)
}
pub fn width(&self) -> u32 {
pub const fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
pub const fn height(&self) -> u32 {
self.height
}
pub fn with_width(&self, width: u32) -> Rectangle {
pub const fn with_width(&self, width: u32) -> Rectangle {
Rectangle {
top_left: self.top_left,
width,
@ -115,7 +115,7 @@ impl Rectangle {
}
}
pub fn with_height(&self, height: u32) -> Rectangle {
pub const fn with_height(&self, height: u32) -> Rectangle {
Rectangle {
top_left: self.top_left,
width: self.width,
@ -123,26 +123,26 @@ impl Rectangle {
}
}
pub fn top_left(&self) -> Point {
pub const fn top_left(&self) -> Point {
self.top_left
}
pub fn top_right(&self) -> Point {
pub const fn top_right(&self) -> Point {
Point::new(self.top_left.x() + self.width, self.top_left.y())
}
pub fn bottom_right(&self) -> Point {
pub const fn bottom_right(&self) -> Point {
Point::new(
self.top_left.x() + self.width,
self.top_left.y() + self.height,
)
}
pub fn bottom_left(&self) -> Point {
pub const fn bottom_left(&self) -> Point {
Point::new(self.top_left.x(), self.top_left.y() + self.height)
}
pub fn area(&self) -> u32 {
pub const fn area(&self) -> u32 {
self.width * self.height
}