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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Opt-in zero-copy POD (Plain Old Data) casting.
//!
//! Provides a safe API for reinterpreting byte slices as typed structs,
//! when the struct is `#[repr(C)]`, `Copy`, and all bit patterns are valid.
//!
//! ## Alignment
//!
//! Solana's loader aligns program input to 8-byte boundaries. The
//! functions below always check alignment on all targets and return
//! `Err(InvalidAccountData)` if the pointer is misaligned. This prevents
//! UB for types with alignment > 8 (e.g. `u128`). Use `LeU128` / `Le*`
//! wrappers for 16-byte scalars.
//!
//! - **`pod_from_bytes` / `pod_from_bytes_mut`**: return a direct
//! reference into the byte slice (zero-copy). Return
//! `Err(InvalidAccountData)` if the pointer is misaligned.
//! - **`pod_read`**: copies via `read_unaligned`, so it works
//! regardless of pointer alignment. Returns an owned `T`, not a
//! reference. Ideal for native tests with uncontrolled alignment.
//!
//! # Usage
//!
//! ```rust,ignore
//! #[repr(C)]
//! #[derive(Clone, Copy)]
//! struct MyState {
//! value: u64,
//! counter: u32,
//! }
//!
//! // SAFETY: MyState is repr(C), Copy, and all bit patterns are valid.
//! unsafe impl Pod for MyState {}
//! impl FixedLayout for MyState { const SIZE: usize = 12; }
//!
//! let state = pod_from_bytes::<MyState>(&data)?;
//! ```
use ProgramError;
/// Marker trait for types that can be safely transmuted from any byte pattern.
///
/// # Safety
///
/// The implementing type must be:
/// - `#[repr(C)]` or `#[repr(transparent)]`
/// - `Copy`
/// - Valid for all possible bit patterns (no padding-dependent invariants)
/// - No interior references or pointers
pub unsafe
/// Implement `Pod` for a list of types in one shot.
///
/// Saves you from writing N identical `unsafe impl Pod for T {}` blocks.
///
/// ```rust,ignore
/// impl_pod!(u8, u16, u32, u64, MyReprCStruct);
/// ```
// Blanket impls for primitives.
impl_pod!;
/// Trait for types with a known compile-time size.
/// Reinterpret a byte slice as an immutable reference to `T`.
///
/// Returns `AccountDataTooSmall` if the slice is shorter than `T::SIZE`.
/// Returns `InvalidAccountData` if the pointer is misaligned.
///
/// Solana's loader aligns program input to 8-byte boundaries, which is
/// sufficient for most layout types but not for alignments > 8 (e.g.
/// `u128`). Use [`LeU128`](crate::abi::LeU128) for 16-byte scalars.
/// For alignment-safe access by copy, use [`pod_read`] instead.
/// Reinterpret a mutable byte slice as a mutable reference to `T`.
///
/// Returns `AccountDataTooSmall` if the slice is shorter than `T::SIZE`.
/// Returns `InvalidAccountData` if the pointer is misaligned.
///
/// See [`pod_from_bytes`] for alignment details.
/// Read a `T` value from a byte slice by copy (alignment-safe on all targets).
///
/// Unlike [`pod_from_bytes`] this always works regardless of pointer
/// alignment, making it ideal for native tests. Returns an owned `T`.
///
/// ```rust,ignore
/// let header: AccountHeader = pod_read::<AccountHeader>(&data)?;
/// ```
/// Write a Pod value into a byte slice at offset 0.