Skip to content

Instantly share code, notes, and snippets.

@itsmontoya
Created October 1, 2019 21:22
Show Gist options
  • Save itsmontoya/3718dceec73b6078ca1bb1aaec63b7d8 to your computer and use it in GitHub Desktop.
Save itsmontoya/3718dceec73b6078ca1bb1aaec63b7d8 to your computer and use it in GitHub Desktop.
const warn = std.debug.warn;
const std = @import("std");
const assert = std.debug.assert;
const time = std.time;
var x: i32 = 1;
pub fn main() !void {
const stdout_file = try std.io.getStdOut();
try stdout_file.write("Hello, world!\n");
var frame = addSlow();
comptime assert(@typeOf(frame) == @Frame(addSlow));
const ptr: anyframe->void = &frame;
const any_ptr: anyframe = ptr;
resume any_ptr;
await ptr;
assert(x == 2);
// If this program encounters pipe failure when printing to stdout, exit
// with an error.
warn("TEST! {}\n", x);
return;
}
fn addSlow() void {
suspend {
var ms = toMilliseconds(3);
var ns = toNanoseconds(ms);
time.sleep(ns);
x += 1;
resume @frame();
}
}
fn toMilliseconds(seconds: u64) u64 {
return seconds * 1000;
}
fn toNanoseconds(ms: u64) u64 {
return ms * 1000000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment