# Accepting any kind of writer as argument

**URL:** <https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274>\
**Category:** Help\
**Tags:** language, standard-library\
**Created:** [November 29, 2025, 6:56pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274 "2025-11-29T18:56:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanoD](https://ziggit.dev/user_avatar/ziggit.dev/stefanod/32/2781_2.png) [@StefanoD](https://ziggit.dev/u/StefanoD)\
**Post date:** [November 29, 2025, 6:56pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/1 "2025-11-29T18:56:01Z")

</div>

Hi,

I would like to accept any kind of `Writer` as argument. Sadly, this fails:

```zig
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!

---

<div class="post-metadata">

**Author:** ![TerenceTux](https://ziggit.dev/letter_avatar_proxy/v4/letter/t/a4c791/32.png) [@TerenceTux](https://ziggit.dev/u/TerenceTux)\
**Post date:** [November 29, 2025, 7:34pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/2 "2025-11-29T19:34:42Z")

</div>

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.AnyWriter`as argument for print. You can then pass `writer.any()`. An second option is to accept `anytype` as argument, then you can just pass `writer`like you do now.

If you want to use the new writer interface used in 0.15 and newer, you can do this:

```zig
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");
}

```

---

<div class="post-metadata">

**Author:** ![tholmes](https://ziggit.dev/user_avatar/ziggit.dev/tholmes/32/5641_2.png) [@tholmes](https://ziggit.dev/u/tholmes)\
**Post date:** [November 29, 2025, 7:34pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/3 "2025-11-29T19:34:54Z")

</div>

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:

```zig
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.

---

<div class="post-metadata">

**Author:** ![StefanoD](https://ziggit.dev/user_avatar/ziggit.dev/stefanod/32/2781_2.png) [@StefanoD](https://ziggit.dev/u/StefanoD)\
**Post date:** [November 29, 2025, 8:00pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/4 "2025-11-29T20:00:58Z")

</div>

Thanks to both of you, guys! 🙂

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

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://ziggit.dev/user_avatar/ziggit.dev/jumpnbrownweasel/32/263_2.png) [@jumpnbrownweasel](https://ziggit.dev/u/jumpnbrownweasel)\
**Post date:** [November 29, 2025, 8:21pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/5 "2025-11-29T20:21:47Z")

</div>

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

> [@Guidelines](https://ziggit.dev/t/guidelines/5#p-6-ai-policyai-9):
>
> [This is a Civilized Place for Public Discussion](#civilized) Please treat this discussion forum with the same respect you would a public park. We, too, are a shared community resource — a place to share skills, knowledge and interests through ongoing conversation. These are not hard and fast rules. They are guidelines to aid the human judgment of our community and keep this a kind, friendly place for civilized public discourse. [Improve the Discussion](#improve) Help us make this a great place for discussion by al…

---

<div class="post-metadata">

**Author:** ![tholmes](https://ziggit.dev/user_avatar/ziggit.dev/tholmes/32/5641_2.png) [@tholmes](https://ziggit.dev/u/tholmes)\
**Post date:** [November 29, 2025, 8:35pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/6 "2025-11-29T20:35:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StefanoD](https://ziggit.dev/user_avatar/ziggit.dev/stefanod/32/2781_2.png) [@StefanoD](https://ziggit.dev/u/StefanoD)\
**Post date:** [November 29, 2025, 8:40pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/7 "2025-11-29T20:40:48Z")

</div>

Sorry, I used only AI, because I couldn’t find the answer in the documentation, nor in the [book](https://pedropark99.github.io/zig-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.

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://ziggit.dev/user_avatar/ziggit.dev/jumpnbrownweasel/32/263_2.png) [@jumpnbrownweasel](https://ziggit.dev/u/jumpnbrownweasel)\
**Post date:** [November 29, 2025, 8:54pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/8 "2025-11-29T20:54:28Z")

</div>

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. 😉

---

<div class="post-metadata">

**Author:** ![jumpnbrownweasel](https://ziggit.dev/user_avatar/ziggit.dev/jumpnbrownweasel/32/263_2.png) [@jumpnbrownweasel](https://ziggit.dev/u/jumpnbrownweasel)\
**Post date:** [November 29, 2025, 9:03pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/9 "2025-11-29T21:03:22Z")

</div>

> [@StefanoD](#):
>
> Sorry, I used only AI, because I couldn’t find the answer in the documentation, nor in the [book](https://pedropark99.github.io/zig-book/) I am reading. And these are only 5 lines of code…

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.

---

<div class="post-metadata">

**Author:** ![sbrow](https://ziggit.dev/user_avatar/ziggit.dev/sbrow/32/4970_2.png) [@sbrow](https://ziggit.dev/u/sbrow)\
**Post date:** [November 29, 2025, 9:29pm UTC](https://ziggit.dev/t/accepting-any-kind-of-writer-as-argument/13274/10 "2025-11-29T21:29:40Z")

</div>

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. 🙂
