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

Use Self whenever possible. #207

Merged
merged 1 commit into from Jun 5, 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

@@ -52,7 +52,7 @@ impl<Unit, T: HeapSizeOf> HeapSizeOf for Length<T, Unit> {
}

impl<Unit, T> Deserialize for Length<T, Unit> where T: Deserialize {
fn deserialize<D>(deserializer: D) -> Result<Length<T, Unit>,D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer {
Ok(Length(try!(Deserialize::deserialize(deserializer)), PhantomData))
}
@@ -65,7 +65,7 @@ impl<T, Unit> Serialize for Length<T, Unit> where T: Serialize {
}

impl<T, Unit> Length<T, Unit> {
pub fn new(x: T) -> Length<T, Unit> {
pub fn new(x: T) -> Self {
Length(x, PhantomData)
}
}
@@ -173,23 +173,23 @@ impl<Unit, T0: NumCast + Clone> Length<T0, Unit> {
}

impl<Unit, T: Clone + PartialEq> PartialEq for Length<T, Unit> {
fn eq(&self, other: &Length<T, Unit>) -> bool { self.get().eq(&other.get()) }
fn eq(&self, other: &Self) -> bool { self.get().eq(&other.get()) }
}

impl<Unit, T: Clone + PartialOrd> PartialOrd for Length<T, Unit> {
fn partial_cmp(&self, other: &Length<T, Unit>) -> Option<Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.get().partial_cmp(&other.get())
}
}

impl<Unit, T: Clone + Eq> Eq for Length<T, Unit> {}

impl<Unit, T: Clone + Ord> Ord for Length<T, Unit> {
fn cmp(&self, other: &Length<T, Unit>) -> Ordering { self.get().cmp(&other.get()) }
fn cmp(&self, other: &Self) -> Ordering { self.get().cmp(&other.get()) }
}

impl<Unit, T: Zero> Zero for Length<T, Unit> {
fn zero() -> Length<T, Unit> {
fn zero() -> Self {
Length::new(Zero::zero())
}
}
@@ -118,7 +118,7 @@ impl<T: Copy + Add<T, Output=T>, U> TypedPoint2D<T, U> {
}

impl<T: Copy + Add<T, Output=T>, U> Add<TypedSize2D<T, U>> for TypedPoint2D<T, U> {
type Output = TypedPoint2D<T, U>;
type Output = Self;
#[inline]
fn add(self, other: TypedSize2D<T, U>) -> Self {
point2(self.x + other.width, self.y + other.height)
@@ -140,7 +140,7 @@ impl<T: Copy + Sub<T, Output=T>, U> SubAssign<TypedVector2D<T, U>> for TypedPoin
}

impl<T: Copy + Add<T, Output=T>, U> Add<TypedVector2D<T, U>> for TypedPoint2D<T, U> {
type Output = TypedPoint2D<T, U>;
type Output = Self;
#[inline]
fn add(self, other: TypedVector2D<T, U>) -> Self {
point2(self.x + other.x, self.y + other.y)
@@ -150,7 +150,7 @@ impl<T: Copy + Add<T, Output=T>, U> Add<TypedVector2D<T, U>> for TypedPoint2D<T,
impl<T: Copy + Sub<T, Output=T>, U> Sub for TypedPoint2D<T, U> {
type Output = TypedVector2D<T, U>;
#[inline]
fn sub(self, other: TypedPoint2D<T, U>) -> TypedVector2D<T, U> {
fn sub(self, other: Self) -> TypedVector2D<T, U> {
vec2(self.x - other.x, self.y - other.y)
}
}
@@ -165,12 +165,12 @@ impl<T: Copy + Sub<T, Output=T>, U> Sub<TypedVector2D<T, U>> for TypedPoint2D<T,

impl<T: Float, U> TypedPoint2D<T, U> {
#[inline]
pub fn min(self, other: TypedPoint2D<T, U>) -> Self {
pub fn min(self, other: Self) -> Self {
point2(self.x.min(other.x), self.y.min(other.y))
}

#[inline]
pub fn max(self, other: TypedPoint2D<T, U>) -> Self {
pub fn max(self, other: Self) -> Self {
point2(self.x.max(other.x), self.y.max(other.y))
}
}
@@ -66,11 +66,11 @@ impl<T: Hash, U> Hash for TypedRect<T, U>
impl<T: Copy, U> Copy for TypedRect<T, U> {}

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

impl<T: PartialEq, U> PartialEq<TypedRect<T, U>> for TypedRect<T, U> {
fn eq(&self, other: &TypedRect<T, U>) -> bool {
impl<T: PartialEq, U> PartialEq<Self> for TypedRect<T, U> {
fn eq(&self, other: &Self) -> bool {
self.origin.eq(&other.origin) && self.size.eq(&other.size)
}
}
@@ -91,7 +91,7 @@ impl<T: fmt::Display, U> fmt::Display for TypedRect<T, U> {

impl<T, U> TypedRect<T, U> {
/// Constructor.
pub fn new(origin: TypedPoint2D<T, U>, size: TypedSize2D<T, U>) -> TypedRect<T, U> {
pub fn new(origin: TypedPoint2D<T, U>, size: TypedSize2D<T, U>) -> Self {
TypedRect {
origin: origin,
size: size,
@@ -102,7 +102,7 @@ impl<T, U> TypedRect<T, U> {
impl<T, U> TypedRect<T, U>
where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T, Output=T> {
#[inline]
pub fn intersects(&self, other: &TypedRect<T, U>) -> bool {
pub fn intersects(&self, other: &Self) -> bool {
self.origin.x < other.origin.x + other.size.width &&
other.origin.x < self.origin.x + self.size.width &&
self.origin.y < other.origin.y + other.size.height &&
@@ -150,7 +150,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
}

#[inline]
pub fn intersection(&self, other: &TypedRect<T, U>) -> Option<TypedRect<T, U>> {
pub fn intersection(&self, other: &Self) -> Option<Self> {
if !self.intersects(other) {
return None;
}
@@ -167,7 +167,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
/// Returns the same rectangle, translated by a vector.
#[inline]
#[must_use]
pub fn translate(&self, by: &TypedVector2D<T, U>) -> TypedRect<T, U> {
pub fn translate(&self, by: &TypedVector2D<T, U>) -> Self {
Self::new(self.origin + *by, self.size)
}

@@ -184,15 +184,15 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
/// returns true if rect is empty, and always returns false if rect is
/// nonempty but this rectangle is empty.
#[inline]
pub fn contains_rect(&self, rect: &TypedRect<T, U>) -> bool {
pub fn contains_rect(&self, rect: &Self) -> bool {
rect.is_empty() ||
(self.min_x() <= rect.min_x() && rect.max_x() <= self.max_x() &&
self.min_y() <= rect.min_y() && rect.max_y() <= self.max_y())
}

#[inline]
#[must_use]
pub fn inflate(&self, width: T, height: T) -> TypedRect<T, U> {
pub fn inflate(&self, width: T, height: T) -> Self {
TypedRect::new(
TypedPoint2D::new(self.origin.x - width, self.origin.y - height),
TypedSize2D::new(self.size.width + width + width, self.size.height + height + height),
@@ -201,7 +201,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T

#[inline]
#[must_use]
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> TypedRect<T, U> {
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> Self {
self.inflate(width.get(), height.get())
}

@@ -222,7 +222,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T

#[inline]
#[must_use]
pub fn translate_by_size(&self, size: &TypedSize2D<T, U>) -> TypedRect<T, U> {
pub fn translate_by_size(&self, size: &TypedSize2D<T, U>) -> Self {
self.translate(&size.to_vector())
}

@@ -269,7 +269,7 @@ where T: Copy + One + Add<Output=T> + Sub<Output=T> + Mul<Output=T> {
impl<T, U> TypedRect<T, U>
where T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero {
#[inline]
pub fn union(&self, other: &TypedRect<T, U>) -> TypedRect<T, U> {
pub fn union(&self, other: &Self) -> Self {
if self.size == Zero::zero() {
return *other;
}
@@ -292,7 +292,7 @@ where T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero

impl<T, U> TypedRect<T, U> {
#[inline]
pub fn scale<Scale: Copy>(&self, x: Scale, y: Scale) -> TypedRect<T, U>
pub fn scale<Scale: Copy>(&self, x: Scale, y: Scale) -> Self
where T: Copy + Clone + Mul<Scale, Output=T> {
TypedRect::new(
TypedPoint2D::new(self.origin.x * x, self.origin.y * y),
@@ -303,7 +303,7 @@ impl<T, U> TypedRect<T, U> {

impl<T: Copy + PartialEq + Zero, U> TypedRect<T, U> {
/// Constructor, setting all sides to zero.
pub fn zero() -> TypedRect<T, U> {
pub fn zero() -> Self {
TypedRect::new(
TypedPoint2D::origin(),
TypedSize2D::zero(),
@@ -326,17 +326,17 @@ pub fn max<T: Clone + PartialOrd>(x: T, y: T) -> T {
}

impl<T: Copy + Mul<T, Output=T>, U> Mul<T> for TypedRect<T, U> {
type Output = TypedRect<T, U>;
type Output = Self;
#[inline]
fn mul(self, scale: T) -> TypedRect<T, U> {
fn mul(self, scale: T) -> Self {
TypedRect::new(self.origin * scale, self.size * scale)
}
}

impl<T: Copy + Div<T, Output=T>, U> Div<T> for TypedRect<T, U> {
type Output = TypedRect<T, U>;
type Output = Self;
#[inline]
fn div(self, scale: T) -> TypedRect<T, U> {
fn div(self, scale: T) -> Self {
TypedRect::new(self.origin / scale, self.size / scale)
}
}
@@ -43,7 +43,7 @@ pub type SideOffsets2D<T> = TypedSideOffsets2D<T, UnknownUnit>;

impl<T: Copy, U> TypedSideOffsets2D<T, U> {
/// Constructor taking a scalar for each side.
pub fn new(top: T, right: T, bottom: T, left: T) -> TypedSideOffsets2D<T, U> {
pub fn new(top: T, right: T, bottom: T, left: T) -> Self {
TypedSideOffsets2D {
top: top,
right: right,
@@ -57,7 +57,7 @@ impl<T: Copy, U> TypedSideOffsets2D<T, U> {
pub fn from_lengths(top: Length<T, U>,
right: Length<T, U>,
bottom: Length<T, U>,
left: Length<T, U>) -> TypedSideOffsets2D<T, U> {
left: Length<T, U>) -> Self {
TypedSideOffsets2D::new(top.0, right.0, bottom.0, left.0)
}

@@ -74,12 +74,12 @@ impl<T: Copy, U> TypedSideOffsets2D<T, U> {
pub fn left_typed(&self) -> Length<T, U> { Length::new(self.left) }

/// Constructor setting the same value to all sides, taking a scalar value directly.
pub fn new_all_same(all: T) -> TypedSideOffsets2D<T, U> {
pub fn new_all_same(all: T) -> Self {
TypedSideOffsets2D::new(all, all, all, all)
}

/// Constructor setting the same value to all sides, taking a typed Length.
pub fn from_length_all_same(all: Length<T, U>) -> TypedSideOffsets2D<T, U> {
pub fn from_length_all_same(all: Length<T, U>) -> Self {
TypedSideOffsets2D::new_all_same(all.0)
}
}
@@ -103,8 +103,8 @@ impl<T, U> TypedSideOffsets2D<T, U> where T: Add<T, Output=T> + Copy {
}

impl<T, U> Add for TypedSideOffsets2D<T, U> where T : Copy + Add<T, Output=T> {
type Output = TypedSideOffsets2D<T, U>;
fn add(self, other: TypedSideOffsets2D<T, U>) -> TypedSideOffsets2D<T, U> {
type Output = Self;
fn add(self, other: Self) -> Self {
TypedSideOffsets2D::new(
self.top + other.top,
self.right + other.right,
@@ -116,7 +116,7 @@ impl<T, U> Add for TypedSideOffsets2D<T, U> where T : Copy + Add<T, Output=T> {

impl<T: Copy + Zero, U> TypedSideOffsets2D<T, U> {
/// Constructor, setting all sides to zero.
pub fn zero() -> TypedSideOffsets2D<T, U> {
pub fn zero() -> Self {
TypedSideOffsets2D::new(
Zero::zero(),
Zero::zero(),
@@ -45,7 +45,7 @@ impl<T: fmt::Display, U> fmt::Display for TypedSize2D<T, U> {

impl<T, U> TypedSize2D<T, U> {
/// Constructor taking scalar values.
pub fn new(width: T, height: T) -> TypedSize2D<T, U> {
pub fn new(width: T, height: T) -> Self {
TypedSize2D {
width: width,
height: height,
@@ -56,7 +56,7 @@ impl<T, U> TypedSize2D<T, U> {

impl<T: Clone, U> TypedSize2D<T, U> {
/// Constructor taking scalar strongly typed lengths.
pub fn from_lengths(width: Length<T, U>, height: Length<T, U>) -> TypedSize2D<T, U> {
pub fn from_lengths(width: Length<T, U>, height: Length<T, U>) -> Self {
TypedSize2D::new(width.get(), height.get())
}
}
@@ -89,15 +89,15 @@ impl<T: Floor, U> TypedSize2D<T, U> {
}

impl<T: Copy + Add<T, Output=T>, U> Add for TypedSize2D<T, U> {
type Output = TypedSize2D<T, U>;
fn add(self, other: TypedSize2D<T, U>) -> TypedSize2D<T, U> {
type Output = Self;
fn add(self, other: Self) -> Self {
TypedSize2D::new(self.width + other.width, self.height + other.height)
}
}

impl<T: Copy + Sub<T, Output=T>, U> Sub for TypedSize2D<T, U> {
type Output = TypedSize2D<T, U>;
fn sub(self, other: TypedSize2D<T, U>) -> TypedSize2D<T, U> {
type Output = Self;
fn sub(self, other: Self) -> Self {
TypedSize2D::new(self.width - other.width, self.height - other.height)
}
}
@@ -122,7 +122,7 @@ where T: Copy + One + Add<Output=T> + Sub<Output=T> + Mul<Output=T> {
}

impl<T: Zero, U> TypedSize2D<T, U> {
pub fn zero() -> TypedSize2D<T, U> {
pub fn zero() -> Self {
TypedSize2D::new(
Zero::zero(),
Zero::zero(),
@@ -131,7 +131,7 @@ impl<T: Zero, U> TypedSize2D<T, U> {
}

impl<T: Zero, U> Zero for TypedSize2D<T, U> {
fn zero() -> TypedSize2D<T, U> {
fn zero() -> Self {
TypedSize2D::new(
Zero::zero(),
Zero::zero(),
@@ -140,17 +140,17 @@ impl<T: Zero, U> Zero for TypedSize2D<T, U> {
}

impl<T: Copy + Mul<T, Output=T>, U> Mul<T> for TypedSize2D<T, U> {
type Output = TypedSize2D<T, U>;
type Output = Self;
#[inline]
fn mul(self, scale: T) -> TypedSize2D<T, U> {
fn mul(self, scale: T) -> Self {
TypedSize2D::new(self.width * scale, self.height * scale)
}
}

impl<T: Copy + Div<T, Output=T>, U> Div<T> for TypedSize2D<T, U> {
type Output = TypedSize2D<T, U>;
type Output = Self;
#[inline]
fn div(self, scale: T) -> TypedSize2D<T, U> {
fn div(self, scale: T) -> Self {
TypedSize2D::new(self.width / scale, self.height / scale)
}
}
@@ -192,7 +192,7 @@ impl<T: Copy, U> TypedSize2D<T, U> {
}

/// Tag a unitless value with units.
pub fn from_untyped(p: &Size2D<T>) -> TypedSize2D<T, U> {
pub fn from_untyped(p: &Size2D<T>) -> Self {
TypedSize2D::new(p.width, p.height)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.