Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: #[inline] small functions #655

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

- #\[inline] small functions

## v0.4.0
- Derive Debug and Clone on `Timer`
- Apply the inverse of `set_view` in `screen_to_camera`
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,29 @@ pub enum FontError {
}

impl From<ImageError> for QuicksilverError {
#[inline]
fn from(err: ImageError) -> QuicksilverError {
QuicksilverError::ImageError(err)
}
}

impl From<IOError> for QuicksilverError {
#[inline]
fn from(err: IOError) -> QuicksilverError {
QuicksilverError::IOError(err)
}
}

impl From<GolemError> for QuicksilverError {
#[inline]
fn from(err: GolemError) -> QuicksilverError {
QuicksilverError::GraphicsError(err)
}
}

#[cfg(feature = "font")]
impl From<FontError> for QuicksilverError {
#[inline]
fn from(err: FontError) -> QuicksilverError {
QuicksilverError::FontError(err)
}
Expand Down
1 change: 1 addition & 0 deletions src/geom/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Circle {

impl Circle {
/// Create a circle with the center as a vector
#[inline]
pub fn new(center: Vector, radius: f32) -> Circle {
Circle {
pos: center,
Expand Down
3 changes: 3 additions & 0 deletions src/geom/objects/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Line {
note = "Use another collision library like `vek` instead; please comment on issue #552 for use-cases other libraries don't solve"
)]
///Create a new line with a start- and an endpoint
#[inline]
pub fn new(start: Vector, end: Vector) -> Line {
Line {
a: start,
Expand All @@ -28,6 +29,7 @@ impl Line {

///Create a line with a changed thickness
#[must_use]
#[inline]
pub fn with_thickness(self, thickness: f32) -> Line {
Line {
t: thickness,
Expand All @@ -37,6 +39,7 @@ impl Line {
}

impl PartialEq for Line {
#[inline]
fn eq(&self, other: &Line) -> bool {
self.a == other.a && self.b == other.b
}
Expand Down
1 change: 1 addition & 0 deletions src/geom/objects/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Triangle {
note = "Use another collision library like `vek` instead; please comment on issue #552 for use-cases other libraries don't solve"
)]
///Create a triangle from `Vector`s of all three points
#[inline]
pub fn new(a: Vector, b: Vector, c: Vector) -> Triangle {
Triangle { a, b, c }
}
Expand Down
8 changes: 8 additions & 0 deletions src/geom/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ pub struct Rectangle {

impl Rectangle {
///Create a rectangle from a top-left vector and a size vector
#[inline]
pub fn new(pos: Vector, size: Vector) -> Rectangle {
Rectangle { pos, size }
}

///Create a rectangle at the origin with the given size
#[inline]
pub fn new_sized(size: Vector) -> Rectangle {
Rectangle {
pos: Vector::ZERO,
Expand All @@ -28,33 +30,39 @@ impl Rectangle {
}

///Get the top left coordinate of the Rectangle
#[inline]
pub fn top_left(&self) -> Vector {
self.pos
}

///Get the x-coordinate of the Rectangle
///(The origin of a Rectangle is at the top left)
#[inline]
pub fn x(&self) -> f32 {
self.pos.x
}

///Get the y-coordinate of the Rectangle
///(The origin of a Rectangle is at the top left)
#[inline]
pub fn y(&self) -> f32 {
self.pos.y
}

///Get the size of the Rectangle
#[inline]
pub fn size(&self) -> Vector {
self.size
}

///Get the height of the Rectangle
#[inline]
pub fn height(&self) -> f32 {
self.size.y
}

///Get the width of the Rectangle
#[inline]
pub fn width(&self) -> f32 {
self.size.x
}
Expand Down
23 changes: 15 additions & 8 deletions src/geom/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ impl Shape for Circle {
fn overlaps_circle(&self, c: &Circle) -> bool {
(self.center() - c.center()).len2() < (self.radius + c.radius).powi(2)
}
#[inline]
fn overlaps(&self, shape: &impl Shape) -> bool {
shape.overlaps_circle(self)
}

#[inline]
fn center(&self) -> Vector {
self.pos
}
Expand All @@ -110,6 +112,7 @@ impl Shape for Circle {
Vector::ONE * 2.0 * self.radius,
)
}
#[inline]
fn translate(&self, v: Vector) -> Self {
Circle {
pos: self.pos + v,
Expand Down Expand Up @@ -139,19 +142,24 @@ impl Shape for Rectangle {
&& self.y() + self.height() > b.pos.y
}

#[inline]
fn intersects(&self, l: &Line) -> bool {
l.overlaps_rectangle(self)
}
#[inline]
fn overlaps(&self, shape: &impl Shape) -> bool {
shape.overlaps_rectangle(self)
}

#[inline]
fn center(&self) -> Vector {
self.pos + self.size / 2.0
}
#[inline]
fn bounding_box(&self) -> Rectangle {
*self
}
#[inline]
fn translate(&self, v: Vector) -> Self {
Rectangle {
pos: self.pos + v,
Expand All @@ -160,10 +168,6 @@ impl Shape for Rectangle {
}
}

#[deprecated(
since = "0.4.0-alpha0.5",
note = "Use another collision library like `vek` instead; please comment on issue #552 for use-cases other libraries don't solve"
)]
impl Shape for Triangle {
fn contains(&self, v: Vector) -> bool {
// form three triangles with this new vector
Expand Down Expand Up @@ -217,10 +221,6 @@ impl Shape for Triangle {
}
}

#[deprecated(
since = "0.4.0-alpha0.5",
note = "Use another collision library like `vek` instead; please comment on issue #552 for use-cases other libraries don't solve"
)]
impl Shape for Line {
fn contains(&self, v: Vector) -> bool {
about_equal(
Expand Down Expand Up @@ -274,10 +274,12 @@ impl Shape for Line {
|| self.intersects(&Line::new(top_right, bottom_right))
|| self.intersects(&Line::new(bottom_left, bottom_right))
}
#[inline]
fn overlaps(&self, shape: &impl Shape) -> bool {
shape.intersects(self)
}

#[inline]
fn center(&self) -> Vector {
(self.a + self.b) / 2.0
}
Expand All @@ -296,19 +298,24 @@ impl Shape for Line {
}

impl Shape for Vector {
#[inline]
fn contains(&self, v: Vector) -> bool {
*self == v
}
#[inline]
fn overlaps(&self, shape: &impl Shape) -> bool {
shape.contains(*self)
}

#[inline]
fn center(&self) -> Vector {
*self
}
#[inline]
fn bounding_box(&self) -> Rectangle {
Rectangle::new(*self, Vector::ONE)
}
#[inline]
fn translate(&self, v: Vector) -> Vector {
*self + v
}
Expand Down
10 changes: 10 additions & 0 deletions src/geom/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Mul<Transform> for Transform {

/// Uses the `impl Mul<Transform> for Transform` internally.
impl MulAssign<Transform> for Transform {
#[inline]
fn mul_assign(&mut self, other: Transform) {
*self = *self * other;
}
Expand All @@ -129,6 +130,7 @@ impl MulAssign<Transform> for Transform {
impl Mul<Vector> for Transform {
type Output = Vector;

#[inline]
fn mul(self, other: Vector) -> Vector {
Vector::new(
other.x * self.0[0][0] + other.y * self.0[0][1] + self.0[0][2],
Expand Down Expand Up @@ -157,6 +159,7 @@ impl Mul<f32> for Transform {

/// Uses the `impl Mul<f32> for Transform` internally.
impl MulAssign<f32> for Transform {
#[inline]
fn mul_assign(&mut self, other: f32) {
*self = *self * other;
}
Expand All @@ -181,6 +184,7 @@ impl Add<Transform> for Transform {

/// Uses the `impl Add<Transform> for Transform` internally
impl AddAssign<Transform> for Transform {
#[inline]
fn add_assign(&mut self, other: Transform) {
*self = *self + other;
}
Expand All @@ -205,6 +209,7 @@ impl Sub<Transform> for Transform {

/// Uses the `impl Sub<Transform> for Transform` internally
impl SubAssign<Transform> for Transform {
#[inline]
fn sub_assign(&mut self, other: Transform) {
*self = *self - other;
}
Expand All @@ -224,6 +229,7 @@ impl fmt::Display for Transform {
}

impl Default for Transform {
#[inline]
fn default() -> Transform {
Transform::IDENTITY
}
Expand All @@ -245,25 +251,29 @@ impl PartialEq for Transform {
impl Eq for Transform {}

impl From<[[f32; 3]; 3]> for Transform {
#[inline]
fn from(array: [[f32; 3]; 3]) -> Transform {
Transform(array)
}
}

impl From<Transform> for [[f32; 3]; 3] {
#[inline]
fn from(trans: Transform) -> [[f32; 3]; 3] {
trans.0
}
}

impl From<mint::RowMatrix3<f32>> for Transform {
#[inline]
fn from(mat: mint::RowMatrix3<f32>) -> Transform {
let data: [f32; 9] = mat.into();
Transform(bytemuck::cast(data))
}
}

impl From<Transform> for mint::RowMatrix3<f32> {
#[inline]
fn from(trans: Transform) -> mint::RowMatrix3<f32> {
let data: [f32; 9] = bytemuck::cast(trans.0);
data.into()
Expand Down
Loading