Skip to content

Commit 44c7580

Browse files
committed
Make accessing members as lengths more ergonomic.
1 parent a738346 commit 44c7580

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

src/length.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ where T: Copy + One + Add<Output=T> + Sub<Output=T> + Mul<Output=T> {
206206
}
207207
}
208208

209+
impl<T, U> From<T> for Length<T, U> {
210+
fn from(val: T) -> Self {
211+
Length::new(val)
212+
}
213+
}
214+
209215
#[cfg(test)]
210216
mod tests {
211217
use super::Length;

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
//! let p = WorldPoint::new(0.0, 1.0, 1.0);
5050
//! // p.x is an f32.
5151
//! println!("p.x = {:?} ", p.x);
52-
//! // p.x is a Length<f32, WorldSpace>.
53-
//! println!("p.x_typed() = {:?} ", p.x_typed());
52+
//! // p.x() is a Length<f32, WorldSpace>.
53+
//! println!("p.x() = {:?} ", p.x());
5454
//! // Length::get returns the scalar value (f32).
55-
//! assert_eq!(p.x, p.x_typed().get());
55+
//! assert_eq!(p.x, p.x().get());
5656
//! ```
5757
5858
extern crate heapsize;

src/point.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ impl<T: Copy, U> TypedPoint2D<T, U> {
9292

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

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

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

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

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

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

454454
#[inline]
455455
pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] }

src/rect.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,15 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
192192

193193
#[inline]
194194
#[must_use]
195-
pub fn inflate(&self, width: T, height: T) -> Self {
195+
pub fn inflate<T2: Into<Length<T, U>>>(&self, width: T2, height: T2) -> Self {
196+
let w = width.into().get();
197+
let h = height.into().get();
196198
TypedRect::new(
197-
TypedPoint2D::new(self.origin.x - width, self.origin.y - height),
198-
TypedSize2D::new(self.size.width + width + width, self.size.height + height + height),
199+
TypedPoint2D::new(self.origin.x - w, self.origin.y - h),
200+
TypedSize2D::new(self.size.width + w + w, self.size.height + h + h),
199201
)
200202
}
201203

202-
#[inline]
203-
#[must_use]
204-
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> Self {
205-
self.inflate(width.get(), height.get())
206-
}
207-
208204
#[inline]
209205
pub fn top_right(&self) -> TypedPoint2D<T, U> {
210206
TypedPoint2D::new(self.max_x(), self.origin.y)

src/side_offsets.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ impl<T: Copy, U> TypedSideOffsets2D<T, U> {
6262
}
6363

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

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

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

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

7676
/// Constructor setting the same value to all sides, taking a scalar value directly.
7777
pub fn new_all_same(all: T) -> Self {

src/vector.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ impl<T: Copy, U> TypedVector2D<T, U> {
9494

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

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

103103
/// Drop the units, preserving only the numeric value.
104104
#[inline]
@@ -151,6 +151,11 @@ where T: Copy + Mul<T, Output=T> + Add<T, Output=T> + Sub<T, Output=T> {
151151
pub fn length(&self) -> T where T: Float + ApproxEq<T> {
152152
self.square_length().sqrt()
153153
}
154+
155+
#[inline]
156+
pub fn length_typed(&self) -> Length<T, U> where T: Float + ApproxEq<T> {
157+
Length::new(self.length())
158+
}
154159
}
155160

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

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

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

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

453458
#[inline]
454459
pub fn to_array(&self) -> [T; 3] { [self.x, self.y, self.z] }

0 commit comments

Comments
 (0)