Copying to Fill Buf

If I've got a buffer and I want fill it to its end by copying from a given Read object, is there a utility method I can use? I know about copy, but it seems to allocate an intermediate buffer which would be unnecessary in my case. Or is there a copy version that's specialized for when the second argument (the Write) is a buffer? There's Read::read_to_end() as well but it takes Vec not a slice. There's Read::read_exact() which looks perfect, but it seems to fail when it can't fill the buf completely which is something I don't want. I want this function to succeed even if it failed to fill and it should return how many bytes were copied.

Take comes close as well, but it returns as soon as the underlying reader returns. I want it to continue to retry in a loop until the underlying reader returns 0 or an error occurs or the buffer is completely filled.

So far it looks like I'll have to roll something of my own :frowning:. Any ideas?

I ran into that same problem and rolled my own, here:

https://github.com/dekellum/body-image/blob/master/src/barc.rs#L946

Note this isn't a general version, since it expects a fixed size.

Thanks @dekellum. My implementation is similar as well.

I don't know how common this use-case is. If it's common, perhaps this functionality should be in stdlib.

1 Like