Skip to content

Make accessing members as lengths more ergonomic. #206

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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] }
Expand Down
14 changes: 5 additions & 9 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/side_offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 10 additions & 5 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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] }
Expand Down