packed_struct.rs icon indicating copy to clipboard operation
packed_struct.rs copied to clipboard

Allow to use on private structs

Open njam opened this issue 4 years ago • 2 comments

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

njam avatar Oct 20 '20 15:10 njam

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.

rudib avatar Oct 21 '20 06:10 rudib

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?

mich101mich avatar Jan 06 '24 20:01 mich101mich