Visibility
It is possible to control the visibility of FieldX-generated entities. This can be done by using the vis(...)
argument (or sub-argument) with corresponding visibility levels using the pub
declaration. Private visibility is achieved by using no sub-argument with vis
: vis()
; there is also an alias private
for it.
By default, the visibility level is inherited from the field or the struct declaration. More precisely, there is a priority order of visibility levels that FieldX follows:
- The immediate declaration for an entity:
get(vis(pub))
. - Field-level declaration:
#[fieldx(vis(pub), get)]
. - Struct-level default for the entity:
#[fxstruct(get(vis(pub)))]
. - Struct-level default:
#[fxstruct(vis(pub))]
. - Field explicit declaration:
pub foo: usize
. - Struct declaration:
pub struct Foo { ... }
.
Don't ignore the "explicit" word at the field level! It means that if the field is private, FieldX skips the step and goes directly to the struct declaration. The reason for this is that the struct is considered part of some kind of API of a module, and as such, it is better for the methods it provides to be exposed at the same level because they're part of the same API.
Think of it in terms of a field accessor where the field itself is recommended to be private, while the accessor is recommended to be accessible anywhere the struct is accessible.
use fieldx::fxstruct;
#[fxstruct(get, builder)]
pub struct Book {
title: String,
author: String,
year: u32,
#[fieldx(
get(copy, vis(pub(crate))),
get_mut(private),
inner_mut,
builder(private)
)]
available: u16,
#[fieldx(
set("place_into", private),
inner_mut,
default("unknown".to_string()),
builder(private)
)]
location: String,
}
The levels in the snippet are somewhat arbitrary for the sake of demonstrating the feature.