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

Improve documentation. #178

Merged
merged 1 commit into from Mar 24, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Improve documentation.

This fixes some typos, some copy/paste errors, and makes more
liberal use of code backticks.
  • Loading branch information
waywardmonkeys committed Mar 24, 2017
commit 0e8e207c837d5d6c28891434d2d917763c8b9a78
@@ -22,14 +22,14 @@ use std::fmt;

/// A one-dimensional distance, with value represented by `T` and unit of measurement `Unit`.
///
/// `T` can be any numeric type, for example a primitive type like u64 or f32.
/// `T` can be any numeric type, for example a primitive type like `u64` or `f32`.
///
/// `Unit` is not used in the representation of a Length value. It is used only at compile time
/// to ensure that a Length stored with one unit is converted explicitly before being used in an
/// `Unit` is not used in the representation of a `Length` value. It is used only at compile time
/// to ensure that a `Length` stored with one unit is converted explicitly before being used in an
/// expression that requires a different unit. It may be a type without values, such as an empty
/// enum.
///
/// You can multiply a Length by a `scale_factor::ScaleFactor` to convert it from one unit to
/// You can multiply a `Length` by a `scale_factor::ScaleFactor` to convert it from one unit to
/// another. See the `ScaleFactor` docs for an example.
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
@@ -12,7 +12,7 @@
//! A collection of strongly typed math tools for computer graphics with an inclination
//! towards 2d graphics and layout.
//!
//! All types are generic over the the scalar type of their component (f32, i32, etc.),
//! All types are generic over the scalar type of their component (`f32`, `i32`, etc.),
//! and tagged with a generic Unit parameter which is useful to prevent mixing
//! values from different spaces. For example it should not be legal to translate
//! a screen-space position by a world-space vector and this can be expressed using
@@ -24,8 +24,8 @@
//! Client code typically creates a set of aliases for each type and doesn't need
//! to deal with the specifics of typed units further. For example:
//!
//! All euclid types are marked #[repr(C)] in order to facilitate exposing them to
//! foreign function interfaces (provided the underlying scalar type is also repr(C)).
//! All euclid types are marked `#[repr(C)]` in order to facilitate exposing them to
//! foreign function interfaces (provided the underlying scalar type is also `repr(C)`).
//!
//! ```rust
//! use euclid::*;
@@ -23,13 +23,13 @@ define_matrix! {
///
/// Matrices can be parametrized over the source and destination units, to describe a
/// transformation from a space to another.
/// For example, TypedMatrix2D<f32, WordSpace, ScreenSpace>::transform_point4d
/// takes a TypedPoint2D<f32, WordSpace> and returns a TypedPoint2D<f32, ScreenSpace>.
/// For example, `TypedMatrix2D<f32, WordSpace, ScreenSpace>::transform_point4d`
/// takes a `TypedPoint2D<f32, WordSpace>` and returns a `TypedPoint2D<f32, ScreenSpace>`.
///
/// Matrices expose a set of convenience methods for pre- and post-transformations.
/// A pre-transformation corresponds to adding an operation that is applied before
/// the rest of the transformation, while a post-transformation adds an operation
/// that is appled after.
/// that is applied after.
pub struct TypedMatrix2D<T, Src, Dst> {
pub m11: T, pub m12: T,
pub m21: T, pub m22: T,
@@ -25,13 +25,13 @@ define_matrix! {
///
/// Matrices can be parametrized over the source and destination units, to describe a
/// transformation from a space to another.
/// For example, TypedMatrix4D<f32, WordSpace, ScreenSpace>::transform_point4d
/// takes a TypedPoint4D<f32, WordSpace> and returns a TypedPoint4D<f32, ScreenSpace>.
/// For example, `TypedMatrix4D<f32, WordSpace, ScreenSpace>::transform_point4d`
/// takes a `TypedPoint4D<f32, WordSpace>` and returns a `TypedPoint4D<f32, ScreenSpace>`.
///
/// Matrices expose a set of convenience methods for pre- and post-transformations.
/// A pre-transformation corresponds to adding an operation that is applied before
/// the rest of the transformation, while a post-transformation adds an operation
/// that is appled after.
/// that is applied after.
pub struct TypedMatrix4D<T, Src, Dst> {
pub m11: T, pub m12: T, pub m13: T, pub m14: T,
pub m21: T, pub m22: T, pub m23: T, pub m24: T,
@@ -193,27 +193,27 @@ impl<T: Round, U> TypedPoint2D<T, U> {
/// Rounds each component to the nearest integer value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example { -0.1, -0.8 }.round() == { 0.0, -1.0 }
/// For example `{ -0.1, -0.8 }.round() == { 0.0, -1.0 }`.
pub fn round(&self) -> Self {
TypedPoint2D::new(self.x.round(), self.y.round())
}
}

impl<T: Ceil, U> TypedPoint2D<T, U> {
/// Rounds each component to the smallest integer equal or greater than the orginal value.
/// Rounds each component to the smallest integer equal or greater than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example { -0.1, -0.8 }.ceil() == { 0.0, 0.0 }.
/// For example `{ -0.1, -0.8 }.ceil() == { 0.0, 0.0 }`.
pub fn ceil(&self) -> Self {
TypedPoint2D::new(self.x.ceil(), self.y.ceil())
}
}

impl<T: Floor, U> TypedPoint2D<T, U> {
/// Rounds each component to the biggest integer equal or lower than the orginal value.
/// Rounds each component to the biggest integer equal or lower than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example { -0.1, -0.8 }.floor() == { -1.0, -1.0 }.
/// For example `{ -0.1, -0.8 }.floor() == { -1.0, -1.0 }`.
pub fn floor(&self) -> Self {
TypedPoint2D::new(self.x.floor(), self.y.floor())
}
@@ -224,7 +224,7 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using round(), ceil or floor() before casting.
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
pub fn cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint2D<NewT, U>> {
match (NumCast::from(self.x), NumCast::from(self.y)) {
(Some(x), Some(y)) => Some(TypedPoint2D::new(x, y)),
@@ -234,34 +234,34 @@ impl<T: NumCast + Copy, U> TypedPoint2D<T, U> {

// Convenience functions for common casts

/// Cast into an f32 vector.
/// Cast into an `f32` point.
pub fn to_f32(&self) -> TypedPoint2D<f32, U> {
self.cast().unwrap()
}

/// Cast into an usize point, truncating decimals if any.
/// Cast into an `usize` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_uint(&self) -> TypedPoint2D<usize, U> {
self.cast().unwrap()
}

/// Cast into an i32 point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i32(&self) -> TypedPoint2D<i32, U> {
self.cast().unwrap()
}

/// Cast into an i64 point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i64(&self) -> TypedPoint2D<i64, U> {
self.cast().unwrap()
}
@@ -437,7 +437,7 @@ impl<T: Round, U> TypedPoint3D<T, U> {
}

impl<T: Ceil, U> TypedPoint3D<T, U> {
/// Rounds each component to the smallest integer equal or greater than the orginal value.
/// Rounds each component to the smallest integer equal or greater than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
pub fn ceil(&self) -> Self {
@@ -446,7 +446,7 @@ impl<T: Ceil, U> TypedPoint3D<T, U> {
}

impl<T: Floor, U> TypedPoint3D<T, U> {
/// Rounds each component to the biggest integer equal or lower than the orginal value.
/// Rounds each component to the biggest integer equal or lower than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
pub fn floor(&self) -> Self {
@@ -471,34 +471,34 @@ impl<T: NumCast + Copy, U> TypedPoint3D<T, U> {

// Convenience functions for common casts

/// Cast into an f32 vector.
/// Cast into an `f32` point.
pub fn to_f32(&self) -> TypedPoint3D<f32, U> {
self.cast().unwrap()
}

/// Cast into an usize point, truncating decimals if any.
/// Cast into an `usize` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_uint(&self) -> TypedPoint3D<usize, U> {
self.cast().unwrap()
}

/// Cast into an i32 point, truncating decimals if any.
/// Cast into an `i32` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i32(&self) -> TypedPoint3D<i32, U> {
self.cast().unwrap()
}

/// Cast into an i64 point, truncating decimals if any.
/// Cast into an `i64` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i64(&self) -> TypedPoint3D<i64, U> {
self.cast().unwrap()
}
@@ -675,7 +675,7 @@ impl<T: Round, U> TypedPoint4D<T, U> {
}

impl<T: Ceil, U> TypedPoint4D<T, U> {
/// Rounds each component to the smallest integer equal or greater than the orginal value.
/// Rounds each component to the smallest integer equal or greater than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
pub fn ceil(&self) -> Self {
@@ -684,7 +684,7 @@ impl<T: Ceil, U> TypedPoint4D<T, U> {
}

impl<T: Floor, U> TypedPoint4D<T, U> {
/// Rounds each component to the biggest integer equal or lower than the orginal value.
/// Rounds each component to the biggest integer equal or lower than the original value.
///
/// This behavior is preserved for negative values (unlike the basic cast).
pub fn floor(&self) -> Self {
@@ -697,7 +697,7 @@ impl<T: NumCast + Copy, U> TypedPoint4D<T, U> {
///
/// When casting from floating point to integer coordinates, the decimals are truncated
/// as one would expect from a simple cast, but this behavior does not always make sense
/// geometrically. Consider using round(), ceil or floor() before casting.
/// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
pub fn cast<NewT: NumCast + Copy>(&self) -> Option<TypedPoint4D<NewT, U>> {
match (NumCast::from(self.x),
NumCast::from(self.y),
@@ -710,34 +710,34 @@ impl<T: NumCast + Copy, U> TypedPoint4D<T, U> {

// Convenience functions for common casts

/// Cast into an f32 vector.
/// Cast into an `f32` point.
pub fn to_f32(&self) -> TypedPoint4D<f32, U> {
self.cast().unwrap()
}

/// Cast into an usize point, truncating decimals if any.
/// Cast into an `usize` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_uint(&self) -> TypedPoint4D<usize, U> {
self.cast().unwrap()
}

/// Cast into an i32 point, truncating decimals if any.
/// Cast into an `i32` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i32(&self) -> TypedPoint4D<i32, U> {
self.cast().unwrap()
}

/// Cast into an i64 point, truncating decimals if any.
/// Cast into an `i64` point, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), ceil() or floor() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point points, it is worth considering whether
/// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
/// the desired conversion behavior.
pub fn to_i64(&self) -> TypedPoint4D<i64, U> {
self.cast().unwrap()
}
@@ -394,40 +394,40 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect

// Convenience functions for common casts
impl<T: NumCast + Copy, Unit> TypedRect<T, Unit> {
/// Cast into an f32 vector.
/// Cast into an `f32` rectangle.
pub fn to_f32(&self) -> TypedRect<f32, Unit> {
self.cast().unwrap()
}

/// Cast into an usize vector, truncating decimals if any.
/// Cast into an `usize` rectangle, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), round_in() or round_out() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point rectangles, it is worth considering whether
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_uint(&self) -> TypedRect<usize, Unit> {
self.cast().unwrap()
}

/// Cast into an i32 vector, truncating decimals if any.
/// Cast into an `i32` rectangle, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), round_in() or round_out() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point rectangles, it is worth considering whether
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_i32(&self) -> TypedRect<i32, Unit> {
self.cast().unwrap()
}

/// Cast into an i64 vector, truncating decimals if any.
/// Cast into an `i64` rectangle, truncating decimals if any.
///
/// When casting from floating point vectors, it is worth considering whether
/// to round(), round_in() or round_out() before the cast in order to obtain the desired
/// conversion behavior.
/// When casting from floating point rectangles, it is worth considering whether
/// to `round()`, `round_in()` or `round_out()` before the cast in order to
/// obtain the desired conversion behavior.
pub fn to_i64(&self) -> TypedRect<i64, Unit> {
self.cast().unwrap()
}
}

/// Shorthand for TypedRect::new(TypedPoint2D::new(x, y), TypedSize2D::new(w, h)).
/// Shorthand for `TypedRect::new(TypedPoint2D::new(x, y), TypedSize2D::new(w, h))`.
pub fn rect<T: Copy, U>(x: T, y: T, w: T, h: T) -> TypedRect<T, U> {
TypedRect::new(TypedPoint2D::new(x, y), TypedSize2D::new(w, h))
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.