Error Compiling Old Rust project

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
   --> /home/michael/.cargo/registry/src/github.com-1ecc6299db9ec823/socket2-0.3.12/src/sockaddr.rs:176:9
    |
176 |         mem::transmute::<SocketAddrV4, sockaddr_in>(v4);
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: source type: `SocketAddrV4` (48 bits)
    = note: target type: `sockaddr_in` (128 bits)

Hi,
I'm getting this error while trying to run my old rust project. Finding it hard to fix

In short, that transmute was always unsound because it assumes that the Rust SocketAddrV4 type is the same as its C counterpart, but that was never guaranteed.

See PR 78802. You can follow some of the links to PRs in popular packages to see how they replaced pointer casts or transmutes with code to construct the sockaddr_in more manually.

3 Likes

You'll probably want to update your dependencies. The version of socket2 you're using (possibly indirectly through some other dependency) is over two years old and has since been yanked[1] from crates.io due to this issue: Yank all `0.3` versions older than `0.3.16` · Issue #139 · rust-lang/socket2 · GitHub. Old versions of crates are supposed to compile on newer versions of Rust, but due to this problem socket2 0.3.12 doesn't and you're getting an error.

If updating is too tricky, you can try using an older version of Rust. Something like

rustup install 1.50.0 // install Rust 1.50.0, which was released in February 2021
cargo +1.50.0 run // `cargo run` using version 1.50.0

  1. Yanking a crate version makes it unavailable to use in new projects, generally due to some serious issue with that version. ↩︎

3 Likes

(Oops, I missed that the error was in a dependency.)

@quinedot @Heliozoa Thanks, it was a dependency issue. I stick with the old dependencies in my cargo.toml file. I updated to latest version before and I guess that incited the error. Thanks once again