Library build flags public functions as "dead code"


#1

I’m very puzzled about why rustc flags certain functions as dead code. At a high level my project structure looks as follows:


// in lib.rs

pub mod transport {

  pub use self::socket::TcpTransport;

  mod socket {
    pub struct TcpTransport {
      // some code here...
    }

    impl TcpTransport {
      fn new() -> TcpTransport { // <--- this is flagged as dead code
        // some code here
      }
    }
  }
}

Note that my actual project uses the above but with lib.rs, mod.rs, etc. Specifically:

+ top_level
|
+- lib.rs
+- transport
    |
    +- mod.rs
    +- socket.rs <-- TcpTransport definition and impl are here

I’m building this crate as a library crate and not sure why that function is being flagged as dead code. Any insights? (I’ve another dead-code question about a separate piece of code, but this one is simpler.)

EDIT:

Changed struct TcpTransport to pub struct TcpTransport to reflect actual code.


#2

Because it is dead code.

TcpTransport::new is a private method, meaning it can only be called by code in the same module. Even if you make the type itself public (which you can’t, because it isn’t pub either), that doesn’t change whether individual methods are public or not.


#3

I updated the above code (the type is public). But you’re right - I did not make the new method public.

Thank you @DanielKeep.