#visibility #attributes #field #struct #macro

macro field-visibility

Attribute to specify the visibility of all fields in a Rust struct

1 unstable release

0.1.0 Sep 1, 2024

#2243 in Rust patterns

Download history 139/week @ 2024-09-01 2/week @ 2024-09-08 15/week @ 2024-09-15

156 downloads per month

MIT license

4KB

field-visibility

Attribute to specify the visibility of all fields in a Rust struct.

Usage

Examples

The argument is the visibility mode (i.e. pub or pub(crate)).

To set all fields in the struct to public:

use field_visibility::visibility;

#[visibility(pub)] // Or `pub(crate)`, etc.
struct Widget {
    a: i32,
    b: i32,
}

This expands to:

use field_visibility::visibility;

#[visibility(pub)]
struct Widget {
    pub a: i32,
    pub b: i32,
}

When using this attribute, the visibility of individual fields can be set.

use field_visibility::visibility;

#[visibility(pub)]
struct SomeStruct {
    field1: i32,
    field2: i32,

    pub(crate) crate_field: i32,

    pub(self) private_field: i32, // Equivalent to private
}

The expansion of this is:

struct SomeStruct {
    pub field1: i32,
    pub field1: i32,

    pub(crate) crate_field: i32,

    pub(self) private_field: i32,
}

Dependencies

~260–710KB
~17K SLoC