packed_struct.rs
packed_struct.rs copied to clipboard
Allow to use on private structs
Using PackedStruct on private structs results in an error:
error[E0446]: private type `FooPacked` in public interface
--> lib/foo.rs:13:10
|
13 | #[derive(PackedStruct)]
| ^^^^^^^^^^^^ can't leak private type
14 | #[packed_struct(endian = "msb", bit_numbering = "msb0")]
15 | struct FooPacked {
| - `FooPacked` declared as private
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info
Is it possible to make it usable on private (and pub(crate)
) structs too?
Environment:
- Rust 1.46.0
- packed_struct 0.3.0
pub(crate)
didn't exist back then, so a bunch of things will have to be upgraded for this to work, but yes, for now, everything has to be public. You can always declare your module as private and your lib won't export these types.
From looking at the source code, it seems the only reason why this error occurs is because this code creates a pub
function using the type. But that function is only used in one place, so simply removing the pub
or declaring the function inside of the other function gets rid of this error and allows deriving on structs with any visibility.
Is there a reason why this is not the case?