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

More general interface for UdpFramed #5969

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GoldsteinE
Copy link

@GoldsteinE GoldsteinE commented Sep 1, 2023

Previously UdpFramed only accepted smart pointers to UdpSocket. This commit changes it to accept anything that has .poll_recv_from() and .poll_send_to(). It allows for easier testing and more composability.

Motivation

My specific usecase is the following: I have a wrapper for UdpSocket that records some low-level metrics. It’s impossible to do so on a higher level (i.e. wrapping UdpFramed), because it requires basically replacing .poll_recv() with different syscalls. I still want to re-use framing logic, but I need to pass a different type to it.

UdpFramed also doesn’t allow to pass a mock implementation for testing.

Solution

I added new traits for sending / receiving datagrams and relaxed the requirements of UdpFramed to make it use these traits.

Design notes

  • Two traits instead of a single trait to allow for receive/send-only streams.
  • I placed it in udp/, because the interface is really specific. Unix sockets have similar APIs, but use paths instead of socket addrs.
  • Blanket impl is really unfortunate, since it poses possible coherence issues for user-provided implementation, but is required to retain backwards compatibility.
  • Traits take &mut self instead of &self to make external implementations easier.

@GoldsteinE GoldsteinE force-pushed the generalize-udp-framed branch 3 times, most recently from 5b3f0b0 to c6e7472 Compare September 1, 2023 10:34
Previously `UdpFramed` only accepted smart pointers to `UdpSocket`.
This commit changes it to accept anything that has `.poll_recv_from()`
and `.poll_send_to()`. It allows for easier testing and more
composability.
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-net Module: tokio/net labels Sep 2, 2023
Comment on lines +28 to +36
impl<T: Borrow<UdpSocket>> PollRecvFrom for T {
fn poll_recv_from(
&mut self,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<SocketAddr>> {
(*self).borrow().poll_recv_from(cx, buf)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this, maybe this should just be added to the list for the next breaking tokio-util release.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s also an option to do both: merge this now and remove the blanket impl on the next breaking release.

@Darksonn Darksonn added the S-breaking-change A breaking change that requires manual coordination to be released. label Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-net Module: tokio/net S-breaking-change A breaking change that requires manual coordination to be released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants