forked from zenc-lang/zenc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_resources.zc
More file actions
59 lines (43 loc) · 1.04 KB
/
Copy pathtest_resources.zc
File metadata and controls
59 lines (43 loc) · 1.04 KB
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
// Copy Trait
@derive(Copy)
struct Point { x: int; y: int; }
// Clone Pattern
trait Clone {
fn clone(self) -> Self;
}
struct Box { val: int; }
impl Clone for Box {
fn clone(self) -> Box {
return Box{val: self.val};
}
}
// Re-initialization
struct Resource { ptr: void*; }
// Function Param Helper
fn take_point(p: Point) {
if p.x != 10 {
// Error
}
}
test "Resource Semantics" {
"Testing Resource Semantics...";
let p1 = Point{x: 10, y: 20};
let p2 = p1; // Copied
let b1 = Box{val: 99};
let b2 = b1.clone();
// let b3 = b1; // This would move if uncommented.
if b2.val != 99 {
!"Clone failed";
exit(1);
}
// Re-initialization
// struct Resource (Global)
let r1 = Resource{ptr: NULL};
let r2 = r1; // Moved
r1 = Resource{ptr: NULL}; // Re-init
let r3 = r1; // Valid again
// Function Param Move (Simulated)
take_point(p1);
take_point(p1); // Still valid because Copy
"Resource Semantics Passed.";
}