smoltcp-rs / smoltcp

a smol tcp/ip stack
BSD Zero Clause License
3.64k stars 404 forks source link

Not possible to obtain reference to payload from reference to owned packet. #822

Closed sammko closed 11 months ago

sammko commented 11 months ago

I find that it is sometimes practical (mostly for simplicity's sake) to work with owned packets, e.g. TcpPacket<Vec<u8>>. However, currently it is impossible to obtain a reference to the payload (&'a [u8]) from a &'a TcpPacket<Vec<u8>>, because the payload() method is only implemented on TcpPacket<&'b T>, returning a &'b [u8]. On the other hand payload_mut is fine. It is not completely trivial to move the implementation of payload to the generic impl, becuase of the different lifetime which is relied on elsewhere. Would you consider adding a way to do this? Either by somehow making the current payload() more generic, or adding a new method.

sammko commented 11 months ago

Oh, oops, there's as_ref(). Sorry for the noise. I'm still not sure how to use it with TcpRepr::parse, without introducing an intermediate Packet<&..>.