Crates.io | magic-space-derive |
lib.rs | magic-space-derive |
version | 0.1.0 |
source | src |
created_at | 2024-08-22 18:48:23.431623 |
updated_at | 2024-08-22 18:48:23.431623 |
description | Derive macro meant to be used with magic-space. Like anchor's InitSpace but without the dependency on anchor. |
homepage | https://github.com/kevinrodriguez-io/magic-space-derive |
repository | https://github.com/kevinrodriguez-io/magic-space-derive |
max_upload_size | |
id | 1348101 |
size | 11,243 |
Yes, It's AI generated.
Derive macro for Solana program state structs that automatically calculates the space required for the struct.
Code mostly taken from @coral-xyz/anchor
project, modified to work without the anchor crate.
Source:
Add the following to your Cargo.toml
:
[dependencies]
magic_space = "^0.1.0"
Or cargo add:
$ cargo add magic-space
use magic_space::*;
#[derive(MagicSpace)]
struct SomeAccount {
apples: u32,
oranges: u32,
#[max_len(10)]
others: Vec<u32>,
#[max_len(10)]
name: String,
#[max_len(10, 5, 5)]
some_other: Vec<Vec<Vec<u8>>>,
#[max_len(10)]
e: Vec<Option<SomeEnum>>,
}
If you have dynamic allocation, you can still use the max_len attribute as follows: #[max_len(0)]
and avoid worrying about the vector length, that way you can apply the following pattern:
#[derive(MagicSpace)]
pub struct DynamicSizeVecPattern {
#[max_len(0)]
pub data: Vec<Item>,
}
impl DynamicSizeVecPattern {
pub fn size(vec_len: usize, str_len: usize) -> usize {
Self::MAGIC_SPACE + (vec_len * Item::size(str_len))
}
}
#[derive(MagicSpace)]
pub struct Item {
pub data: u64,
#[max_len(0)]
pub name: String,
}
Apache 2.0 LICENSE