Skip to content

Commit d8c5240

Browse files
committed
Make accessing members as lengths more ergonomic.
1 parent 07c1b53 commit d8c5240

File tree

7 files changed

+46
-34
lines changed

7 files changed

+46
-34
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
@@ -86,11 +86,11 @@ impl<T: Copy, U> TypedPoint2D<T, U> {
8686

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

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

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

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

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

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

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

src/rect.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,22 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
130130
}
131131

132132
#[inline]
133-
pub fn max_x_typed(&self) -> Length<T, U> {
133+
pub fn get_max_x(&self) -> Length<T, U> {
134134
Length::new(self.max_x())
135135
}
136136

137137
#[inline]
138-
pub fn min_x_typed(&self) -> Length<T, U> {
138+
pub fn get_min_x(&self) -> Length<T, U> {
139139
Length::new(self.min_x())
140140
}
141141

142142
#[inline]
143-
pub fn max_y_typed(&self) -> Length<T, U> {
143+
pub fn get_max_y(&self) -> Length<T, U> {
144144
Length::new(self.max_y())
145145
}
146146

147147
#[inline]
148-
pub fn min_y_typed(&self) -> Length<T, U> {
148+
pub fn get_min_y(&self) -> Length<T, U> {
149149
Length::new(self.min_y())
150150
}
151151

@@ -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: 6 additions & 6 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 {
@@ -93,11 +93,11 @@ impl<T, U> TypedSideOffsets2D<T, U> where T: Add<T, Output=T> + Copy {
9393
self.top + self.bottom
9494
}
9595

96-
pub fn horizontal_typed(&self) -> Length<T, U> {
96+
pub fn get_horizontal(&self) -> Length<T, U> {
9797
Length::new(self.horizontal())
9898
}
9999

100-
pub fn vertical_typed(&self) -> Length<T, U> {
100+
pub fn get_vertical(&self) -> Length<T, U> {
101101
Length::new(self.vertical())
102102
}
103103
}

src/size.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ impl<T: Copy + Div<T, Output=T>, U1, U2> Div<ScaleFactor<T, U1, U2>> for TypedSi
174174
impl<T: Copy, U> TypedSize2D<T, U> {
175175
/// Returns self.width as a Length carrying the unit.
176176
#[inline]
177-
pub fn width_typed(&self) -> Length<T, U> { Length::new(self.width) }
177+
pub fn get_width(&self) -> Length<T, U> { Length::new(self.width) }
178178

179179
/// Returns self.height as a Length carrying the unit.
180180
#[inline]
181-
pub fn height_typed(&self) -> Length<T, U> { Length::new(self.height) }
181+
pub fn get_height(&self) -> Length<T, U> { Length::new(self.height) }
182182

183183
#[inline]
184184
pub fn to_array(&self) -> [T; 2] { [self.width, self.height] }

src/vector.rs

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

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

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

9797
/// Drop the units, preserving only the numeric value.
9898
#[inline]
@@ -145,6 +145,16 @@ where T: Copy + Mul<T, Output=T> + Add<T, Output=T> + Sub<T, Output=T> {
145145
pub fn length(&self) -> T where T: Float + ApproxEq<T> {
146146
self.square_length().sqrt()
147147
}
148+
149+
#[inline]
150+
pub fn get_square_length(&self) -> Length<T, U> {
151+
Length::new(self.x * self.x + self.y * self.y)
152+
}
153+
154+
#[inline]
155+
pub fn get_length(&self) -> Length<T, U> where T: Float + ApproxEq<T> {
156+
Length::new(self.length())
157+
}
148158
}
149159

150160
impl<T, U> TypedVector2D<T, U>
@@ -434,15 +444,15 @@ impl<T: Copy, U> TypedVector3D<T, U> {
434444

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

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

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

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

0 commit comments

Comments
 (0)