Skip to content

Ipv4Addr ordering and endianness #29691

Closed
@hroi

Description

@hroi

Sorting Ipv4Addrs results in unexpected ordering. Ordering is based on the internal representation of the Ipv4Addr without regard to "network order".

I tried this code on little-endian architecture:

use std::net::Ipv4Addr;

fn main() {
    let mut ips = vec![Ipv4Addr::new(100,64,3,1),
                       Ipv4Addr::new(100,64,3,3),
                       Ipv4Addr::new(192,0,2,2),
                       Ipv4Addr::new(192,0,2,4),];

    ips.sort();
    for ip in ips.iter() {
        println!("ip: {}, u32: {}", ip, u32::from(*ip));
    }
}

I expected to see this happen:

ip: 100.64.3.1, u32: 1681916673
ip: 100.64.3.3, u32: 1681916675
ip: 192.0.2.2, u32: 3221225986
ip: 192.0.2.4, u32: 3221225988

Instead, this happened:

ip: 100.64.3.1, u32: 1681916673
ip: 192.0.2.2, u32: 3221225986
ip: 100.64.3.3, u32: 1681916675
ip: 192.0.2.4, u32: 3221225988

Rust playground demonstration: http://is.gd/hEjytb.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions