-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[to be discussed] lang: macro that calculates type size at compile time #1647
Comments
This would be much appreciated. Perhaps there could also be an additional macro to indicate the maximum length of a dynamic type. e.g. #[derive(size)]
pub struct MyData {
pub value: u64,
pub other_value: u8,
pub authority: Pubkey,
#[max_len = 10]
pub this_seed: Vec<u8>
} impl MyData {
pub const SIZE: usize = 8 + 1 + 32 + 10;
} Naturally it's also important that this be able to work recursively for structs containing custom types. e.g. #[derive(size)]
pub struct MyData {
#[max_len = 10]
pub custom_accounts: Vec<CustomAccount>
}
#[derive(size)]
pub struct CustomAccount {
pub value: u64,
pub other_value: u8,
} impl MyData {
pub const SIZE: usize = 10 * ( 8 + 1);
} |
If it works for the most common dynamic length type Another request, can we make this happen by default on the |
this feature is now much harder to achieve consistently since the introduction of account space reallocation. static assertions on dynamic type sizing isn't feasible anymore and the data fields within an account struct are no longer indicative of the actual space allocated to the account. |
This would have to refer not to the account's data slice size, but to the minimum size to required to store the type, which would still help for things like default space allocation. |
i guess thats true. once the account has been initialized with the default space allocation, the default space calculation isn't very relevant afterwards. could be misleading if people use it for data size comp filters on program account reads though. |
TO BE DISCUSSED
Problem
space calculation via
derive(Default)
is gone in favour of size declarations at compile time.This adds a little bit of work for users because they have to specify size in
init
even for types that are not dynamic.Solution
derive(size)
macro on types which expand into animpl
with aconst
variable that defines the type's size by reading its inner types' lengths.would result in the following added code:
The text was updated successfully, but these errors were encountered: