From 0b0b54f89cce54e5b33f14f9856a3c5a3fb5e361 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Tue, 28 Jul 2020 09:54:17 +0200 Subject: [PATCH] Add getters providing stroingly typed Length values. --- src/box2d.rs | 13 +++++++++++++ src/box3d.rs | 19 +++++++++++++++++++ src/point.rs | 30 ++++++++++++++++++++++++++++++ src/size.rs | 29 +++++++++++++++++++++++++++++ src/vector.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) diff --git a/src/box2d.rs b/src/box2d.rs index 75b20845..4ed4ac5a 100644 --- a/src/box2d.rs +++ b/src/box2d.rs @@ -12,6 +12,7 @@ use crate::approxord::{max, min}; use crate::nonempty::NonEmpty; use crate::num::*; use crate::point::{point2, Point2D}; +use crate::length::Length; use crate::rect::Rect; use crate::scale::Scale; use crate::side_offsets::SideOffsets2D; @@ -232,6 +233,18 @@ where self.max.y - self.min.y } + /// Return this box's width as a strongly typed `Length`. + #[inline] + pub fn get_width(self) -> Length { + Length::new(self.width()) + } + + /// Return this box's height as a strongly typed `Length`. + #[inline] + pub fn get_height(self) -> Length { + Length::new(self.height()) + } + #[inline] pub fn to_rect(&self) -> Rect { Rect { diff --git a/src/box3d.rs b/src/box3d.rs index dc17ddea..9bec92b8 100644 --- a/src/box3d.rs +++ b/src/box3d.rs @@ -12,6 +12,7 @@ use crate::approxord::{max, min}; use crate::nonempty::NonEmpty; use crate::num::*; use crate::point::{point3, Point3D}; +use crate::length::Length; use crate::scale::Scale; use crate::size::Size3D; use crate::vector::Vector3D; @@ -229,6 +230,24 @@ where pub fn depth(&self) -> T { self.max.z - self.min.z } + + /// Return this box's width as a strongly typed `Length`. + #[inline] + pub fn get_width(self) -> Length { + Length::new(self.width()) + } + + /// Return this box's height as a strongly typed `Length`. + #[inline] + pub fn get_height(self) -> Length { + Length::new(self.height()) + } + + /// Return this box's depth as a strongly typed `Length`. + #[inline] + pub fn get_depth(self) -> Length { + Length::new(self.depth()) + } } impl Box3D diff --git a/src/point.rs b/src/point.rs index a6714699..f11b9739 100644 --- a/src/point.rs +++ b/src/point.rs @@ -153,6 +153,18 @@ impl Point2D { pub fn from_untyped(p: Point2D) -> Self { point2(p.x, p.y) } + + /// Return this point's x component as a strongly typed `Length`. + #[inline] + pub fn get_x(self) -> Length { + Length::new(self.x) + } + + /// Return this point's y component as a strongly typed `Length`. + #[inline] + pub fn get_y(self) -> Length { + Length::new(self.y) + } } impl Point2D { @@ -824,6 +836,24 @@ impl Point3D { pub fn from_untyped(p: Point3D) -> Self { point3(p.x, p.y, p.z) } + + /// Return this point's x component as a strongly typed `Length`. + #[inline] + pub fn get_x(self) -> Length { + Length::new(self.x) + } + + /// Return this point's y component as a strongly typed `Length`. + #[inline] + pub fn get_y(self) -> Length { + Length::new(self.y) + } + + /// Return this point's z component as a strongly typed `Length`. + #[inline] + pub fn get_z(self) -> Length { + Length::new(self.z) + } } impl Point3D { diff --git a/src/size.rs b/src/size.rs index 6fe16519..f9c33d98 100644 --- a/src/size.rs +++ b/src/size.rs @@ -149,6 +149,18 @@ impl Size2D { pub fn from_untyped(p: Size2D) -> Self { Size2D::new(p.width, p.height) } + + /// Return this size's width as a strongly typed `Length`. + #[inline] + pub fn get_width(self) -> Length { + Length::new(self.width) + } + + /// Return this size's height as a strongly typed `Length`. + #[inline] + pub fn get_height(self) -> Length { + Length::new(self.height) + } } impl Size2D { @@ -982,6 +994,23 @@ impl Size3D { pub fn from_untyped(p: Size3D) -> Self { Size3D::new(p.width, p.height, p.depth) } + + /// Return this size's width as a strongly typed `Length`. + #[inline] + pub fn get_width(self) -> Length { + Length::new(self.width) + } + + /// Return this size's height as a strongly typed `Length`. + #[inline] + pub fn get_height(self) -> Length { + Length::new(self.height) + } + /// Return this size's depth as a strongly typed `Length`. + #[inline] + pub fn get_depth(self) -> Length { + Length::new(self.depth) + } } impl Size3D { diff --git a/src/vector.rs b/src/vector.rs index 35ea9e0f..aa5af3d2 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -205,6 +205,18 @@ impl Vector2D { { self.x * other.y - self.y * other.x } + + /// Return this vector's x component as a strongly typed `Length`. + #[inline] + pub fn get_x(self) -> Length { + Length::new(self.x) + } + + /// Return this vector's y component as a strongly typed `Length`. + #[inline] + pub fn get_y(self) -> Length { + Length::new(self.y) + } } impl Vector2D { @@ -389,6 +401,12 @@ impl Vector2D { self.square_length().sqrt() } + /// Returns the vector length as a strongly typed `Length`. + #[inline] + pub fn get_length(self) -> Length { + Length::new(self.length()) + } + /// Returns the vector with length of one unit. #[inline] #[must_use] @@ -973,6 +991,24 @@ impl Vector3D { { self.x * other.x + self.y * other.y + self.z * other.z } + + /// Return this vector's x component as a strongly typed `Length`. + #[inline] + pub fn get_x(self) -> Length { + Length::new(self.x) + } + + /// Return this vector's y component as a strongly typed `Length`. + #[inline] + pub fn get_y(self) -> Length { + Length::new(self.y) + } + + /// Return this vector's z component as a strongly typed `Length`. + #[inline] + pub fn get_z(self) -> Length { + Length::new(self.z) + } } impl Vector3D { @@ -1172,6 +1208,12 @@ impl Vector3D { self.square_length().sqrt() } + /// Returns the vector length as a strongly typed `Length`. + #[inline] + pub fn get_length(self) -> Length { + Length::new(self.length()) + } + /// Returns the vector with length of one unit #[inline] #[must_use]