generics_test.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #![allow(dead_code)]
  2. use anchor_lang::prelude::borsh::maybestd::io::Write;
  3. use anchor_lang::prelude::*;
  4. use borsh::{BorshDeserialize, BorshSerialize};
  5. // Needed to declare accounts.
  6. declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
  7. #[derive(Accounts)]
  8. pub struct GenericsTest<'info, T, U, const N: usize>
  9. where
  10. T: AccountSerialize + AccountDeserialize + Clone,
  11. U: BorshSerialize + BorshDeserialize + Default + Clone,
  12. {
  13. pub non_generic: AccountInfo<'info>,
  14. pub generic: ProgramAccount<'info, T>,
  15. pub const_generic: Loader<'info, Account<N>>,
  16. pub associated: CpiAccount<'info, Associated<U>>,
  17. }
  18. #[account(zero_copy)]
  19. pub struct Account<const N: usize> {
  20. pub data: WrappedU8Array<N>,
  21. }
  22. #[account]
  23. #[derive(Default)]
  24. pub struct Associated<T>
  25. where
  26. T: BorshDeserialize + BorshSerialize + Default,
  27. {
  28. pub data: T,
  29. }
  30. #[derive(Copy, Clone)]
  31. pub struct WrappedU8Array<const N: usize>(u8);
  32. impl<const N: usize> BorshSerialize for WrappedU8Array<N> {
  33. fn serialize<W: Write>(&self, _writer: &mut W) -> borsh::maybestd::io::Result<()> {
  34. todo!()
  35. }
  36. }
  37. impl<const N: usize> BorshDeserialize for WrappedU8Array<N> {
  38. fn deserialize(_buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
  39. todo!()
  40. }
  41. }