From 267e242238fab0aba14fb4c2e27269ce3a3e3951 Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Fri, 29 Nov 2019 21:24:52 -0500 Subject: Make many functions `const` The point is to set up repeated components or boilerplate before their use sites. The majority of these make sense as `const`. However, some functions such as those regarding state may not make sense as `const`. --- core/src/point.rs | 2 +- core/src/vector.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index 183998dd..52307bba 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -14,7 +14,7 @@ impl Point { /// Creates a new [`Point`] with the given coordinates. /// /// [`Point`]: struct.Point.html - pub fn new(x: f32, y: f32) -> Self { + pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } } diff --git a/core/src/vector.rs b/core/src/vector.rs index 7d87343a..e0c5f073 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -16,7 +16,7 @@ impl Vector { /// Creates a new [`Vector`] with the given components. /// /// [`Vector`]: struct.Vector.html - pub fn new(x: T, y: T) -> Self { + pub const fn new(x: T, y: T) -> Self { Self { x, y } } } -- cgit From b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 23:45:54 +0100 Subject: Implement `Primitive::Cached` --- core/src/point.rs | 11 +++++++++++ core/src/vector.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index b9a8149c..b55f5099 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -46,3 +46,14 @@ impl std::ops::Add for Point { } } } + +impl std::ops::Sub for Point { + type Output = Self; + + fn sub(self, vector: Vector) -> Self { + Self { + x: self.x - vector.x, + y: self.y - vector.y, + } + } +} diff --git a/core/src/vector.rs b/core/src/vector.rs index 4c1cbfab..a75053a0 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -32,6 +32,17 @@ where } } +impl std::ops::Sub for Vector +where + T: std::ops::Sub, +{ + type Output = Self; + + fn sub(self, b: Self) -> Self { + Self::new(self.x - b.x, self.y - b.y) + } +} + impl Default for Vector where T: Default, -- cgit