Private fields with accessors

From 0xMiden/protocol · View interactive version

Private fields with accessors is an AI agent skill from the GitHub repository 0xMiden/protocol. Use when adding a public struct or changing a field's visibility in a Rust library crate — keep fields encapsulated so the layout can evolve without breaking callers.

How to use this skill

Skill file

Keep Struct Fields Private; Expose Accessors

Rule

Public structs in library crates have private fields. Read access goes through an pub fn field(&self) -> &T accessor; mutation goes through dedicated methods (no pub fn field_mut).

Exceptions:

Why

Public fields freeze the representation: you can't rename, retype, split, or compute-on-read them without breaking every caller. An accessor lets a field become a computed expression in a later release with no one noticing.

Examples

// Good
pub struct FungibleTokenMetadata {
    name: Box<str>,
    supply: u64,
}

impl FungibleTokenMetadata {
    pub fn name(&self) -> &str { &self.name }
    pub fn supply(&self) -> u64 { self.supply }
}

// Bad: every consumer locked to these field names and types forever
pub struct FungibleTokenMetadata {
    pub name: String,
    pub supply: u64,
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.