generics_test.rs 1.7 KB

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