Accepting any kind of writer as argument

Hi,

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.

Thanks in advance!

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");
}
2 Likes

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.

Thanks to both of you, guys! :slight_smile:

I am actually using Zig 0.15, it’s just that ChatGPT generated outdated code. It works now! :smiley:

In case you’re not aware, there are policies here on posting AI-generated code:

2 Likes

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.

5 Likes

Sorry, I used only AI, because I couldn’t find the answer in the documentation, nor in the book I am reading. And these are only 5 lines of code…

Asking a question without any line of code is in my opinion rude, as you have a harder time to guess what my problem actually is.

3 Likes

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. :wink:

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. :slight_smile:

3 Likes