Skip to content

Remove lifetime from #[account] dynamic types (String/Vec fields) #123

@L0STE

Description

@L0STE

Context:
PR #119 removes the 'info lifetime from #[derive(Accounts)] structs by making account fields owned. However, #[account] state types with dynamic fields (String, Vec, tail) still require a lifetime parameter:

// Current — lifetime required for dynamic fields
#[account(discriminator = 1)]
pub struct Profile<'a> {
    pub owner: Address,
    pub name: String<u32, 32>,
    pub tags: Vec<Address, u32, 10>,
}

// This forces Accounts structs to carry the lifetime too:
#[derive(Accounts)]
pub struct UpdateProfile<'profile> {
    pub user: Signer,
    pub profile: Account<Profile<'profile>>,
}

Proposal:
Make the dynamic #[account] type own its AccountView instead of holding &'a mut AccountView, eliminating the lifetime:

// After — no lifetime
#[account(discriminator = 1)]
pub struct Profile {
    pub owner: Address,
    pub name: String<u32, 32>,
    pub tags: Vec<Address, u32, 10>,
}

#[derive(Accounts)]
pub struct UpdateProfile {
    pub user: Signer,
    pub profile: Account<Profile>,
}

Why this is safe:
Dynamic field accessors (name(), signers()) don't store references — they construct &str / &[T] on each call from the raw data pointer, bounded by &self. The lifetime on the state struct was only there because it held &'a mut AccountView. With an owned AccountView, the accessors work identically. The Inner struct (for set_inner) keeps its own method-scoped lifetime for borrowed &str/&[T] parameters.

Changes needed:

  • derive/src/account/dynamic.rs — struct owns AccountView, from_account_view uses ptr::read
  • derive/src/account/accessors.rs&*self.__view&self.__view
  • derive/src/account/mod.rs — remove lifetime requirement check

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions