Skip to content

Add a SockAddr type #2

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

Merged
merged 2 commits into from
Jun 13, 2017
Merged

Add a SockAddr type #2

merged 2 commits into from
Jun 13, 2017

Conversation

sfackler
Copy link
Member

We don't want to be stuck only supporting IP sockets

We don't want to be stuck only supporting IP sockets
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

👍 looks like exactly what I envisioned

Want to add a to_socket_addr method as well to convert to libstd? A follow-up can add a unix-specific conversion to the unix socket paths as well.

return None;
}

Some(mem::transmute_copy(&self.storage))
Copy link
Member

Choose a reason for hiding this comment

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

One day this may bite us, but let's hope it's way far away!

Copy link
Member Author

Choose a reason for hiding this comment

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

I was somewhat terrified when I saw that this was how the addr2raw function was implemented as well, but it works.


/// Returns this address as a `SocketAddrV4` if it is in the `AF_INET`
/// family.
pub fn as_inet(&self) -> Option<SocketAddrV4> {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this could return &SocketAddrV4 instead? (a relatively large struct to copy as we go down)

Copy link
Member Author

Choose a reason for hiding this comment

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

That will lock us into guaranteeing that SocketAddrV4 has the same representation as sockaddr_in, but if that's okay then sure.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wait good point, let's not do that.

impl From<SocketAddrV4> for SockAddr {
fn from(addr: SocketAddrV4) -> SockAddr {
unsafe {
SockAddr::from_raw_parts(&addr as *const _ as *const _,
Copy link
Member

Choose a reason for hiding this comment

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

Could this throw in some assert_eq! about the size of SocketAddrV4? (same for v6 below)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done via transmute to check statically.

@sfackler
Copy link
Member Author

to_socket_addr may not be the best name since there are both std::net::SocketAddr and std::os::unix::net::SocketAddr :(

@alexcrichton
Copy link
Member

Ok, let's stick with it as-is and fix up ergonomics as we see fit. Thanks again!

(if you'd like a publish of this just lemme know)

@alexcrichton alexcrichton merged commit 9abb700 into rust-lang:master Jun 13, 2017
@alexcrichton
Copy link
Member

Er actually, added you as collaborator + crate owner, feel free to publish if you need it! (just don't forget to bump the version)

@sfackler sfackler deleted the sockaddr branch June 13, 2017 02:44
@sfackler
Copy link
Member Author

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants