Make point immutable

This commit is contained in:
expectocode 2019-12-31 00:08:58 +00:00
parent 3f9d6b21c5
commit bc1ba4f88e
1 changed files with 6 additions and 8 deletions

View File

@ -23,14 +23,12 @@ impl Point {
Self { x: 0, y: 0 } Self { x: 0, y: 0 }
} }
pub fn with_x(&mut self, x: u32) -> &mut Self { pub fn with_x(&self, x: u32) -> Self {
self.x = x; Self { x, y: self.y }
self
} }
pub fn with_y(&mut self, y: u32) -> &mut Self { pub fn with_y(&self, y: u32) -> Self {
self.y = y; Self { x: self.x, y }
self
} }
pub fn x(&self) -> u32 { pub fn x(&self) -> u32 {
@ -93,12 +91,12 @@ mod tests {
#[test] #[test]
fn test_point_set_x() { fn test_point_set_x() {
assert_eq!(Point::new(42, 52).with_x(12), &Point::new(12, 52)); assert_eq!(Point::new(42, 52).with_x(12), Point::new(12, 52));
} }
#[test] #[test]
fn test_point_set_y() { fn test_point_set_y() {
assert_eq!(Point::new(42, 52).with_y(12), &Point::new(42, 12)); assert_eq!(Point::new(42, 52).with_y(12), Point::new(42, 12));
} }
#[test] #[test]