Make point immutable

master
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 }
}
pub fn with_x(&mut self, x: u32) -> &mut Self {
self.x = x;
self
pub fn with_x(&self, x: u32) -> Self {
Self { x, y: self.y }
}
pub fn with_y(&mut self, y: u32) -> &mut Self {
self.y = y;
self
pub fn with_y(&self, y: u32) -> Self {
Self { x: self.x, y }
}
pub fn x(&self) -> u32 {
@ -93,12 +91,12 @@ mod tests {
#[test]
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]
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]