Problems with "pub" being ineffective

A module structure problem has me puzzled.

.
├── main.rs
├── messages
│   ├── mod.rs
│   └── msgdecode.rs
├── pcap.rs
└── viewer
    └── mod.rs

Layout is per the Rust book.

Error message is:

error[E0603]: struct import `Msgdecode` is private
  --> src/pcap.rs:77:53
   |
77 | fn processudppkt(mut decoder: &messages::msgdecode::Msgdecode, pkt: &PacketHeaders)
   |                                                     ^^^^^^^^^ private struct import
   |
note: the struct import `Msgdecode` is defined here...
  --> src/messages/msgdecode.rs:13:5
   |
13 | use crate::messages::Msgdecode;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...and refers to the struct `Msgdecode` which is defined here
  --> src/messages/mod.rs:18:1
   |
18 | pub struct Msgdecode {
   | ^^^^^^^^^^^^^^^^^^^^ consider importing it directly

"mod.rs" is just a skeleton right now:

pub mod msgdecode;
pub struct Msgdecode {       
}

Everything is public.

In "pcap.rs", the module is made visible with

use crate::messages;

That's enough that the compiler can find "messsages/mod.rs" from "pcap.rs", but it insists it's private.

The error hint "Consider importing it directly" let me to try "mod pub messages", but that just led to dead ends like

pub mod crate::messages; // not allowed
pub mod messages; // not found, since it's at the same level in the tree

How do I fix the privacy problem?

You have something like this **in `main.rs`**:
mod messages;

That's where you need to add pub.

Never mind, I skimmed the error message and leapt to an incorrect conclusion.

Tried that. No effect, Here are the imports in main.rs now:

extern crate simplelog;
extern crate argparse;
use std::fs::File;
use std::collections::HashMap;
use simplelog::{LevelFilter,TermLogger,WriteLogger,Config};

pub mod messages;
mod pcap;

Just added the "pub" to "mod messages", as suggested.

Your struct Msgdecode is defined in the messages module, not in msgdecode, yet you refer to it as messages::msgdecode::Msgdecode

You do not need the pub modifier on pub mod messages; to access it from pcap because messages is a sibling to pcap

2 Likes

If you want it to be available at both paths, then you can change the use to pub use in msgdecode.rs:

pub use crate::messages::Msgdecode;

This will “re-export” the name so that it is visible as crate::messages::msgdecode::Msgdecode as well as at its original path crate::messages::Msgdecode.

3 Likes

Thanks. That works.