pub struct FieldBuilder { /* private fields */ }Expand description
Builder for constructing Field instances in const contexts.
This builder is primarily used by derive macros to generate more compact code.
All methods are const fn to allow usage in static/const contexts.
§Example
// For normal fields (default, most efficient):
const FIELD: Field = FieldBuilder::new(
"field_name",
<T as Facet>::SHAPE,
offset_of!(MyStruct, field_name)
).build();
// For recursive type fields (use lazy to break cycles):
const FIELD: Field = FieldBuilder::new_lazy(
"children",
|| <Vec<Self> as Facet>::SHAPE,
offset_of!(MyStruct, children)
).build();Implementations§
Source§impl FieldBuilder
impl FieldBuilder
Sourcepub const fn new(
name: &'static str,
shape: fn() -> &'static Shape,
offset: usize,
) -> FieldBuilder
pub const fn new( name: &'static str, shape: fn() -> &'static Shape, offset: usize, ) -> FieldBuilder
Creates a new FieldBuilder with a shape function.
The shape is provided as a function pointer to enable lazy evaluation, which improves compile times by deferring const evaluation to runtime.
Use the shape_of::<T> helper function for the common case:
FieldBuilder::new("field", shape_of::<i32>, offset)Sourcepub const fn new_lazy(
name: &'static str,
shape: fn() -> &'static Shape,
offset: usize,
) -> FieldBuilder
pub const fn new_lazy( name: &'static str, shape: fn() -> &'static Shape, offset: usize, ) -> FieldBuilder
Alias for new - kept for backward compatibility.
Previously used for recursive types, but now all fields use lazy shape references.
Sourcepub const fn attributes(self, attributes: &'static [Attr]) -> FieldBuilder
pub const fn attributes(self, attributes: &'static [Attr]) -> FieldBuilder
Sets the attributes for this field.
Sourcepub const fn doc(self, doc: &'static [&'static str]) -> FieldBuilder
pub const fn doc(self, doc: &'static [&'static str]) -> FieldBuilder
Sets the documentation for this field.
Sourcepub const fn flags(self, flags: FieldFlags) -> FieldBuilder
pub const fn flags(self, flags: FieldFlags) -> FieldBuilder
Sets the flags for this field.
Sourcepub const fn rename(self, rename: &'static str) -> FieldBuilder
pub const fn rename(self, rename: &'static str) -> FieldBuilder
Sets the rename for this field.
Sourcepub const fn alias(self, alias: &'static str) -> FieldBuilder
pub const fn alias(self, alias: &'static str) -> FieldBuilder
Sets the alias for this field.
Sourcepub const fn default_from_trait(self) -> FieldBuilder
pub const fn default_from_trait(self) -> FieldBuilder
Sets the default to use the type’s Default trait.
Sourcepub const fn default_custom(
self,
f: unsafe fn(PtrUninit) -> PtrMut,
) -> FieldBuilder
pub const fn default_custom( self, f: unsafe fn(PtrUninit) -> PtrMut, ) -> FieldBuilder
Sets a custom default function.
Sourcepub const fn skip_serializing_if(
self,
f: unsafe fn(PtrConst) -> bool,
) -> FieldBuilder
pub const fn skip_serializing_if( self, f: unsafe fn(PtrConst) -> bool, ) -> FieldBuilder
Sets the skip_serializing_if predicate.
Sourcepub const fn invariants(self, f: unsafe fn(PtrConst) -> bool) -> FieldBuilder
pub const fn invariants(self, f: unsafe fn(PtrConst) -> bool) -> FieldBuilder
Sets the invariants validation function.
Sourcepub const fn proxy(self, proxy: &'static ProxyDef) -> FieldBuilder
Available on crate feature alloc only.
pub const fn proxy(self, proxy: &'static ProxyDef) -> FieldBuilder
alloc only.Sets the proxy definition for custom ser/de.
Sourcepub const fn metadata(self, kind: &'static str) -> FieldBuilder
pub const fn metadata(self, kind: &'static str) -> FieldBuilder
Marks this field as storing metadata of the given kind.
Metadata fields are excluded from structural hashing and equality, and are populated by deserializers that support the metadata kind.
Common metadata kinds:
"span": Source byte offset and length"line": Source line number"column": Source column number