Last active
June 3, 2022 22:32
-
-
Save andrewrk/d50e1d6899ba9e23cb4e3d59b71083b0 to your computer and use it in GitHub Desktop.
https://hirrolot.github.io/posts/rust-is-hard-or-the-misery-of-mainstream-programming.html zig code example
This file contains hidden or 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
package main | |
import "fmt" | |
type Update struct{} | |
type Handler func(*Update) | |
type Dispatcher struct { | |
handlers []Handler | |
} | |
func (dp *Dispatcher) pushHandler(handler Handler) { | |
dp.handlers = append(dp.handlers, handler) | |
} | |
func main() { | |
dp := Dispatcher{handlers: nil} | |
dp.pushHandler(func(upd *Update) { | |
fmt.Println(upd) | |
}) | |
} |
This file contains hidden or 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
const std = @import("std"); | |
const Update = struct {}; | |
const Handler = fn (*Update) callconv(.Async) void; | |
const Dispatcher = struct { | |
handlers: std.ArrayList(Handler), | |
fn pushHandler(dp: *Dispatcher, handler: Handler) !void { | |
try dp.handlers.append(handler); | |
} | |
}; | |
pub const io_mode = .evented; | |
pub fn main() !void { | |
var gpa_instance: std.heap.GeneralPurposeAllocator(.{}) = .{}; | |
const gpa = gpa_instance.allocator(); | |
var dp = Dispatcher{ | |
.handlers = std.ArrayList(Handler).init(gpa), | |
}; | |
try dp.pushHandler(struct { | |
fn f(upd: *Update) void { | |
std.debug.print("{}", .{upd}); | |
} | |
}.f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment