#How to get a std.io.Reader for a slice of bytes?

1 messages · Page 1 of 1 (latest)

obtuse fern
#

This may seem incredibly obvious to anyone more than a couple days into learning Zig, but I am struggling so hard right now to turn an embedded file (a const ptr to a byte slice?) into a reader.

cobalt ravine
#

std.io.FixedBufferStream

#

the usage is

var stream = std.io.fixedBufferStream(buf);
const reader = stream.reader();
obtuse fern
#

I am getting some sort of type error:
src\ase.zig:478:40: error: expected type 'type', found 'fn (comptime type, comptime type, comptime anytype) type'

#

I tried using std.io.GenericReader as well for my parameter's type.

#

Same error.

obtuse fern
#

wait... hold on.

#

This maybe some weird file system issue

#

Okay not quite, I just wasn't updating other lines where I was using Reader ...

#

Hmm, so why should this be anytype? I'd like to use a more concrete type for my reader parameter in a function.

#

Oh okay the compiler still ensures type safety. I'm too used to TypeScript's any type.

clever anchor
#

GenericReader is not really an interface, the generic reader from a File is different type than one from a FixedBufferStream from example, so a function that takes a GenericReader needs anytype

#

You can use std.io.AnyReader instead

#

By calling .any() on a generic reader