Skip to content

Commit

Permalink
Allow TCPSockets in a SocketSet without 'static storage lifetime
Browse files Browse the repository at this point in the history
`TCPSocket` buffers are type `RingBuffer<'a, _>` whilst other
socket types have buffers with type `PacketBuffer<'a, 'b,
_>`.

There is an enum over socket types `pub enum Socket<'a, 'b: 'a>`,
 which is in turn used by `set::Item` and `SocketSet`. The lifetime
 `'b` is not required for `TcpSocket`, but the `Into<Socket>`
 implementation for `TCPSocket` specifies the `'static` lifetime for
 `'b`.

Therefore using `TCPSocket` in a `SocketSet` currently requires the
lifetime `'b` for other sockets to be `'static` which is unnecessarily
restrictive. This patch fixes this.

The lifetime signature of socket specifies `'b: 'a`, that is `'b`
outlives `'a`. Instead of `'static`, this is also satisfied by
`impl<'a> Into<Socket<'a, 'a>> for TcpSocket<'a>` as `'a` "outlives"
`'a` by definition.

The example
[here](https://gist.github.com/richardeoin/b562e79d0d499e00a8bf4944f00594d9)
fails to compile without this patch.

Closes: smoltcp-rs#304
Approved by: whitequark
  • Loading branch information
richardeoin authored and Homu committed Aug 15, 2019
1 parent efdde33 commit 1ada3da
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,8 @@ impl<'a> TcpSocket<'a> {
}
}

impl<'a> Into<Socket<'a, 'static>> for TcpSocket<'a> {
fn into(self) -> Socket<'a, 'static> {
impl<'a> Into<Socket<'a, 'a>> for TcpSocket<'a> {
fn into(self) -> Socket<'a, 'a> {
Socket::Tcp(self)
}
}
Expand Down

0 comments on commit 1ada3da

Please sign in to comment.