File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -6,4 +6,4 @@ Any+ is a Zig library for handling anytypes in the runtime.
66
77- [x] Runtime ` anytype `
88- [ ] Any-reader
9- - [ ] Any-writer
9+ - [x ] Any-writer
Original file line number Diff line number Diff line change 11/// Anytype in runtime
22pub const Anytype = @import ("any+/anytype.zig" );
33
4+ /// An opaque writer type
5+ pub const Anywriter = @import ("any+/anywriter.zig" );
6+
47test {
58 _ = Anytype ;
9+ _ = Anywriter ;
610}
Original file line number Diff line number Diff line change 1+ const std = @import ("std" );
2+ const Self = @This ();
3+
4+ pub const Error = anyerror ;
5+ const PointerWrite = * const fn (* const Self , []const u8 ) Error ! void ;
6+
7+ pub const Writer = std .io .Writer (Self , Error , write );
8+
9+ type : []const u8 ,
10+ ptr : * anyopaque ,
11+ ptrWrite : PointerWrite ,
12+
13+ pub inline fn init (writer : anytype ) Self {
14+ return initExplicit (@TypeOf (writer ), writer );
15+ }
16+
17+ pub inline fn initExplicit (comptime T : type , writer : T ) Self {
18+ return .{
19+ .type = @typeName (T ),
20+ .ptr = @constCast (& writer ),
21+ .ptrWrite = (struct {
22+ fn func (self : * const Self , buf : []const u8 ) Error ! void {
23+ if (! std .mem .eql (u8 , self .type , @typeName (T ))) return error .InvalidCast ;
24+ const w : * T = @ptrCast (@alignCast (self .ptr ));
25+ return w .write (buf );
26+ }
27+ }).func ,
28+ };
29+ }
30+
31+ pub fn write (self : Self , buf : []const u8 ) Error ! void {
32+ return self .ptrWrite (& self , buf );
33+ }
You can’t perform that action at this time.
0 commit comments