Skip to content

Commit 18976c6

Browse files
committed
Replace std for alloc
1 parent da237d4 commit 18976c6

File tree

9 files changed

+30
-33
lines changed

9 files changed

+30
-33
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,12 @@ pub fn process_instruction(
171171
By default, Pinocchio is a `no_std` crate. This means that it does not use any code from the
172172
standard (`std`) library. While this does not affect how Pinocchio is used, there is a one
173173
particular apparent difference. Helpers that need to allocate memory, such as fetching `SlotHashes`
174-
sysvar data, are not available. To enable these helpers, the `std` feature must be enabled when adding
175-
Pinocchio as a dependency:
174+
sysvar data, are not available. To enable these helpers, the `alloc` feature must be enabled when
175+
adding Pinocchio as a dependency:
176176
```
177-
pinocchio = { version = "0.10.0", features = ["std"] }
177+
pinocchio = { version = "0.10.0", features = ["alloc"] }
178178
```
179+
The `alloc` feature uses the `alloc` crate.
179180

180181
## Advance entrypoint configuration
181182

sdk/pinocchio/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ unexpected_cfgs = { level = "warn", check-cfg = [
1919

2020
[features]
2121
copy = ["solana-account-view/copy", "solana-address/copy"]
22-
std = ["solana-address/std"]
22+
alloc = []
2323

2424
[dependencies]
2525
solana-account-view = { workspace = true }

sdk/pinocchio/src/entrypoint/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod lazy;
55

66
pub use lazy::{InstructionContext, MaybeAccount};
77

8-
#[cfg(not(feature = "std"))]
8+
#[cfg(not(feature = "alloc"))]
99
use core::alloc::{GlobalAlloc, Layout};
1010

1111
#[cfg(target_os = "solana")]
@@ -459,8 +459,8 @@ macro_rules! default_panic_handler {
459459
/// This macro sets up a default panic handler that logs the location (file, line and column) where
460460
/// the panic occurred and then calls the syscall `abort()`.
461461
///
462-
/// This macro can only be used when all crates are `no_std` and the `"std"` feature is disabled.
463-
#[cfg(not(feature = "std"))]
462+
/// This macro can only be used when all crates are `no_std` and the `"alloc"` feature is disabled.
463+
#[cfg(not(feature = "alloc"))]
464464
#[macro_export]
465465
macro_rules! nostd_panic_handler {
466466
() => {
@@ -525,12 +525,12 @@ macro_rules! default_allocator {
525525

526526
/// A global allocator that does not allocate memory.
527527
///
528-
/// Using this macro with the `"std"` feature enabled will result in a compile error.
529-
#[cfg(feature = "std")]
528+
/// Using this macro with the `"alloc"` feature enabled will result in a compile error.
529+
#[cfg(feature = "alloc")]
530530
#[macro_export]
531531
macro_rules! no_allocator {
532532
() => {
533-
compile_error!("Feature 'std' cannot be enabled.");
533+
compile_error!("Feature 'alloc' cannot be enabled.");
534534
};
535535
}
536536

@@ -542,8 +542,8 @@ macro_rules! no_allocator {
542542
///
543543
/// The program will panic if it tries to dynamically allocate memory.
544544
///
545-
/// This is used when the `"std"` feature is disabled.
546-
#[cfg(not(feature = "std"))]
545+
/// This is used when the `"alloc"` feature is disabled.
546+
#[cfg(not(feature = "alloc"))]
547547
#[macro_export]
548548
macro_rules! no_allocator {
549549
() => {
@@ -653,13 +653,13 @@ mod alloc {
653653
}
654654
}
655655

656-
#[cfg(not(feature = "std"))]
656+
#[cfg(not(feature = "alloc"))]
657657
/// An allocator that does not allocate memory.
658658
#[cfg_attr(feature = "copy", derive(Copy))]
659659
#[derive(Clone, Debug)]
660660
pub struct NoAllocator;
661661

662-
#[cfg(not(feature = "std"))]
662+
#[cfg(not(feature = "alloc"))]
663663
unsafe impl GlobalAlloc for NoAllocator {
664664
#[inline]
665665
unsafe fn alloc(&self, _: Layout) -> *mut u8 {
@@ -674,13 +674,13 @@ unsafe impl GlobalAlloc for NoAllocator {
674674

675675
#[cfg(all(test, not(target_os = "solana")))]
676676
mod tests {
677-
extern crate std;
677+
extern crate alloc;
678678

679-
use core::{alloc::Layout, ptr::copy_nonoverlapping};
680-
use std::{
679+
use alloc::{
681680
alloc::{alloc, dealloc},
682681
vec,
683682
};
683+
use core::{alloc::Layout, ptr::copy_nonoverlapping};
684684

685685
use super::*;
686686

@@ -703,7 +703,7 @@ mod tests {
703703
unsafe {
704704
let ptr = alloc(layout);
705705
if ptr.is_null() {
706-
std::alloc::handle_alloc_error(layout);
706+
alloc::alloc::handle_alloc_error(layout);
707707
}
708708
AlignedMemory { ptr, layout }
709709
}

sdk/pinocchio/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@
166166
//! code from the standard (`std`) library. While this does not affect how Pinocchio
167167
//! is used, there is a one particular apparent difference. Helpers that need to
168168
//! allocate memory, such as fetching `SlotHashes` sysvar, are not available. To
169-
//! enable these helpers, the `std` feature must be enabled when adding Pinocchio
169+
//! enable these helpers, the `alloc` feature must be enabled when adding Pinocchio
170170
//! as a dependency:
171171
//! ```ignore
172-
//! pinocchio = { version = "0.10.0", features = ["std"] }
172+
//! pinocchio = { version = "0.10.0", features = ["alloc"] }
173173
//! ```
174174
//!
175175
//! ## Advanced entrypoint configuration
@@ -208,10 +208,10 @@
208208
//! cargo build-sbf --features bpf-entrypoint
209209
//! ```
210210
211-
#![no_std]
211+
#![cfg_attr(not(test), no_std)]
212212

213-
#[cfg(feature = "std")]
214-
extern crate std;
213+
#[cfg(feature = "alloc")]
214+
extern crate alloc;
215215

216216
pub mod entrypoint;
217217
pub mod sysvars;

sdk/pinocchio/src/sysvars/slot_hashes/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::{
2323
Address,
2424
};
2525

26-
#[cfg(feature = "std")]
27-
use std::boxed::Box;
26+
#[cfg(feature = "alloc")]
27+
use alloc::{boxed::Box, vec::Vec};
2828

2929
/// `SysvarS1otHashes111111111111111111111111111`
3030
pub const SLOTHASHES_ID: Address = Address::new_from_array([
@@ -299,7 +299,7 @@ impl<'a> SlotHashes<Ref<'a, [u8]>> {
299299
}
300300
}
301301

302-
#[cfg(feature = "std")]
302+
#[cfg(feature = "alloc")]
303303
impl SlotHashes<Box<[u8]>> {
304304
/// Fills the provided buffer with the full `SlotHashes` sysvar data.
305305
///
@@ -320,7 +320,7 @@ impl SlotHashes<Box<[u8]>> {
320320
/// Allocates an optimal buffer for the sysvar data based on available features.
321321
#[inline(always)]
322322
fn allocate_and_fetch() -> Result<Box<[u8]>, ProgramError> {
323-
let mut buf = std::vec::Vec::with_capacity(MAX_SIZE);
323+
let mut buf = Vec::with_capacity(MAX_SIZE);
324324
unsafe {
325325
// SAFETY: `buf` was allocated with capacity `MAX_SIZE` so its
326326
// pointer is valid for exactly that many bytes. `fill_from_sysvar`

sdk/pinocchio/src/sysvars/slot_hashes/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use core::{
99
ptr,
1010
};
1111

12-
extern crate std;
1312
use std::io::Write;
1413
use std::vec::Vec;
1514

@@ -460,7 +459,7 @@ fn test_from_account_info_constructor() {
460459
/// `SlotHashes` getters to make sure the view itself works. We do not verify
461460
/// that the syscall populated real on-chain bytes, as doing so requires an
462461
/// environment outside the scope of host `cargo test`.
463-
#[cfg(feature = "std")]
462+
#[cfg(feature = "alloc")]
464463
#[test]
465464
fn test_fetch_allocates_buffer_host() {
466465
const START_SLOT: u64 = 500;

sdk/pinocchio/src/sysvars/slot_hashes/test_edge.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::{error::ProgramError, sysvars::slot_hashes::*};
2-
extern crate std;
31
use super::test_utils::{build_slot_hashes_bytes as raw_slot_hashes, make_account_info};
2+
use crate::{error::ProgramError, sysvars::slot_hashes::*};
43

54
#[test]
65
fn test_wrong_key_from_account_view() {

sdk/pinocchio/src/sysvars/slot_hashes/test_raw.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use super::raw;
44
use super::*;
5-
extern crate std;
65

76
#[test]
87
fn test_validate_buffer_size() {

sdk/pinocchio/src/sysvars/slot_hashes/test_utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! freely while production code remains `#![no_std]`.
44
55
use super::*;
6-
extern crate std;
76
use crate::account::{Account, AccountView};
87
use core::ptr;
98
use std::vec::Vec;

0 commit comments

Comments
 (0)