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

Bump to 0.9.0 #156

Merged
merged 2 commits into from Aug 12, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Uniformise all trait impls

Some cannot be derived because the derivation include bounds on unit types.
  • Loading branch information
nox committed Aug 12, 2016
commit c41c850abdb492cd3cb2d8bac5de09a65002bdff
@@ -36,9 +36,17 @@ pub struct UnknownUnit;
/// another. See the `ScaleFactor` docs for an example.
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
#[derive(Clone, Copy, RustcDecodable, RustcEncodable)]
#[derive(RustcDecodable, RustcEncodable)]
pub struct Length<T, Unit>(pub T, PhantomData<Unit>);

impl<T: Clone, Unit> Clone for Length<T, Unit> {
fn clone(&self) -> Self {
Length(self.0.clone(), PhantomData)
}
}

impl<T: Copy, Unit> Copy for Length<T, Unit> {}

impl<Unit, T: HeapSizeOf> HeapSizeOf for Length<T, Unit> {
fn heap_size_of_children(&self) -> usize {
self.0.heap_size_of_children()
@@ -182,9 +190,7 @@ mod tests {
use super::Length;
use scale_factor::ScaleFactor;

#[derive(Debug, Copy, Clone)]
enum Inch {}
#[derive(Debug, Copy, Clone)]
enum Mm {}

#[test]
@@ -20,6 +20,17 @@ macro_rules! define_matrix {
_unit: PhantomData<($($phantom),+)>
}

impl<T: Clone, $($phantom),+> Clone for $name<T, $($phantom),+> {
fn clone(&self) -> Self {
$name {
$($field: self.$field.clone(),)+
_unit: PhantomData,
}
}
}

impl<T: Copy, $($phantom),+> Copy for $name<T, $($phantom),+> {}

impl<T, $($phantom),+> ::heapsize::HeapSizeOf for $name<T, $($phantom),+>
where T: ::heapsize::HeapSizeOf
{
@@ -52,5 +63,24 @@ macro_rules! define_matrix {
($(&self.$field,)+).serialize(serializer)
}
}

impl<T, $($phantom),+> ::std::cmp::Eq for $name<T, $($phantom),+>
where T: ::std::cmp::Eq {}

impl<T, $($phantom),+> ::std::cmp::PartialEq for $name<T, $($phantom),+>
where T: ::std::cmp::PartialEq
{
fn eq(&self, other: &Self) -> bool {
true $(&& self.$field == other.$field)+
}
}

impl<T, $($phantom),+> ::std::hash::Hash for $name<T, $($phantom),+>
where T: ::std::hash::Hash
{
fn hash<H: ::std::hash::Hasher>(&self, h: &mut H) {
$(self.$field.hash(h);)+
}
}
)
}
@@ -16,7 +16,6 @@ use std::ops::{Add, Mul, Sub};
use std::marker::PhantomData;

define_matrix! {
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct TypedMatrix2D<T, Src, Dst> {
pub m11: T, pub m12: T,
pub m21: T, pub m22: T,
@@ -47,12 +47,6 @@ impl<T, Src, Dst> TypedMatrix4D<T, Src, Dst> {
}
}

impl<T: Copy, Src, Dst> Copy for TypedMatrix4D<T, Src, Dst> {}

impl<T: Copy, Src, Dst> Clone for TypedMatrix4D<T, Src, Dst> {
fn clone(&self) -> Self { *self }
}

impl <T, Src, Dst> TypedMatrix4D<T, Src, Dst>
where T: Copy + Clone +
Add<T, Output=T> +
@@ -423,15 +417,6 @@ impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix4D<T, Src, Dst> {
}
}

impl<T: PartialEq, Src, Dst> PartialEq for TypedMatrix4D<T, Src, Dst> {
fn eq(&self, other: &Self) -> bool {
self.m11 == other.m11 && self.m12 == other.m12 && self.m13 == other.m13 && self.m14 == other.m14 &&
self.m21 == other.m21 && self.m22 == other.m22 && self.m23 == other.m23 && self.m24 == other.m24 &&
self.m31 == other.m31 && self.m32 == other.m32 && self.m33 == other.m33 && self.m34 == other.m34 &&
self.m41 == other.m41 && self.m42 == other.m42 && self.m43 == other.m43 && self.m44 == other.m44
}
}

#[cfg(test)]
mod tests {
use point::Point2D;
@@ -16,8 +16,6 @@ use num_traits::{Float, NumCast};
use std::fmt;
use std::ops::{Add, Neg, Mul, Sub, Div};
use std::marker::PhantomData;
use std::cmp::{PartialEq, Eq};
use std::hash::{Hash, Hasher};

define_matrix! {
#[derive(RustcDecodable, RustcEncodable)]
@@ -29,29 +27,6 @@ define_matrix! {

pub type Point2D<T> = TypedPoint2D<T, UnknownUnit>;

impl<T: Copy, U> Copy for TypedPoint2D<T, U> {}

impl<T: Clone, U> Clone for TypedPoint2D<T, U> {
fn clone(&self) -> TypedPoint2D<T, U> {
TypedPoint2D::new(self.x.clone(), self.y.clone())
}
}

impl<T: PartialEq, U> PartialEq<TypedPoint2D<T, U>> for TypedPoint2D<T, U> {
fn eq(&self, other: &TypedPoint2D<T, U>) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y)
}
}

impl<T: Eq, U> Eq for TypedPoint2D<T, U> {}

impl<T: Hash, U> Hash for TypedPoint2D<T, U> {
fn hash<H: Hasher>(&self, h: &mut H) {
self.x.hash(h);
self.y.hash(h);
}
}

impl<T: Zero, U> TypedPoint2D<T, U> {
pub fn zero() -> TypedPoint2D<T, U> {
TypedPoint2D::new(Zero::zero(), Zero::zero())
@@ -223,37 +198,13 @@ define_matrix! {

pub type Point3D<T> = TypedPoint3D<T, UnknownUnit>;

impl<T: Hash, U> Hash for TypedPoint3D<T, U> {
fn hash<H: Hasher>(&self, h: &mut H) {
self.x.hash(h);
self.y.hash(h);
self.z.hash(h);
}
}

impl<T: Zero, U> TypedPoint3D<T, U> {
#[inline]
pub fn zero() -> TypedPoint3D<T, U> {
TypedPoint3D::new(Zero::zero(), Zero::zero(), Zero::zero())
}
}

impl<T: Copy, U> Copy for TypedPoint3D<T, U> {}

impl<T: Clone, U> Clone for TypedPoint3D<T, U> {
fn clone(&self) -> TypedPoint3D<T, U> {
TypedPoint3D::new(self.x.clone(), self.y.clone(), self.z.clone())
}
}

impl<T: PartialEq, U> PartialEq<TypedPoint3D<T, U>> for TypedPoint3D<T, U> {
fn eq(&self, other: &TypedPoint3D<T, U>) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y) && self.z.eq(&other.z)
}
}

impl<T: Eq, U> Eq for TypedPoint3D<T, U> {}

impl<T: fmt::Debug, U> fmt::Debug for TypedPoint3D<T, U> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?},{:?},{:?})", self.x, self.y, self.z)
@@ -367,34 +318,6 @@ define_matrix! {

pub type Point4D<T> = TypedPoint4D<T, UnknownUnit>;

impl<T: Copy, U> Copy for TypedPoint4D<T, U> {}

impl<T: Clone, U> Clone for TypedPoint4D<T, U> {
fn clone(&self) -> TypedPoint4D<T, U> {
TypedPoint4D::new(self.x.clone(),
self.y.clone(),
self.z.clone(),
self.w.clone())
}
}

impl<T: PartialEq, U> PartialEq<TypedPoint4D<T, U>> for TypedPoint4D<T, U> {
fn eq(&self, other: &TypedPoint4D<T, U>) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y) && self.z.eq(&other.z) && self.w.eq(&other.w)
}
}

