Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TCPSockets in a SocketSet without 'static storage lifetime #304

Closed

Conversation

richardeoin
Copy link
Contributor

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 fails to compile without this patch (tested on rustc 1.38.0-nightly (07e0c3651 2019-07-16))

`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.
@crawford
Copy link
Contributor

At a glance, it looks like this might fix #223.

@whitequark
Copy link
Contributor

@m-labs-homu r+

Very nice, thank you.

@m-labs-homu
Copy link

📌 Commit 3ccb327 has been approved by whitequark

@m-labs-homu
Copy link

⌛ Testing commit 3ccb327 with merge 1ada3da...

@m-labs-homu
Copy link

☀️ Test successful - status-travis
Approved by: whitequark
Pushing 1ada3da to master...

@richardeoin richardeoin deleted the tcpsocket-into-lifetime branch August 17, 2019 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants