achanda / ipnetwork

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

Ipnetwork mismatched types #160

Closed matteyeux closed 2 years ago

matteyeux commented 2 years ago

Hi ! Since version 0.20.0 I fail to build my tool because there is a mismatch between IpNetwork types :

λ ~/dev/fail(main*) » cargo run
    Updating crates.io index
   Compiling fail v0.1.0 (/home/mathieu/dev/fail)
error[E0308]: mismatched types
 --> src/main.rs:8:17
  |
7 |             match ip {
  |                   -- this expression has type `ipnetwork::IpNetwork`
8 |                 IpNetwork::V4(ipv4) => {
  |                 ^^^^^^^^^^^^^^^^^^^ expected enum `ipnetwork::IpNetwork`, found enum `IpNetwork`
  |
  = note: perhaps two different versions of crate `ipnetwork` are being used?

error[E0308]: mismatched types
  --> src/main.rs:11:17
   |
7  |             match ip {
   |                   -- this expression has type `ipnetwork::IpNetwork`
...
11 |                 IpNetwork::V6(ipv6) => {
   |                 ^^^^^^^^^^^^^^^^^^^ expected enum `ipnetwork::IpNetwork`, found enum `IpNetwork`
   |
   = note: perhaps two different versions of crate `ipnetwork` are being used?

For more information about this error, try `rustc --explain E0308`.
error: could not compile `fail` due to 2 previous errors

Here is the snippet I try to build bellow :

use ipnetwork::IpNetwork;
use pnet_datalink;

fn main() {
    for iface in pnet_datalink::interfaces() {
        for ip in iface.ips {
            match ip {
                IpNetwork::V4(ipv4) => {
                    println!("ipv4 : {}", ipv4.ip());
                }
                IpNetwork::V6(ipv6) => {
                    println!("ipv6 : {}", ipv6.ip());
                }
            }
        }
    }
}

It also uses pnet_datalink, but I only updated ipnetwork, that's why I suspect the issue to be related to this repo (I'm new to Rust so I may be wrong).

Any idea how I can fix this problem ?

Thanks.

paolobarbolini commented 2 years ago

It also uses pnet_datalink, but I only update ipnetwork, that why I suspect the issue to be related to this repo

The issue is with the fact that you can't have types from a crate's version work with the same types from another version, even if type's structures are exactly equal. Make sure the versions match and it should all go back to working fine.

matteyeux commented 2 years ago

Oh that perfectly makes sense, I'll stay on 0.19.0 until ipnetwork version is bumped on libpnet.

thank you @paolobarbolini !