Skip to main content

sol_parser_sdk/instr/
inner_common.rs

1//! 通用 Inner Instruction 解析工具
2//!
3//! 提供零拷贝、高性能的通用读取函数,供所有协议的 inner instruction 解析器使用
4
5/// 零拷贝读取 u8
6///
7/// # Safety
8///
9/// Caller must ensure `offset` is within `data`.
10#[inline(always)]
11pub unsafe fn read_u8_unchecked(data: &[u8], offset: usize) -> u8 {
12    *data.get_unchecked(offset)
13}
14
15/// 零拷贝读取 u16
16///
17/// # Safety
18///
19/// Caller must ensure `offset..offset + 2` is within `data`.
20#[inline(always)]
21pub unsafe fn read_u16_unchecked(data: &[u8], offset: usize) -> u16 {
22    let ptr = data.as_ptr().add(offset) as *const u16;
23    u16::from_le(ptr.read_unaligned())
24}
25
26/// 零拷贝读取 u32
27///
28/// # Safety
29///
30/// Caller must ensure `offset..offset + 4` is within `data`.
31#[inline(always)]
32pub unsafe fn read_u32_unchecked(data: &[u8], offset: usize) -> u32 {
33    let ptr = data.as_ptr().add(offset) as *const u32;
34    u32::from_le(ptr.read_unaligned())
35}
36
37/// 零拷贝读取 u64
38///
39/// # Safety
40///
41/// Caller must ensure `offset..offset + 8` is within `data`.
42#[inline(always)]
43pub unsafe fn read_u64_unchecked(data: &[u8], offset: usize) -> u64 {
44    let ptr = data.as_ptr().add(offset) as *const u64;
45    u64::from_le(ptr.read_unaligned())
46}
47
48/// 零拷贝读取 u128
49///
50/// # Safety
51///
52/// Caller must ensure `offset..offset + 16` is within `data`.
53#[inline(always)]
54pub unsafe fn read_u128_unchecked(data: &[u8], offset: usize) -> u128 {
55    let ptr = data.as_ptr().add(offset) as *const u128;
56    u128::from_le(ptr.read_unaligned())
57}
58
59/// 零拷贝读取 i32
60///
61/// # Safety
62///
63/// Caller must ensure `offset..offset + 4` is within `data`.
64#[inline(always)]
65pub unsafe fn read_i32_unchecked(data: &[u8], offset: usize) -> i32 {
66    let ptr = data.as_ptr().add(offset) as *const i32;
67    i32::from_le(ptr.read_unaligned())
68}
69
70/// 零拷贝读取 i64
71///
72/// # Safety
73///
74/// Caller must ensure `offset..offset + 8` is within `data`.
75#[inline(always)]
76pub unsafe fn read_i64_unchecked(data: &[u8], offset: usize) -> i64 {
77    let ptr = data.as_ptr().add(offset) as *const i64;
78    i64::from_le(ptr.read_unaligned())
79}
80
81/// 零拷贝读取 i128
82///
83/// # Safety
84///
85/// Caller must ensure `offset..offset + 16` is within `data`.
86#[inline(always)]
87pub unsafe fn read_i128_unchecked(data: &[u8], offset: usize) -> i128 {
88    let ptr = data.as_ptr().add(offset) as *const i128;
89    i128::from_le(ptr.read_unaligned())
90}
91
92/// 零拷贝读取 bool
93///
94/// # Safety
95///
96/// Caller must ensure `offset` is within `data`.
97#[inline(always)]
98pub unsafe fn read_bool_unchecked(data: &[u8], offset: usize) -> bool {
99    *data.get_unchecked(offset) == 1
100}
101
102/// 零拷贝读取 Pubkey (32 bytes)
103///
104/// # Safety
105///
106/// Caller must ensure `offset..offset + 32` is within `data`.
107#[inline(always)]
108pub unsafe fn read_pubkey_unchecked(data: &[u8], offset: usize) -> solana_sdk::pubkey::Pubkey {
109    use solana_sdk::pubkey::Pubkey;
110    let ptr = data.as_ptr().add(offset);
111    let mut bytes = [0u8; 32];
112    std::ptr::copy_nonoverlapping(ptr, bytes.as_mut_ptr(), 32);
113    Pubkey::new_from_array(bytes)
114}
115
116/// 零拷贝读取字符串(带长度前缀)
117///
118/// # Safety
119///
120/// Caller must ensure the length-prefixed bytes are readable and valid UTF-8.
121#[inline(always)]
122pub unsafe fn read_string_unchecked(data: &[u8], offset: usize) -> Option<(String, usize)> {
123    if data.len() < offset + 4 {
124        return None;
125    }
126
127    let len = read_u32_unchecked(data, offset) as usize;
128    if data.len() < offset + 4 + len {
129        return None;
130    }
131
132    let string_bytes = &data[offset + 4..offset + 4 + len];
133    let s = std::str::from_utf8_unchecked(string_bytes);
134    Some((s.to_string(), 4 + len))
135}
136
137/// 检查数据长度是否足够
138#[inline(always)]
139pub fn check_length(data: &[u8], required: usize) -> bool {
140    data.len() >= required
141}