Warning when building public trait returning private struct

⚓ Rust    📅 2026-04-12    👤 surdeus    👁️ 5      

surdeus

Hi

I just noticed that we can create trait that returns an internal type and this type can be private

pub trait HasZipMetadata {
    // Required method
    fn get_metadata(&self) -> &ZipFileData; # the ZipFileData is not viewable/usable
}

Example: HasZipMetadata in zip::read - Rust

1. Should this really possible?

ZipFileData is not viewable/usable right?

But we don't get any errors or warning when compiling. The zip crate builds without error


But if I change pub ZipFileData to pub(crate) ZipFileData the build produces warning

warning: type `ZipFileData` is more private than the item `HasZipMetadata::get_metadata`
   --> src/read.rs:731:5
    |
731 |     fn get_metadata(&self) -> &ZipFileData;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method `HasZipMetadata::get_metadata` is reachable at visibility `pub`
    |
note: but type `ZipFileData` is only usable at visibility `pub(crate)`

2. But it was already the case no? In both case I cannot use ZipFileData right ?

Note:
Looks kinda similar to Referencing private struct

1 post - 1 participant

Read full topic

🏷️ Rust_feed