I would like to accept any kind of `Writer` as argument. Sadly, this fails:
pub fn print(writer: *std.io.Writer) !void {
// write
}
test "Foo" {
var buffer: [1024]u8 = undefined;
// Create a fixed buffer stream
var fbs = std.io.fixedBufferStream(&buffer);
// Get the writer
var writer = fbs.writer();
print(&writer);
}
error: expected type ‘*Io.Writer’, found ‘*Io.GenericWriter(*Io.fixed_buffer_stream.FixedBufferStream(u8),error{NoSpaceLeft},(function ‘write’))’
move.print(&writer);
^~~~~~~
src/move.zig:199:16: note: pointer type child ‘Io.GenericWriter(*Io.fixed_buffer_stream.FixedBufferStream(u8),error{NoSpaceLeft},(function ‘write’))’ cannot cast into pointer type child ‘Io.Writer’
How can I fix this? ChatGPT gives me some (old) Zig code which does not work.
The writer interface has changed a lot in the 0.15 update, but it seems you are still using 0.14.
In 0.14 and older, you can accept an std.io.AnyWriteras argument for print. You can then pass writer.any(). An second option is to accept anytype as argument, then you can just pass writerlike you do now.
If you want to use the new writer interface used in 0.15 and newer, you can do this:
const std = @import("std");
pub fn print(writer: *std.Io.Writer) !void {
// Write
try writer.writeAll("Foo");
}
test "Foo" {
var buffer: [1024]u8 = undefined;
// Create a fixed buffer writer
var writer = std.Io.Writer.fixed(&buffer);
try print(&writer);
try std.testing.expectEqualSlices(u8, buffer[0..3], "Foo");
}
You need to do something with the GenericWriter and its adaptToNewApi() method.
Personally I’d recommend doing what the user above is doing, and using std.Io.Writer.fixed().
My preferred pattern to do it looks like this:
var writer_interface: std.Io.Writer = .fixed(buffer);
const writer: = &writer_interface;
This gives me a pointer to a std.Io.Writer, which ensures I never copy the interface.
He didn’t post code outputted by a neural network, he only said that he input a question to a neural network which gave him a bad answer.
I understand the sentiment, but policing the clearly innocent (even pre-emptively!) is an insecure move.
I could be wrong, but I thought they clearly implied that the code posted in the original question was AI generated and doesn’t compile, which is specifically not allowed in the guidelines. I apologize if that’s not the case or if I misunderstand the guidelines.
What I did do wrong is that I should have just flagged it, as it says in the guidelines. I apologize and I’ll do that in the future instead (if at all).
(But please don’t call me insecure, I feel insecure enough already.
That’s understandable. I may misunderstand the guidelines and it is really up to the moderators to decide that and tell you about it, not me, so I apologize for that.
I don’t recall the exact details of the AI policy (though I have definitely read it), but as someone that uses a lot of AI at work, and having seen how strongly anti-AI a lot of the Zig community is, I try to mark anything AI I have as soon as possible in the post, so those that don’t want to waste their time with it can move on efficiently.