Attributes and modifiers
Beans has custom annotations for typed metadata and a small set of built-in declaration modifiers. Modifiers are words that sit before a declaration and change language behavior such as visibility, layout, ownership, or CPU requirements. This page lists those modifiers.
Visibility
Section titled “Visibility”pubmakes a declaration or field public outside its package. An unmarked name is visible only in its package.privapplies to a class or struct field or method. Only code inside the declaring type can access it, including when other code is in the same package. It works with static andinoutmethods. There is noprotectedvisibility.
See Source files and modules for package visibility.
Classes and methods
Section titled “Classes and methods”abstract classdeclares a class that cannot be constructed. It may contain bodylessabstract fnmethods.singleton classdeclares one eager instance, read asType.instance.staticdeclares a class field or a class/struct method owned by the type. Static members have noself.overrideis required when replacing a concrete or abstract base-class method, or an interface method that has a default body. It is optional for the first implementation of a bodyless interface requirement.- A private method cannot be
abstractoroverride, and an interface cannot declare one.
See Classes and Interfaces, abstract classes, and inheritance.
C interop
Section titled “C interop”extern "C"declares a C-ABI entity: a struct, union, function, global, or thread-local. See Foreign function interface.opaquegoes withextern "C" opaque struct Handleto declare an incomplete C type, valid only behindRawPtr.
Layout modifiers
Section titled “Layout modifiers”Two modifiers apply only to extern "C" structs and unions. A modifier that
moves bytes only means something against a fixed C layout, which is what
extern "C" promises:
packedremoves every byte of padding between fields.align(N)raises a record’s alignment, or one field’s.Nmust be a power of two, no larger than the target’s maximum (4096).
pub extern "C" packed struct Header { kind: u8 length: u32 checksum: u32 }extern "C" align(64) struct Counter { hits: u32 }extern "C" struct Slot { tag: u8 align(16) payload: u64 }Rules:
- Both names are contextual:
packedonly beforestruct/union, andalignonly when followed by(. A field or variable may still be namedpackedoralign. align(N)on a field can only raise its alignment. A fieldalign(N)inside apackedrecord is rejected, rather than letting one silently win over the other.- Classes, interfaces, enums, and functions reject both by name.
- Semantics are C’s, checked against Clang for every supported target.
CPU features
Section titled “CPU features”feature "name" fnmarks a function body as allowed to use that CPU feature’s instructions. Calling it, or storing it as a function value, requires the feature to be known present. See Compile-time features.
feature "aes" fn mix_fast(seed: int) -> int { /* ... */ }Ownership
Section titled “Ownership”uniquegoes withunique classto make a type a move-only outer handle. See Variables and constants.moveis a parameter mode that takes ownership of an argument. It is also the expressionmove name, which moves a value out of a binding.inoutis a parameter mode that aliases a mutable caller local for the call. The caller writesinoutat the call site and the argument must be avar.inout fndeclares a mutating struct method. It gets mutableselfand must be called on avarlocal. The caller does not writeinoutbefore the receiver.
asyncbeforefndeclares an async function.awaitinside an async body waits on an async call.
Both are contextual and stay usable as ordinary identifiers elsewhere. See Async and await.
Modifier order
Section titled “Modifier order”The standard order places visibility first, then the kind modifiers:
pub unique class, pub abstract class, pub singleton class, and
pub extern "C" struct. The C interop and layout modifiers stack in the same
chain: pub extern "C" packed struct.
Custom annotations can describe declarations, parameters, and locals, but they
do not add new language modifiers or replace rules such as priv, abstract,
singleton, or extern "C". The foreign function interface
page shows extern "C" and opaque in use, and
structs and unions shows the layout and method modifiers on
real records.