Skip to content

Update to work with Zig 0.15#8

Open
yoshibox wants to merge 4 commits into
Remy2701:mainfrom
yoshibox:zig-0.15
Open

Update to work with Zig 0.15#8
yoshibox wants to merge 4 commits into
Remy2701:mainfrom
yoshibox:zig-0.15

Conversation

@yoshibox
Copy link
Copy Markdown

Updated to Zig's ArrayListUnmanaged as it's the new default
Make the formatting explicit: "{}" -> "{f}"
Changed the svg.writeTo function to accept a writer's interface and flush at the end

@Sivecano
Copy link
Copy Markdown

this seem useful

@Sivecano
Copy link
Copy Markdown

the documentation should probably be updated.

a bunch of writer-consuming functions like svg.writeTo should also use the new Reader/Writer interface instead of being generic.
this can cause problems since it will automatically cast pointers to const pointers.

this also allows for error sets better than anyerror.

@yoshibox
Copy link
Copy Markdown
Author

I remember updating the examples in the readme, did I miss something ?

I forgot to change the writeTo's function signatures, since it was working I did not look further, does this look right to you:
old: pub fn writeTo(self: *const Kind, writer: anytype) anyerror!void {
new: pub fn writeTo(self: *const Kind, writer: *std.Io.Writer) std.Io.Writer.Error!void

@Sivecano
Copy link
Copy Markdown

yeah, I think that looks about right. there's no non-writer errors in this, right?
(I guess the compiler would tell you)

@Sivecano
Copy link
Copy Markdown

This is what I got after running some sed scripts. seems to work. but I suppose I should test it properly

diff --git a/src/svg/Circle.zig b/src/svg/Circle.zig
index 53e6dcf..5bd26d2 100644
--- a/src/svg/Circle.zig
+++ b/src/svg/Circle.zig
@@ -44,7 +44,7 @@ pub fn init(options: Options) Circle {
 }
 
 /// Write the circle to the given writer
-pub fn writeTo(self: *const Circle, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Circle, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<circle ");
     try writer.print("cx=\"{f}\" ", .{self.options.center_x});
     try writer.print("cy=\"{f}\" ", .{self.options.center_y});
diff --git a/src/svg/Line.zig b/src/svg/Line.zig
index 99de75b..a62bc19 100644
--- a/src/svg/Line.zig
+++ b/src/svg/Line.zig
@@ -18,7 +18,7 @@ pub const LineCap = enum {
     round,
     square,
 
-    pub fn format(self: LineCap, writer: anytype) !void {
+    pub fn format(self: LineCap, writer: *std.Io.Writer) !void {
         switch (self) {
             .butt => try writer.writeAll("butt"),
             .round => try writer.writeAll("round"),
@@ -62,7 +62,7 @@ pub fn init(options: Options) Line {
 }
 
 /// Write the line to the given writer
-pub fn writeTo(self: *const Line, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Line, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<line ");
     try writer.print("x1=\"{f}\" ", .{self.options.x1});
     try writer.print("y1=\"{f}\" ", .{self.options.y1});
diff --git a/src/svg/Path.zig b/src/svg/Path.zig
index 4287902..c2c43a7 100644
--- a/src/svg/Path.zig
+++ b/src/svg/Path.zig
@@ -129,7 +129,7 @@ pub const Command = union(enum) {
     ClosePath: void,
 
     /// Write the command to the given writer
-    pub fn writeTo(self: *const Command, writer: anytype) anyerror!void {
+    pub fn writeTo(self: *const Command, writer: *std.Io.Writer) std.Io.Writer.Error!void {
         switch (self.*) {
             .MoveTo => {
                 try writer.print("M {d} {d}", .{ self.MoveTo.x, self.MoveTo.y });
@@ -283,7 +283,7 @@ pub fn deinit(self: *const Path) void {
 }
 
 /// Write the path to the given writer
-pub fn writeTo(self: *const Path, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Path, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<path");
     if (self.options.commands) |commands| {
         try writer.writeAll(" d=\"");
diff --git a/src/svg/Polyline.zig b/src/svg/Polyline.zig
index 0e092fd..a2b7892 100644
--- a/src/svg/Polyline.zig
+++ b/src/svg/Polyline.zig
@@ -51,7 +51,7 @@ pub fn deinit(self: *const Polyline) void {
 }
 
 /// Write the Polyline to the given writer.
-pub fn writeTo(self: *const Polyline, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Polyline, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<polyline ");
     if (self.options.points) |points| {
         try writer.writeAll("points=\"");
diff --git a/src/svg/Rect.zig b/src/svg/Rect.zig
index 43d1134..89293c0 100644
--- a/src/svg/Rect.zig
+++ b/src/svg/Rect.zig
@@ -50,7 +50,7 @@ pub fn init(options: Options) Rect {
 }
 
 /// Write the rectangle to the given writer
-pub fn writeTo(self: *const Rect, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Rect, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<rect ");
     try writer.print("x=\"{f}\" ", .{self.options.x});
     try writer.print("y=\"{f}\" ", .{self.options.y});
diff --git a/src/svg/SVG.zig b/src/svg/SVG.zig
index b3f2075..04b5622 100644
--- a/src/svg/SVG.zig
+++ b/src/svg/SVG.zig
@@ -89,7 +89,7 @@ const SVG_HEADER =
 ;
 
 /// Write the SVG to the given writer
-pub fn writeTo(self: *const SVG, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const SVG, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     // Write the header
     try writer.print(SVG_HEADER, .{
         self.width,
diff --git a/src/svg/Text.zig b/src/svg/Text.zig
index 3d3ffb4..a88daac 100644
--- a/src/svg/Text.zig
+++ b/src/svg/Text.zig
@@ -46,7 +46,7 @@ pub const FontSize = union(enum) {
     /// Math value
     math: void,
 
-    pub fn format(self: FontSize, writer: anytype) !void {
+    pub fn format(self: FontSize, writer: *std.Io.Writer) !void {
         switch (self) {
             .pixel => |value| try writer.print("{d}px", .{value}),
             .em => |value| try writer.print("{d}em", .{value}),
@@ -95,7 +95,7 @@ pub const FontWeight = enum {
     /// The bolder font weight
     bolder,
 
-    pub fn format(self: FontWeight, writer: anytype) !void {
+    pub fn format(self: FontWeight, writer: *std.Io.Writer) !void {
         switch (self) {
             .normal => try writer.writeAll("normal"),
             .bold => try writer.writeAll("bold"),
@@ -123,7 +123,7 @@ pub const TextAnchor = enum {
     /// The end anchor
     end,
 
-    pub fn format(self: TextAnchor, writer: anytype) !void {
+    pub fn format(self: TextAnchor, writer: *std.Io.Writer) !void {
         switch (self) {
             .start => try writer.writeAll("start"),
             .middle => try writer.writeAll("middle"),
@@ -159,7 +159,7 @@ pub const DominantBaseline = enum {
     /// The text before edge baseline
     text_before_edge,
 
-    pub fn format(self: DominantBaseline, writer: anytype) !void {
+    pub fn format(self: DominantBaseline, writer: *std.Io.Writer) !void {
         switch (self) {
             .auto => try writer.writeAll("auto"),
             .use_script => try writer.writeAll("use-script"),
@@ -235,7 +235,7 @@ pub fn deinit(self: *const Text) void {
 }
 
 /// Write the text to the given writer
-pub fn writeTo(self: *const Text, writer: anytype) anyerror!void {
+pub fn writeTo(self: *const Text, writer: *std.Io.Writer) std.Io.Writer.Error!void {
     try writer.writeAll("<text ");
     try writer.print("x=\"{f}\" ", .{self.options.x});
     try writer.print("y=\"{f}\" ", .{self.options.y});
diff --git a/src/svg/kind.zig b/src/svg/kind.zig
index b1e6931..40833ee 100644
--- a/src/svg/kind.zig
+++ b/src/svg/kind.zig
@@ -21,7 +21,7 @@ pub const Kind = union(enum) {
     path: SVG.Path,
 
     /// Write the Kind to the given writer
-    pub fn writeTo(self: *const Kind, writer: anytype) anyerror!void {
+    pub fn writeTo(self: *const Kind, writer: *std.Io.Writer) std.Io.Writer.Error!void {
         try switch (self.*) {
             .line => |line| line.writeTo(writer),
             .rect => |rect| rect.writeTo(writer),
diff --git a/src/svg/util/length.zig b/src/svg/util/length.zig
index 16305fa..0e9e080 100644
--- a/src/svg/util/length.zig
+++ b/src/svg/util/length.zig
@@ -9,7 +9,7 @@ pub const LengthPercentAuto = union(enum) {
     /// Automatic length
     auto: void,
 
-    pub fn format(self: LengthPercentAuto, writer: anytype) !void {
+    pub fn format(self: LengthPercentAuto, writer: *std.Io.Writer) !void {
         switch (self) {
             .pixel => |value| try writer.print("{d}", .{value}),
             .percent => |value| try writer.print("{d}%", .{value}),
@@ -25,7 +25,7 @@ pub const LengthPercent = union(enum) {
     /// The length in percent (of the parent).
     percent: f32,
 
-    pub fn format(self: LengthPercent, writer: anytype) !void {
+    pub fn format(self: LengthPercent, writer: *std.Io.Writer) !void {
         switch (self) {
             .pixel => |value| try writer.print("{d}", .{value}),
             .percent => |value| try writer.print("{d}%", .{value}),

@yoshibox
Copy link
Copy Markdown
Author

Yes the std.Io.Writer.Error is the error set return by .write and .flush.
I pushed the modifications

@Sivecano
Copy link
Copy Markdown

I remember updating the examples in the readme, did I miss something ?

Only the this is developed for zig 0.13 disclaimer at the top. should probably say 0.15.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants