Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for the InitSpace macro #2521

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion docs/src/pages/docs/space.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ so there the `C` layout applies.

In addition to the space for the account data, you have to add `8` to the `space` constraint for Anchor's internal discriminator (see the example).

## Type chart

| Types | Space in bytes | Details/Example |
| ---------- | ----------------------------- | ----------------------------------------------------------------------------------------------- |
| bool | 1 | would only require 1 bit but still uses 1 byte |
Expand All @@ -29,7 +31,7 @@ In addition to the space for the account data, you have to add `8` to the `space
| f32 | 4 | serialization will fail for NaN |
| f64 | 8 | serialization will fail for NaN |

# Example
## Example

```rust
#[account]
Expand Down Expand Up @@ -59,3 +61,33 @@ pub struct InitializeMyData<'info> {
pub system_program: Program<'info, System>
}
```

## The InitSpace macro

Sometimes it can be difficult to calculate the initial space of an account. This macro will add an `INIT_SPACE` constant to the structure. It is not necessary for the structure to contain the `#[account]` macro to generate the constant. Here's an example:

```rust
#[account]
#[derive(InitSpace)]
pub struct ExampleAccount {
pub data: u64,
#[max_len(50)]
pub string_one: String,
#[max_len(10, 5)]
pub nested: Vec<Vec<u8>>,
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
#[account(init, payer = payer, space = 8 + ExampleAccount::INIT_SPACE)]
pub data: Account<'info, ExampleAccount>,
}
```

A few important things to know:

- Don't forget the discriminator when defining `space`
- The `max_len` length represents the length of the structure, not the total length. (ie: the `max_len` of a Vec<u32> will be `max_len` \* 4)
2 changes: 1 addition & 1 deletion lang/derive/accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ use syn::parse_macro_input;
/// The given space number is the size of the account in bytes, so accounts that hold
/// a variable number of items such as a <code>Vec</code> should allocate sufficient space for all items that may
/// be added to the data structure because account size is fixed.
/// Check out the <a href = "https://book.anchor-lang.com/anchor_references/space.html" target = "_blank" rel = "noopener noreferrer">space reference</a>
/// Check out the <a href = "https://www.anchor-lang.com/docs/space" target = "_blank" rel = "noopener noreferrer">space reference</a>
/// and the <a href = "https://borsh.io/" target = "_blank" rel = "noopener noreferrer">borsh library</a>
/// (which anchor uses under the hood for serialization) specification to learn how much
/// space different data structures require.
Expand Down