-
Notifications
You must be signed in to change notification settings - Fork 2
/
liskov_rectangle.rs
73 lines (59 loc) · 1.23 KB
/
liskov_rectangle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extern crate stainless;
use stainless::*;
/// The typical violation of the **Liskov substitution principle** can be
/// checked and guarded against with type classes and laws.
trait Rectangle: Sized {
fn width(&self) -> u32;
fn height(&self) -> u32;
fn set_width(&self, width: u32) -> Self;
fn set_height(&self, height: u32) -> Self;
#[law]
fn preserve_height(&self, any: u32) -> bool {
self.set_width(any).height() == self.height()
}
#[law]
fn preserve_width(&self, any: u32) -> bool {
self.set_height(any).width() == self.width()
}
}
struct Rect {
width: u32,
height: u32,
}
impl Rectangle for Rect {
fn width(&self) -> u32 {
self.width
}
fn height(&self) -> u32 {
self.height
}
fn set_width(&self, width: u32) -> Self {
Rect {
width,
height: self.height,
}
}
fn set_height(&self, height: u32) -> Self {
Rect {
width: self.width,
height,
}
}
}
struct Square {
width: u32,
}
impl Rectangle for Square {
fn width(&self) -> u32 {
self.width
}
fn height(&self) -> u32 {
self.width
}
fn set_width(&self, width: u32) -> Self {
Square { width }
}
fn set_height(&self, height: u32) -> Self {
Square { width: height }
}
}