summaryrefslogtreecommitdiff
path: root/exercises/085_async.zig
blob: d4f4a3fd7594d39e97ed0bcb484984f58ef3919c (plain)
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
//
// In previous versions of Zig, async/await used special keywords
// like 'suspend', 'resume', and 'async' that operated on stackframes
// directly. Those keywords no longer exist!
//
// Zig 0.16 replaced them with a unified I/O interface: std.Io.
// This interface uses a VTable pattern - a struct of function pointers -
// to abstract over different concurrency backends:
//
//   * Threaded  - thread-pool based I/O
//   * Evented   - chooses the best event-loop backend for your OS:
//       * Uring    on Linux (io_uring)
//       * Kqueue   on BSD/macOS
//       * Dispatch on macOS (Grand Central Dispatch)
//
// The Io struct itself is tiny:
//
//     const Io = struct {
//         userdata: ?*anyopaque,   // opaque state of the backend
//         vtable: *const VTable,   // table of function pointers
//     };
//
// Your code receives an Io value and calls methods on it.
// The backend is chosen at initialization time - your code doesn't
// need to know which one it is!
//
// In Zig 0.16, main() receives a std.process.Init struct to opt
// into I/O and concurrency support:
//
//     pub fn main(init: std.process.Init) !void {
//         const io = init.io;
//         // ... use io ...
//     }
//
// Let's start simple. Fix the main function to extract the Io
// interface from init, then use it to get the current time.
//
const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    // Get the current wall-clock time using the Io interface.
    // Hint: Timestamp.now() takes an Io and a Clock type (.real = wall clock).
    const timestamp = std.Io.Timestamp.now(io, .real);

    // Print the timestamp in seconds since the Unix epoch.
    std.debug.print("Current time: {}s since epoch\n", .{timestamp.toSeconds()});
}