Skip to content
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

Add getters providing strongly typed Length values #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T, U> {
Length::new(self.width())
}

/// Return this box's height as a strongly typed `Length`.
#[inline]
pub fn get_height(self) -> Length<T, U> {
Length::new(self.height())
}

#[inline]
pub fn to_rect(&self) -> Rect<T, U> {
Rect {
Expand Down
19 changes: 19 additions & 0 deletions src/box3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T, U> {
Length::new(self.width())
}

/// Return this box's height as a strongly typed `Length`.
#[inline]
pub fn get_height(self) -> Length<T, U> {
Length::new(self.height())
}

/// Return this box's depth as a strongly typed `Length`.
#[inline]
pub fn get_depth(self) -> Length<T, U> {
Length::new(self.depth())
}
}

impl<T, U> Box3D<T, U>
Expand Down
30 changes: 30 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ impl<T, U> Point2D<T, U> {
pub fn from_untyped(p: Point2D<T, UnknownUnit>) -> Self {
point2(p.x, p.y)
}

/// Return this point's x component as a strongly typed `Length`.
#[inline]
pub fn get_x(self) -> Length<T, U> {
Length::new(self.x)
}

/// Return this point's y component as a strongly typed `Length`.
#[inline]
pub fn get_y(self) -> Length<T, U> {
Length::new(self.y)
}
}

impl<T: Copy, U> Point2D<T, U> {
Expand Down Expand Up @@ -824,6 +836,24 @@ impl<T, U> Point3D<T, U> {
pub fn from_untyped(p: Point3D<T, UnknownUnit>) -> 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<T, U> {
Length::new(self.x)
}

/// Return this point's y component as a strongly typed `Length`.
#[inline]
pub fn get_y(self) -> Length<T, U> {
Length::new(self.y)
}

/// Return this point's z component as a strongly typed `Length`.
#[inline]
pub fn get_z(self) -> Length<T, U> {
Length::new(self.z)
}
}

impl<T: Copy, U> Point3D<T, U> {
Expand Down
29 changes: 29 additions & 0 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ impl<T, U> Size2D<T, U> {
pub fn from_untyped(p: Size2D<T, UnknownUnit>) -> Self {
Size2D::new(p.width, p.height)
}

/// Return this size's width as a strongly typed `Length`.
#[inline]
pub fn get_width(self) -> Length<T, U> {
Length::new(self.width)
}

/// Return this size's height as a strongly typed `Length`.
#[inline]
pub fn get_height(self) -> Length<T, U> {
Length::new(self.height)
}
}

impl<T: Copy, U> Size2D<T, U> {
Expand Down Expand Up @@ -982,6 +994,23 @@ impl<T, U> Size3D<T, U> {
pub fn from_untyped(p: Size3D<T, UnknownUnit>) -> 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<T, U> {
Length::new(self.width)
}

/// Return this size's height as a strongly typed `Length`.
#[inline]
pub fn get_height(self) -> Length<T, U> {
Length::new(self.height)
}
/// Return this size's depth as a strongly typed `Length`.
#[inline]
pub fn get_depth(self) -> Length<T, U> {
Length::new(self.depth)
}
}

impl<T: Copy, U> Size3D<T, U> {
Expand Down
42 changes: 42 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ impl<T, U> Vector2D<T, U> {
{
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<T, U> {
Length::new(self.x)
}

/// Return this vector's y component as a strongly typed `Length`.
#[inline]
pub fn get_y(self) -> Length<T, U> {
Length::new(self.y)
}
}

impl<T: Copy, U> Vector2D<T, U> {
Expand Down Expand Up @@ -389,6 +401,12 @@ impl<T: Float, U> Vector2D<T, U> {
self.square_length().sqrt()
}

/// Returns the vector length as a strongly typed `Length`.
#[inline]
pub fn get_length(self) -> Length<T, U> {
Length::new(self.length())
}

/// Returns the vector with length of one unit.
#[inline]
#[must_use]
Expand Down Expand Up @@ -973,6 +991,24 @@ impl<T, U> Vector3D<T, U> {
{
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<T, U> {
Length::new(self.x)
}

/// Return this vector's y component as a strongly typed `Length`.
#[inline]
pub fn get_y(self) -> Length<T, U> {
Length::new(self.y)
}

/// Return this vector's z component as a strongly typed `Length`.
#[inline]
pub fn get_z(self) -> Length<T, U> {
Length::new(self.z)
}
}

impl<T: Copy, U> Vector3D<T, U> {
Expand Down Expand Up @@ -1172,6 +1208,12 @@ impl<T: Float, U> Vector3D<T, U> {
self.square_length().sqrt()
}

/// Returns the vector length as a strongly typed `Length`.
#[inline]
pub fn get_length(self) -> Length<T, U> {
Length::new(self.length())
}

/// Returns the vector with length of one unit
#[inline]
#[must_use]
Expand Down