Skip to main content

light_sdk_types/lca/
pubkey.rs

1//! `Pubkey` is the standard Solana pubkey type.
2//!
3//! Previously this module defined a custom `Pubkey([u8; 32])` newtype so it
4//! could implement zero-copy traits. For the general SDK surface we re-export
5//! `solana_pubkey::Pubkey` directly.
6//!
7//! [`ZeroCopyPubkey`] is a `[u8; 32]` newtype that additionally implements the
8//! `zerocopy` and `bytemuck` traits. It is used only where a pubkey must be
9//! embedded in a zero-copy / `Pod` struct (e.g. on-chain account metadata),
10//! since `solana_pubkey::Pubkey` does not implement those traits.
11
12pub use solana_pubkey::Pubkey;
13
14use bytemuck::{Pod, Zeroable};
15use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
16
17#[repr(C)]
18#[cfg_attr(
19    all(feature = "std", feature = "anchor"),
20    derive(anchor_lang::AnchorDeserialize, anchor_lang::AnchorSerialize)
21)]
22#[cfg_attr(
23    not(feature = "anchor"),
24    derive(borsh::BorshDeserialize, borsh::BorshSerialize)
25)]
26#[derive(
27    Pod,
28    Zeroable,
29    FromBytes,
30    IntoBytes,
31    KnownLayout,
32    Immutable,
33    Unaligned,
34    Debug,
35    Default,
36    Copy,
37    Clone,
38    PartialEq,
39    Eq,
40    Hash,
41)]
42pub struct ZeroCopyPubkey(pub [u8; 32]);
43
44impl ZeroCopyPubkey {
45    pub fn to_bytes(&self) -> [u8; 32] {
46        self.0
47    }
48}
49
50impl From<[u8; 32]> for ZeroCopyPubkey {
51    fn from(bytes: [u8; 32]) -> Self {
52        Self(bytes)
53    }
54}
55
56impl From<ZeroCopyPubkey> for [u8; 32] {
57    fn from(value: ZeroCopyPubkey) -> Self {
58        value.0
59    }
60}
61
62impl From<Pubkey> for ZeroCopyPubkey {
63    fn from(value: Pubkey) -> Self {
64        Self(value.to_bytes())
65    }
66}
67
68impl From<ZeroCopyPubkey> for Pubkey {
69    fn from(value: ZeroCopyPubkey) -> Self {
70        Pubkey::from(value.0)
71    }
72}