There are at least three ways to return bytes from a function.
- As an iterator producing
u8s. - Taking a
Writeras an argument. - Taking an
&mut [u8]argument.
My function will be producing 1-9 bytes on each call.
I want it to be easily useable, and efficient, to:
- Feed an iterator pipeline which consumes bytes.
- Write to disk.
- Write to network.
- Write to memory.
Can I just implement the function once, and trust the the compiler will make a single implementation efficient for all use cases, when the user adapts the output?
Or do I need to implement it three (or more) times to cover all the use-cases efficiently?