diff --git a/src/length.rs b/src/length.rs index 3ea3c183..990996a7 100644 --- a/src/length.rs +++ b/src/length.rs @@ -206,6 +206,12 @@ where T: Copy + One + Add + Sub + Mul { } } +impl From for Length { + fn from(val: T) -> Self { + Length::new(val) + } +} + #[cfg(test)] mod tests { use super::Length; diff --git a/src/lib.rs b/src/lib.rs index bc967fba..88290ee7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. -//! println!("p.x_typed() = {:?} ", p.x_typed()); +//! // p.x() is a Length. +//! 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; diff --git a/src/point.rs b/src/point.rs index 7ff8779f..02b1f09c 100644 --- a/src/point.rs +++ b/src/point.rs @@ -92,11 +92,11 @@ impl TypedPoint2D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x(&self) -> Length { Length::new(self.x) } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y(&self) -> Length { Length::new(self.y) } /// Drop the units, preserving only the numeric value. #[inline] @@ -441,15 +441,15 @@ impl TypedPoint3D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x(&self) -> Length { Length::new(self.x) } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y(&self) -> Length { Length::new(self.y) } /// Returns self.z as a Length carrying the unit. #[inline] - pub fn z_typed(&self) -> Length { Length::new(self.z) } + pub fn z(&self) -> Length { Length::new(self.z) } #[inline] pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] } diff --git a/src/rect.rs b/src/rect.rs index 4e9592b6..68f17099 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -192,19 +192,15 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add + Sub Self { + pub fn inflate>>(&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, height: Length) -> Self { - self.inflate(width.get(), height.get()) - } - #[inline] pub fn top_right(&self) -> TypedPoint2D { TypedPoint2D::new(self.max_x(), self.origin.y) diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 58f58f1c..32acc79f 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -62,16 +62,16 @@ impl TypedSideOffsets2D { } /// Access self.top as a typed Length instead of a scalar value. - pub fn top_typed(&self) -> Length { Length::new(self.top) } + pub fn top(&self) -> Length { Length::new(self.top) } /// Access self.right as a typed Length instead of a scalar value. - pub fn right_typed(&self) -> Length { Length::new(self.right) } + pub fn right(&self) -> Length { Length::new(self.right) } /// Access self.bottom as a typed Length instead of a scalar value. - pub fn bottom_typed(&self) -> Length { Length::new(self.bottom) } + pub fn bottom(&self) -> Length { Length::new(self.bottom) } /// Access self.left as a typed Length instead of a scalar value. - pub fn left_typed(&self) -> Length { Length::new(self.left) } + pub fn left(&self) -> Length { 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 { diff --git a/src/vector.rs b/src/vector.rs index 141306b8..abee9973 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -94,11 +94,11 @@ impl TypedVector2D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x(&self) -> Length { Length::new(self.x) } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y(&self) -> Length { Length::new(self.y) } /// Drop the units, preserving only the numeric value. #[inline] @@ -151,6 +151,11 @@ where T: Copy + Mul + Add + Sub { pub fn length(&self) -> T where T: Float + ApproxEq { self.square_length().sqrt() } + + #[inline] + pub fn length_typed(&self) -> Length where T: Float + ApproxEq { + Length::new(self.length()) + } } impl TypedVector2D @@ -440,15 +445,15 @@ impl TypedVector3D { /// Returns self.x as a Length carrying the unit. #[inline] - pub fn x_typed(&self) -> Length { Length::new(self.x) } + pub fn x(&self) -> Length { Length::new(self.x) } /// Returns self.y as a Length carrying the unit. #[inline] - pub fn y_typed(&self) -> Length { Length::new(self.y) } + pub fn y(&self) -> Length { Length::new(self.y) } /// Returns self.z as a Length carrying the unit. #[inline] - pub fn z_typed(&self) -> Length { Length::new(self.z) } + pub fn z(&self) -> Length { Length::new(self.z) } #[inline] pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] }