Closed paul-schaaf closed 2 years ago
Im not sure this is possible with the current anchor architecture though. User
is not part of the Accounts struct so derive(Accounts)
cannot check whether Default
is implemented for it.
The #[account]
macro should calculate the size at compile time in the same way the ts client does--and use that calculation to implement some type of size/space trait, which can be used instead of Default.
ok so how about we have the account macro traverse the fields of the data structure and e.g. generates an
impl MyAccount {
const BORSH_MAXIMUM_SIZE: usize = #size;
}
this would fail if there is a type with a dynamic size (vec, string, etc.). Then you would have to do
#[account(size = 1337)]
pub struct MyAccount {...}
which then results in
impl MyAccount {
const BORSH_MAXIMUM_SIZE: usize = 1337;
}
one problem that arises with this approach is that space
is now connected to MyAccount
. So your accounts of the same type all have the same size. probably in most cases this is fine but we could still keep the space
constraint in init
that would allow an override of BORSH_MAXIMUM_SIZE
calculation using Default
is no longer supported so this is obsolete
This code errors with
because the
space
constraint is not given which means that anchor will try to use the default trait to infer it.The error should be sth like: