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

impl Stream/Sink for UdpSocket #276

Open
canndrew opened this issue Nov 3, 2017 · 5 comments
Open

impl Stream/Sink for UdpSocket #276

canndrew opened this issue Nov 3, 2017 · 5 comments

Comments

@canndrew
Copy link

canndrew commented Nov 3, 2017

I'd like to use a UdpSocket as a Stream/Sink of (SocketAddr, Vec<u8>). Is there a reason these impls don't already exist? I'm aware I can use Framed, but I'd prefer not to have to write a UdpCodec for converting bytes to bytes. IMHO UdpCodec belongs in some higher-level library and tokio-core should just provide a UDP API for sending/receiving packets of bytes.

@alexcrichton
Copy link
Contributor

Thanks for the report! This sort of raises similar questions to tokio-rs/tokio-io#78 in that it's not clear what the right type is here (Vec<u8>? a reference counted buffer? etc).

I could imagine though that having a codec or two baked in would also be fine though!

@canndrew
Copy link
Author

canndrew commented Nov 7, 2017

I'm fine with either Bytes or Vec<u8>, I just think it's odd that I need to use a codec to do this since a UDP socket is already a stream of byte-chunks. I'm forced to go up a level of abstraction, then use BytesCodec to go back down.

An alternative, (though it may be less efficient, I haven't thought about this too hard), would be to have Sink/Stream adapters that turn a sink/stream of Bytes into a sink/stream of T where T: Serialize/Deserialize.

@alexcrichton
Copy link
Contributor

Indeed yeah! There's crate like tokio-serde-bincode to do that for you

@cramertj
Copy link
Contributor

cramertj commented Nov 7, 2017

@canndrew Would tokio-rs/tokio-io#82 help solve your usecase?

@canndrew
Copy link
Author

canndrew commented Nov 8, 2017

That would certainly be a more efficient of way doing it, I see the problem now with just making UdpSocket a Stream of Bytes. Maybe what I really want is for Stream::Item to have a lifetime parameter so that a Stream can return a &mut [u8] to an internal buffer. hmm...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants