Created
July 13, 2019 13:38
-
-
Save via/1bd5854801b7cfe986c37d16d20fee68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub const Console = struct { | |
buf: [1024]u8, | |
outstream: std.io.SliceOutStream, | |
pub fn init() Console { | |
var ret = Console{ | |
.buf = undefined, | |
.outstream = undefined, | |
}; | |
ret.outstream = std.io.SliceOutStream.init(ret.buf[0..]); | |
return ret; | |
} | |
pub fn process(self: *Console) void { | |
self.outstream.reset(); | |
self.outstream.stream.print("rpm: {}\n", c_tfi.config.decoder.rpm) catch unreachable; | |
self.outstream.stream.print("fueling_us: {}\n", c_tfi.calculated_values.fueling_us) catch unreachable; | |
const output = self.outstream.getWritten(); | |
std.debug.warn("{}", output); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ret
is an instance ofConsole
put on stack with undefined valuesvar ret: Console = undefined
)outstream
which stores a copy of sliceret.buf[0..]
there is a new result-location mechanism as part of copy-elision in zig. and it allows returning structs to callsite that elide redundant copies. and it works like this. imagine if we could init the struct in one shot:
this would be ok if we were able to self-reference because zig is using callsite storage for Console in this return statement.