#visibility #field

macro field-visibility

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

1 unstable release

0.1.0 Sep 1, 2024

#1349 in Rust patterns

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

~265–720KB
~17K SLoC