Skip to content

Commit

Permalink
docs: Add doc for InitSpace macro (coral-xyz#2521)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen authored and ananas-block committed Jun 20, 2023
1 parent 65bdd27 commit 891127e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit 891127e

Please sign in to comment.