|
| 1 | +const builtin = @import("builtin"); |
| 2 | +const stream = @import("watcher/stream.zig"); |
| 3 | +const Backend = @import("backend.zig").Backend; |
| 4 | + |
| 5 | +/// Creates the Xev API based on a backend type. |
| 6 | +/// |
| 7 | +/// For the default backend type for your system (i.e. io_uring on Linux), |
| 8 | +/// this is the main API you interact with. It is forwarded into the main |
| 9 | +/// the "xev" package so you'd use types such as `xev.Loop`, `xev.Completion`, |
| 10 | +/// etc. |
| 11 | +/// |
| 12 | +/// Unless you're using a custom or specific backend type, you do NOT ever |
| 13 | +/// need to call the Xev function itself. |
| 14 | +pub fn Xev(comptime be: Backend, comptime T: type) type { |
| 15 | + return struct { |
| 16 | + const Self = @This(); |
| 17 | + const loop = @import("loop.zig"); |
| 18 | + |
| 19 | + /// This is used to detect a static vs dynamic API at comptime. |
| 20 | + pub const dynamic = false; |
| 21 | + |
| 22 | + /// The backend that this is. This is supplied at comptime so |
| 23 | + /// it is up to the caller to say the right thing. This lets custom |
| 24 | + /// implementations also "quack" like an implementation. |
| 25 | + pub const backend = be; |
| 26 | + |
| 27 | + /// A function to test if this API is available on the |
| 28 | + /// current system. |
| 29 | + pub const available = T.available; |
| 30 | + |
| 31 | + /// The core loop APIs. |
| 32 | + pub const Loop = T.Loop; |
| 33 | + pub const Completion = T.Completion; |
| 34 | + pub const Result = T.Result; |
| 35 | + pub const ReadBuffer = T.ReadBuffer; |
| 36 | + pub const WriteBuffer = T.WriteBuffer; |
| 37 | + pub const Options = loop.Options; |
| 38 | + pub const RunMode = loop.RunMode; |
| 39 | + pub const CallbackAction = loop.CallbackAction; |
| 40 | + pub const CompletionState = loop.CompletionState; |
| 41 | + |
| 42 | + /// Error types |
| 43 | + pub const AcceptError = T.AcceptError; |
| 44 | + pub const CancelError = T.CancelError; |
| 45 | + pub const CloseError = T.CloseError; |
| 46 | + pub const ConnectError = T.ConnectError; |
| 47 | + pub const ShutdownError = T.ShutdownError; |
| 48 | + pub const WriteError = T.WriteError; |
| 49 | + pub const ReadError = T.ReadError; |
| 50 | + |
| 51 | + /// Shared stream types |
| 52 | + const SharedStream = stream.Shared(Self); |
| 53 | + pub const PollError = SharedStream.PollError; |
| 54 | + pub const PollEvent = SharedStream.PollEvent; |
| 55 | + pub const WriteQueue = SharedStream.WriteQueue; |
| 56 | + pub const WriteRequest = SharedStream.WriteRequest; |
| 57 | + |
| 58 | + /// The high-level helper interfaces that make it easier to perform |
| 59 | + /// common tasks. These may not work with all possible Loop implementations. |
| 60 | + pub const Async = @import("watcher/async.zig").Async(Self); |
| 61 | + pub const File = @import("watcher/file.zig").File(Self); |
| 62 | + pub const Process = @import("watcher/process.zig").Process(Self); |
| 63 | + pub const Stream = stream.GenericStream(Self); |
| 64 | + pub const Timer = @import("watcher/timer.zig").Timer(Self); |
| 65 | + pub const TCP = @import("watcher/tcp.zig").TCP(Self); |
| 66 | + pub const UDP = @import("watcher/udp.zig").UDP(Self); |
| 67 | + |
| 68 | + /// The callback of the main Loop operations. Higher level interfaces may |
| 69 | + /// use a different callback mechanism. |
| 70 | + pub const Callback = loop.Callback(T); |
| 71 | + |
| 72 | + /// A callback that does nothing and immediately disarms. This |
| 73 | + /// implements xev.Callback and is the default value for completions. |
| 74 | + pub const noopCallback = loop.NoopCallback(T); |
| 75 | + |
| 76 | + /// A way to access the raw type. |
| 77 | + pub const Sys = T; |
| 78 | + |
| 79 | + test { |
| 80 | + @import("std").testing.refAllDecls(@This()); |
| 81 | + } |
| 82 | + |
| 83 | + test "completion is zero-able" { |
| 84 | + const c: Self.Completion = .{}; |
| 85 | + _ = c; |
| 86 | + } |
| 87 | + }; |
| 88 | +} |
0 commit comments