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

Make accessing members as lengths more ergonomic. #206

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -206,6 +206,12 @@ where T: Copy + One + Add<Output=T> + Sub<Output=T> + Mul<Output=T> {
}
}

impl<T, U> From<T> for Length<T, U> {
fn from(val: T) -> Self {
Length::new(val)
}
}

#[cfg(test)]
mod tests {
use super::Length;
@@ -49,10 +49,10 @@
//! let p = WorldPoint::new(0.0, 1.0, 1.0);
//! // p.x is an f32.
//! println!("p.x = {:?} ", p.x);
//! // p.x is a Length<f32, WorldSpace>.
//! println!("p.x_typed() = {:?} ", p.x_typed());
//! // p.x() is a Length<f32, WorldSpace>.
//! println!("p.x() = {:?} ", p.x());
//! // Length::get returns the scalar value (f32).
//! assert_eq!(p.x, p.x_typed().get());
//! assert_eq!(p.x, p.x().get());
//! ```

extern crate heapsize;
@@ -92,11 +92,11 @@ impl<T: Copy, U> TypedPoint2D<T, U> {

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
pub fn x(&self) -> Length<T, U> { Length::new(self.x) }

/// Returns self.y as a Length carrying the unit.
#[inline]
pub fn y_typed(&self) -> Length<T, U> { Length::new(self.y) }
pub fn y(&self) -> Length<T, U> { Length::new(self.y) }

/// Drop the units, preserving only the numeric value.
#[inline]
@@ -441,15 +441,15 @@ impl<T: Copy, U> TypedPoint3D<T, U> {

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
pub fn x(&self) -> Length<T, U> { Length::new(self.x) }

/// Returns self.y as a Length carrying the unit.
#[inline]
pub fn y_typed(&self) -> Length<T, U> { Length::new(self.y) }
pub fn y(&self) -> Length<T, U> { Length::new(self.y) }

/// Returns self.z as a Length carrying the unit.
#[inline]
pub fn z_typed(&self) -> Length<T, U> { Length::new(self.z) }
pub fn z(&self) -> Length<T, U> { Length::new(self.z) }

#[inline]
pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] }
@@ -192,19 +192,15 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T

#[inline]
#[must_use]
pub fn inflate(&self, width: T, height: T) -> Self {
pub fn inflate<T2: Into<Length<T, U>>>(&self, width: T2, height: T2) -> Self {
let w = width.into().get();
let h = height.into().get();
TypedRect::new(
TypedPoint2D::new(self.origin.x - width, self.origin.y - height),
TypedSize2D::new(self.size.width + width + width, self.size.height + height + height),
TypedPoint2D::new(self.origin.x - w, self.origin.y - h),
TypedSize2D::new(self.size.width + w + w, self.size.height + h + h),
)
}

#[inline]
#[must_use]
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> Self {
self.inflate(width.get(), height.get())
}

#[inline]
pub fn top_right(&self) -> TypedPoint2D<T, U> {
TypedPoint2D::new(self.max_x(), self.origin.y)
@@ -62,16 +62,16 @@ impl<T: Copy, U> TypedSideOffsets2D<T, U> {
}

/// Access self.top as a typed Length instead of a scalar value.
pub fn top_typed(&self) -> Length<T, U> { Length::new(self.top) }
pub fn top(&self) -> Length<T, U> { Length::new(self.top) }

/// Access self.right as a typed Length instead of a scalar value.
pub fn right_typed(&self) -> Length<T, U> { Length::new(self.right) }
pub fn right(&self) -> Length<T, U> { Length::new(self.right) }

/// Access self.bottom as a typed Length instead of a scalar value.
pub fn bottom_typed(&self) -> Length<T, U> { Length::new(self.bottom) }
pub fn bottom(&self) -> Length<T, U> { Length::new(self.bottom) }

/// Access self.left as a typed Length instead of a scalar value.
pub fn left_typed(&self) -> Length<T, U> { Length::new(self.left) }
pub fn left(&self) -> Length<T, U> { Length::new(self.left) }

/// Constructor setting the same value to all sides, taking a scalar value directly.
pub fn new_all_same(all: T) -> Self {
@@ -94,11 +94,11 @@ impl<T: Copy, U> TypedVector2D<T, U> {

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
pub fn x(&self) -> Length<T, U> { Length::new(self.x) }

/// Returns self.y as a Length carrying the unit.
#[inline]
pub fn y_typed(&self) -> Length<T, U> { Length::new(self.y) }
pub fn y(&self) -> Length<T, U> { Length::new(self.y) }

/// Drop the units, preserving only the numeric value.
#[inline]
@@ -151,6 +151,11 @@ where T: Copy + Mul<T, Output=T> + Add<T, Output=T> + Sub<T, Output=T> {
pub fn length(&self) -> T where T: Float + ApproxEq<T> {
self.square_length().sqrt()
}

#[inline]
pub fn length_typed(&self) -> Length<T, U> where T: Float + ApproxEq<T> {
Length::new(self.length())
}
}

impl<T, U> TypedVector2D<T, U>
@@ -440,15 +445,15 @@ impl<T: Copy, U> TypedVector3D<T, U> {

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
pub fn x(&self) -> Length<T, U> { Length::new(self.x) }

/// Returns self.y as a Length carrying the unit.
#[inline]
pub fn y_typed(&self) -> Length<T, U> { Length::new(self.y) }
pub fn y(&self) -> Length<T, U> { Length::new(self.y) }

/// Returns self.z as a Length carrying the unit.
#[inline]
pub fn z_typed(&self) -> Length<T, U> { Length::new(self.z) }
pub fn z(&self) -> Length<T, U> { Length::new(self.z) }

#[inline]
pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] }
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.