Skip to content

Commit

Permalink
Merge Coordinate into Point type
Browse files Browse the repository at this point in the history
This simplifies the Point API and removes an unnecessary abstraction level.

Resolves georust#15
  • Loading branch information
Turbo87 committed Oct 22, 2015
1 parent f045099 commit b8bba2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
11 changes: 2 additions & 9 deletions examples/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ extern crate geo;
use geo::*;

fn main() {
let c = Coordinate {
x: 40.02f64,
y: 116.34
};

let p = Point(c);

let Point(coord) = p;
println!("Point at ({}, {})", coord.x, coord.y);
let p = Point::new(40.02, 116.34);
println!("Point at ({}, {})", p.x(), p.y());
}
35 changes: 12 additions & 23 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ use std::ops::Neg;
use std::ops::Sub;

#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Coordinate {
pub x: f64,
pub y: f64,
pub struct Point {
x: f64,
y: f64,
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Point(pub Coordinate);

impl Point {
/// Creates a new point.
///
Expand All @@ -23,10 +20,10 @@ impl Point {
/// assert_eq!(p.y(), 2.345);
/// ```
pub fn new(x: f64, y: f64) -> Point {
Point(Coordinate {
Point {
x: x,
y: y,
})
}
}

/// Returns the x/horizontal component of the point.
Expand All @@ -39,7 +36,7 @@ impl Point {
/// assert_eq!(p.x(), 1.234);
/// ```
pub fn x(&self) -> f64 {
self.0.x
self.x
}

/// Sets the x/horizontal component of the point.
Expand All @@ -53,7 +50,7 @@ impl Point {
/// assert_eq!(p.x(), 9.876);
/// ```
pub fn set_x(&mut self, x: f64) -> &mut Point {
self.0.x = x;
self.x = x;
self
}

Expand All @@ -67,7 +64,7 @@ impl Point {
/// assert_eq!(p.y(), 2.345);
/// ```
pub fn y(&self) -> f64 {
self.0.y
self.y
}

/// Sets the y/vertical component of the point.
Expand All @@ -81,7 +78,7 @@ impl Point {
/// assert_eq!(p.y(), 9.876);
/// ```
pub fn set_y(&mut self, y: f64) -> &mut Point {
self.0.y = y;
self.y = y;
self
}

Expand Down Expand Up @@ -244,16 +241,8 @@ mod test {

#[test]
fn type_test() {
let c = Coordinate {
x: 40.02f64,
y: 116.34
};

let p = Point(c);

let Point(c2) = p;
assert_eq!(c, c2);
assert_eq!(c.x, c2.x);
assert_eq!(c.y, c2.y);
let p = Point::new(40.02, 116.34);
assert_eq!(p.x(), 40.02);
assert_eq!(p.y(), 116.34);
}
}

0 comments on commit b8bba2e

Please sign in to comment.