Skip to content

Commit 6a1fbaa

Browse files
authored
instruction: Add no_std support (anza-xyz#627)
* Use alloc directly * Add to check-no-std script * Clean up features
1 parent d6307ee commit 6a1fbaa

File tree

6 files changed

+15
-19
lines changed

6 files changed

+15
-19
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ tiny-bip39 = "2.0.0"
337337
toml = "0.8.23"
338338
uriparse = "0.6.4"
339339
wasm-bindgen = "0.2.100"
340-
wincode = { version = "0.4.7", features = ["derive"], default-features = false }
340+
wincode = { version = "0.4.8", features = ["derive"], default-features = false }
341341

342342
[profile.release]
343343
split-debuginfo = "unpacked"

instruction/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ bincode = ["dep:bincode", "dep:serde"]
1919
borsh = ["dep:borsh"]
2020
default = ["std"]
2121
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro", "serde", "std"]
22-
serde = ["dep:serde", "dep:serde_derive", "solana-pubkey/serde"]
23-
std = ["serde?/std", "borsh?/std"]
24-
syscalls = ["std"]
22+
serde = ["serde/alloc", "dep:serde_derive", "solana-pubkey/serde"]
23+
# The `std` feature is currently unused and retained for backwards compatibility.
24+
std = []
25+
syscalls = []
2526
wincode = ["dep:wincode", "solana-pubkey/wincode"]
2627

2728
[dependencies]

instruction/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
#![allow(clippy::arithmetic_side_effects)]
1515
#![no_std]
1616

17-
#[cfg(feature = "std")]
18-
extern crate std;
19-
use solana_pubkey::Pubkey;
20-
#[cfg(feature = "std")]
21-
use std::vec::Vec;
17+
extern crate alloc;
18+
2219
pub mod account_meta;
23-
#[cfg(feature = "std")]
24-
pub use account_meta::AccountMeta;
25-
pub use solana_instruction_error as error;
2620
#[cfg(any(feature = "syscalls", target_os = "solana"))]
2721
pub mod syscalls;
2822

23+
pub use {account_meta::AccountMeta, solana_instruction_error as error};
24+
use {alloc::vec::Vec, solana_pubkey::Pubkey};
25+
2926
/// A directive for a single invocation of a Solana program.
3027
///
3128
/// An instruction specifies which program it is calling, which accounts it may
@@ -85,7 +82,6 @@ pub mod syscalls;
8582
/// Programs may require signatures from some accounts, in which case they
8683
/// should be specified as signers during `Instruction` construction. The
8784
/// program must still validate during execution that the account is a signer.
88-
#[cfg(feature = "std")]
8985
#[cfg_attr(
9086
feature = "serde",
9187
derive(serde_derive::Serialize, serde_derive::Deserialize)
@@ -101,7 +97,6 @@ pub struct Instruction {
10197
pub data: Vec<u8>,
10298
}
10399

104-
#[cfg(feature = "std")]
105100
impl Instruction {
106101
#[cfg(feature = "borsh")]
107102
/// Create a new instruction from a value, encoded with [`borsh`].
@@ -344,7 +339,6 @@ pub struct BorrowedAccountMeta<'a> {
344339
///
345340
/// This struct is used by the runtime when constructing the instructions sysvar. It is not
346341
/// useful to Solana programs.
347-
#[cfg(feature = "std")]
348342
pub struct BorrowedInstruction<'a> {
349343
pub program_id: &'a Pubkey,
350344
pub accounts: Vec<BorrowedAccountMeta<'a>>,

instruction/src/syscalls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub fn get_processed_sibling_instruction(index: usize) -> Option<Instruction> {
5959
&mut account_meta as *mut _ as *mut u8,
6060
)
6161
} {
62-
let mut data = std::vec::Vec::new();
63-
let mut accounts = std::vec::Vec::new();
62+
let mut data = alloc::vec::Vec::new();
63+
let mut accounts = alloc::vec::Vec::new();
6464
data.resize_with(meta.data_len as usize, u8::default);
6565
accounts.resize_with(meta.accounts_len as usize, AccountMeta::default);
6666

scripts/check-no-std.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ no_std_crates=(
4343
# These are only checked in the alloc+core pass, not the core-only pass.
4444
no_std_alloc_crates=(
4545
-p solana-account-info
46+
-p solana-instruction
4647
)
4748

4849
# Use the upstream BPF target, which doesn't support std, to make sure that our

0 commit comments

Comments
 (0)