logoalt Hacker News

erk__today at 9:46 AM1 replyview on HN

The std api can only create UdpSockets, the trick here is that you use Socket2 which allows more kinds of sockets and then you tell UdpSocket that some raw file descriptor is a upd socket through a unsafe api with no checks and I guess it works because they use the same api on posix.

Edit: It is possible in safe rust as well, see child comment.

The macro used by socket2: https://docs.rs/socket2/0.6.1/src/socket2/lib.rs.html#108

The FromRawFd trait: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.h...


Replies

the8472today at 11:07 AM

From/Into conversion via OwnedFd is the safe API, RawFd is the older and lower-level one.

show 1 reply