I'm too dumb for Zig's new IO interface

83 points by janerik


andrewrk

Never do this:

  defer tls_client.end() catch {};

The language is trying to help you diligently check all the errors, but this code explicitly catches the error and then disregards it. This is obviously a bug.

xash

To make it work: don’t forget to flush. :-) After writing to the tls_client’s writer:

try tls_client.writer.flush();
try writer.interface.flush();

Also reader.stream just pushes whatever is in the buffer. As the server probably hasn’t answered by then, something like streamDelimiterLimit is more what is wanted – wait until the delimiter shows up. And finally the final writer is – depending on the use case – probably not necessary, just takeDelimiter to see the data in the reader’s buffer directly.

I think mostly this is just a case of missing docs which will surely be written eventually.

Buffering is a first class citizen of the new Io interface - who needs composition?

I don’t understand this – buffering is orthogonal to composition, no? In this case it even helps: TLS shouldn’t need to handle more bytes than max_ciphertext_inner_record_len (I think this is the correct variable for the writer, but I’m not sure about the TLS protocol) in one message, so have a buffer this size. Once you want to write more, the buffer gets automatically packed into one TLS message and cleared for the next message. Neither the implementation of the TLS writer has to handle this (it only sees one single write with the buffer’s content) nor the user needs to handle this (they write as much as they want – and hopefully don’t forget to flush.)