Zig 0.15.1 reader/writer: Don't make copies of @fieldParentPtr()-based interfaces

Forgive me if it’s not pertinent, but wouldn’t it be simpler to consider it a problem of the standard library, rather than of the language?

If you use @fieldParentPtr yourself, you know where the troubles lie, but if it’s in std, you have to read std, which also changes across versions.

By reading I'm too dumb for Zig's new IO interface, I noticed that in std.net there is actually a interface method

            pub fn interface(r: *Reader) *Io.Reader {
                return &r.interface_state;
            }

By introducing the practice of getting interfaces by methods and not directly you’d bypass the problem.

Moreover, if std exposes methods to access the interfaces, then the implementation could be changed without breaking existing code, because the methods could be modified to adapt to the changes.

So in std.fs.File, interface could become io_reader, and the method would be

    pub fn interface(r: *Reader) *std.Io.Reader {
        return &r.io_reader;
    }

std wouldn’t even need to change its code, since these methods would be made for the users. Honestly I think it’s a bit hard to require everybody to study std to avoid pitfalls.

It’s basically what I suggested here without the pseudo-private field.

Again, forgive me if not pertinent.

5 Likes