coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.59k stars 1.32k forks source link

Custom zero copy collections #1388

Open armaniferrante opened 2 years ago

armaniferrante commented 2 years ago

We should create zero copy collections so that we can use vec, map, etc in zero copy types.

jarry-xiao commented 2 years ago

+1 This would be huge

archseer commented 2 years ago

I have an arrayvec-like implementation here: https://github.com/smartcontractkit/chainlink-solana/blob/develop/contracts/crates/arrayvec/src/lib.rs

Had to strip out the generics (and define the type outside the macro) to make the IDL generator happy.

lidatong commented 2 years ago

We should create zero copy collections so that we can use vec, map, etc in zero copy types.

it's worth noting these types are not Copy, and the ZeroCopy trait does have an explicit Copy bound:

pub trait ZeroCopy: Discriminator + Copy + Clone + Zeroable + Pod { }

what do you think of dropping the explicit Copy bound to make this possible?

archseer commented 2 years ago

There's a good reason for this: https://github.com/Lokathor/bytemuck/issues/87#issuecomment-1007577400

lidatong commented 2 years ago

There's a good reason for this: Lokathor/bytemuck#87 (comment)

i understand why Pod needs it, but you can unsafe impl Pod. here to satisfy #[account(zero_copy)] we would additionally need something like unsafe impl Copy which doesn't exist

lidatong commented 2 years ago

nvm, i was totally wrong here. bytemuck will yell at you if you unsafe impl Pod without the type being Copy. i had a misunderstanding about unsafe impl Pod vs. #[derive(Pod)] but saw in the source the macro is generating unsafe impl. sorry