-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
generics_test.rs
55 lines (48 loc) · 1.46 KB
/
generics_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![allow(dead_code)]
use anchor_lang::prelude::borsh::maybestd::io::Write;
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;
// Needed to declare accounts.
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[derive(Accounts)]
pub struct GenericsTest<'info, T, U, const N: usize>
where
T: AccountSerialize + AccountDeserialize + Owner + Clone,
U: BorshSerialize + BorshDeserialize + Default + Clone,
{
pub non_generic: AccountInfo<'info>,
pub generic: Account<'info, T>,
pub const_generic: AccountLoader<'info, FooAccount<N>>,
pub const_generic_loader: AccountLoader<'info, FooAccount<N>>,
pub associated: Account<'info, Associated<U>>,
}
#[account(zero_copy(unsafe))]
pub struct FooAccount<const N: usize> {
pub data: WrappedU8Array<N>,
}
#[account]
#[derive(Default)]
pub struct Associated<T>
where
T: BorshDeserialize + BorshSerialize + Default,
{
pub data: T,
}
#[derive(Copy, Clone)]
pub struct WrappedU8Array<const N: usize>(u8);
impl<const N: usize> BorshSerialize for WrappedU8Array<N> {
fn serialize<W: Write>(&self, _writer: &mut W) -> borsh::maybestd::io::Result<()> {
todo!()
}
}
impl<const N: usize> BorshDeserialize for WrappedU8Array<N> {
fn deserialize(_buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
todo!()
}
}
impl<const N: usize> Owner for WrappedU8Array<N> {
fn owner() -> Pubkey {
crate::ID
}
}