Your code allows you to copy T , not just primitives. This means that you can try to parse something with a link, which is probably not what you want:
However, a general sketch of your code is what I expect. You need a temporary place to store some data, and then you need to convert this data to the corresponding primitive (possibly dealing with problems that result from endinaness).
I would recommend the byteorder library. With it, you invoke specific methods for the primitive that is required:
reader.read_u16::<LittleEndian>()
Since these methods know the desired size, they can allocate a stack for the array, which will be used as a temporary buffer, which is likely to be more efficient than heap allocation. In addition, I suggest changing the code to accept a shared object with a Read tag instead of a specific GzDecoder .
You can also look at a serialization library like rustc-serialize or serde to see if they are suitable for your use.
source share