impl<T: Eq, U> Eq for TypedPoint4D<T, U> {}

impl<T: Hash, U> Hash for TypedPoint4D<T, U> {
fn hash<H: Hasher>(&self, h: &mut H) {
self.x.hash(h);
self.y.hash(h);
self.z.hash(h);
self.w.hash(h);
}
}

impl<T: Zero, U> TypedPoint4D<T, U> {
#[inline]
pub fn zero() -> TypedPoint4D<T, U> {
@@ -543,9 +466,7 @@ mod typedpoint2d {
use super::TypedPoint2D;
use scale_factor::ScaleFactor;

#[derive(Debug, Copy, Clone)]
pub enum Mm {}
#[derive(Debug, Copy, Clone)]
pub enum Cm {}

pub type Point2DMm<T> = TypedPoint2D<T, Mm>;
@@ -13,6 +13,7 @@ use num::One;
use heapsize::HeapSizeOf;
use num_traits::NumCast;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::ops::{Add, Mul, Sub, Div};
use std::marker::PhantomData;

@@ -35,9 +36,7 @@ use std::marker::PhantomData;
/// let one_foot: Length<f32, Inch> = Length::new(12.0);
/// let one_foot_in_mm: Length<f32, Mm> = one_foot * mm_per_inch;
/// ```
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
#[derive(Copy, RustcDecodable, RustcEncodable, Debug)]
#[derive(RustcDecodable, RustcEncodable)]
pub struct ScaleFactor<T, Src, Dst>(pub T, PhantomData<(Src, Dst)>);

impl<T: HeapSizeOf, Src, Dst> HeapSizeOf for ScaleFactor<T, Src, Dst> {
@@ -117,9 +116,9 @@ impl<T: NumCast + Clone, Src, Dst0> ScaleFactor<T, Src, Dst0> {
// FIXME: Switch to `derive(PartialEq, Clone)` after this Rust issue is fixed:
// https://github.com/mozilla/rust/issues/7671

impl<T: Clone + PartialEq, Src, Dst> PartialEq for ScaleFactor<T, Src, Dst> {
impl<T: PartialEq, Src, Dst> PartialEq for ScaleFactor<T, Src, Dst> {
fn eq(&self, other: &ScaleFactor<T, Src, Dst>) -> bool {
self.get().eq(&other.get())
self.0 == other.0
}
}

@@ -129,15 +128,26 @@ impl<T: Clone, Src, Dst> Clone for ScaleFactor<T, Src, Dst> {
}
}

impl<T: Copy, Src, Dst> Copy for ScaleFactor<T, Src, Dst> {}

impl<T: fmt::Debug, Src, Dst> fmt::Debug for ScaleFactor<T, Src, Dst> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T: fmt::Display, Src, Dst> fmt::Display for ScaleFactor<T, Src, Dst> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

#[cfg(test)]
mod tests {
use super::ScaleFactor;

#[derive(Debug)]
enum Inch {}
#[derive(Debug)]
enum Cm {}
#[derive(Debug)]
enum Mm {}

#[test]
@@ -11,6 +11,7 @@
//! and margins in CSS.

use num::Zero;
use std::fmt;
use std::ops::Add;
use std::marker::PhantomData;
use length::{Length, UnknownUnit};
@@ -21,7 +22,6 @@ use heapsize::HeapSizeOf;
/// A group of side offsets, which correspond to top/left/bottom/right for borders, padding,
/// and margins in CSS.
define_matrix! {
#[derive(Clone, Debug)]
pub struct TypedSideOffsets2D<T, U> {
pub top: T,
pub right: T,
@@ -30,6 +30,13 @@ define_matrix! {
}
}

impl<T: fmt::Debug, U> fmt::Debug for TypedSideOffsets2D<T, U> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?},{:?},{:?},{:?})",
self.top, self.right, self.bottom, self.left)
}
}

pub type SideOffsets2D<T> = TypedSideOffsets2D<T, UnknownUnit>;

impl<T, U> TypedSideOffsets2D<T, U> {
@@ -26,22 +26,6 @@ define_matrix! {

pub type Size2D<T> = TypedSize2D<T, UnknownUnit>;

impl<T: Copy, U> Copy for TypedSize2D<T, U> {}

impl<T: Clone, U> Clone for TypedSize2D<T, U> {
fn clone(&self) -> TypedSize2D<T, U> {
TypedSize2D::new(self.width.clone(), self.height.clone())
}
}

impl<T: PartialEq, U> PartialEq<TypedSize2D<T, U>> for TypedSize2D<T, U> {
fn eq(&self, other: &TypedSize2D<T, U>) -> bool {
self.width.eq(&other.width) && self.height.eq(&other.height)
}
}

impl<T: Eq, U> Eq for TypedSize2D<T, U> {}

impl<T: fmt::Debug, U> fmt::Debug for TypedSize2D<T, U> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}×{:?}", self.width, self.height)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.