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

Add From<[u16; 8]> to Ipv6Addr #38131

Merged
merged 1 commit into from
Dec 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,14 @@ impl From<[u8; 16]> for Ipv6Addr {
}
}

#[stable(feature = "ipv6_from_segments", since = "1.15.0")]
impl From<[u16; 8]> for Ipv6Addr {
fn from(segments: [u16; 8]) -> Ipv6Addr {
let [a, b, c, d, e, f, g, h] = segments;
Copy link
Contributor

@achanda achanda Dec 6, 2016

Choose a reason for hiding this comment

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

We should be able to avoid the allocations here by directly indexing the array in the call below

Copy link
Member

Choose a reason for hiding this comment

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

How is this allocating anything?

Copy link
Contributor

@achanda achanda Dec 6, 2016

Choose a reason for hiding this comment

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

Sorry if I'm mistaken, but isn't that line allocating 8 new u16s?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if allocation is the right way to describe that. The compiler can and will significantly transform data manipulations.

Copy link
Contributor Author

@clarfonthey clarfonthey Dec 6, 2016

Choose a reason for hiding this comment

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

I was under the impression that this is just providing aliases for each of the indexed variables, but I could be wrong. I assumed that bound variables and registers/stack slots was not an onto mapping.

I also specifically used a pattern match to avoid any bounds checking on the array at runtime.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense then, thanks!

Ipv6Addr::new(a, b, c, d, e, f, g, h)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a test like it's v4 counterpart

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree! I added some.

// Tests for this module
#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
Expand Down Expand Up @@ -1413,10 +1421,28 @@ mod tests {
}

#[test]
fn ipv4_from_u32_slice() {
fn ipv4_from_octets() {
assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1))
}

#[test]
fn ipv6_from_segments() {
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff]);
let new = Ipv6Addr::new(0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff);
assert_eq!(new, from_u16s);
}

#[test]
fn ipv6_from_octets() {
let from_u16s = Ipv6Addr::from([0x0011, 0x2233, 0x4455, 0x6677,
0x8899, 0xaabb, 0xccdd, 0xeeff]);
let from_u8s = Ipv6Addr::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(from_u16s, from_u8s);
}

#[test]
fn ord() {
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
Expand Down