achanda / ipnetwork

A library to work with CIDRs in rust
Apache License 2.0
121 stars 38 forks source link

Add const unsafe `new_unchecked` to `Ipv4Network` & `Ipv6Network` #185

Closed aurelilys closed 9 months ago

aurelilys commented 9 months ago

Hello 😃

I've encountered a problem using your crate, when I want to define an &[Ipv4Network], I'm not able to get the result, unwrap() is not marked as const. One of the solutions I've found would be to introduce two new new_unchecked functions for Ipv4Network and Ipv6Network. This would make it possible to:

const TEST: &[Ipv4Network] = unsafe {
    &[
        Ipv4Network::new_unchecked(Ipv4Addr::new(127, 0, 0, 0), 8),
        Ipv4Network::new_unchecked(Ipv4Addr::new(10, 0, 0, 0), 8),
    ]
};
achanda commented 9 months ago

Thank, this is interesting. Is it possible for you to add some tests please?

aurelilys commented 9 months ago

Yes, of course, here are two tests added.