-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_test.zig
97 lines (78 loc) · 2.43 KB
/
interface_test.zig
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const std = @import("std");
const warn = std.debug.warn;
const Allocator = std.mem.Allocator;
const Stream = std.io.OutStream(std.os.WriteError);
//showcases conventional method of implementing
//interfaces in zig as of 0.5.0
const Animal = struct {
const Self = @This();
const SpeakFn = fn(*const Self, *Stream) void;
speakFn: SpeakFn,
fn speak(self: *const Self, stream: *Stream) void {
return self.speakFn(self,stream);
}
};
const Dog = struct {
const Self = @This();
animal: Animal,
name: []const u8,
allocator: *Allocator,
fn init(allocator: *Allocator, name: []const u8) !Self {
const animal = Animal {
.speakFn = speak,
};
const buf = try allocator.alloc(u8,name.len);
std.mem.copy(u8,buf,name);
return Self {
.animal = animal,
.name = buf,
.allocator = allocator,
};
}
fn deinit(self: Self) void {
self.allocator.free(self.name);
}
fn speak(animal: *const Animal, stream: *Stream) void {
const self = @fieldParentPtr(Self,"animal",animal);
stream.print("{} says {}\n",self.name,"Woof!") catch {};
}
};
const Cat = struct {
const Self = @This();
animal: Animal,
name: []const u8,
allocator: *Allocator,
fn init(allocator: *Allocator, name: []const u8) !Self {
const animal = Animal {
.speakFn = speak,
};
const buf = try allocator.alloc(u8,name.len);
std.mem.copy(u8,buf,name);
return Self {
.animal = animal,
.name = buf,
.allocator = allocator,
};
}
fn deinit(self: Self ) void {
self.allocator.free(self.name);
}
fn speak(animal: *const Animal, stream: *Stream) void {
const self = @fieldParentPtr(Self,"animal",animal);
stream.print("{} says {}\n",self.name,"Meow!") catch {};
}
};
pub fn main() !void {
const allocator = std.heap.direct_allocator;
var stdout = &(try std.io.getStdOut()).outStream().stream;
//var stdout_state = (try std.io.getStdOut()).outStream();
//var stdout = &stdout_state.stream;
const dog_state = try Dog.init(allocator,"Jeff");
defer dog_state.deinit();
const dog = &dog_state.animal;
const cat_state = try Cat.init(allocator,"Will");
defer cat_state.deinit();
const cat = &cat_state.animal;
dog.speak(stdout);
cat.speak(stdout);
}