From 98cbe1251155177dc69c208205bef11cc482ee49 Mon Sep 17 00:00:00 2001 From: brooks Date: Thu, 9 Feb 2023 15:22:04 -0500 Subject: [PATCH] fixups --- program-test/src/lib.rs | 1 - sdk/program/src/program.rs | 1 - sdk/program/src/stable_layout.rs | 5 ++++- .../src/stable_layout/stable_instruction.rs | 21 ++++++++++++++++++- sdk/program/src/stable_layout/stable_vec.rs | 4 ++-- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index 526dfa39babddd..99c32decb590da 100644 --- a/program-test/src/lib.rs +++ b/program-test/src/lib.rs @@ -228,7 +228,6 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs { account_infos: &[AccountInfo], signers_seeds: &[&[&[u8]]], ) -> ProgramResult { - // bprumo TODO: can I do anything to remove this clone? let instruction = StableInstruction::from(instruction.clone()); let invoke_context = get_invoke_context(); let log_collector = invoke_context.get_log_collector(); diff --git a/sdk/program/src/program.rs b/sdk/program/src/program.rs index 9a18b4df38a729..a41f7b52678817 100644 --- a/sdk/program/src/program.rs +++ b/sdk/program/src/program.rs @@ -293,7 +293,6 @@ pub fn invoke_signed_unchecked( ) -> ProgramResult { #[cfg(target_os = "solana")] { - // bprumo TODO: it would be nice to not clone `instruction`... Can this fn take a StableInstruction instead? let instruction = StableInstruction::from(instruction.clone()); let result = unsafe { crate::syscalls::sol_invoke_signed_rust( diff --git a/sdk/program/src/stable_layout.rs b/sdk/program/src/stable_layout.rs index 39fd26da34e360..37d3d8bf20ccb5 100644 --- a/sdk/program/src/stable_layout.rs +++ b/sdk/program/src/stable_layout.rs @@ -1,4 +1,7 @@ -//! bprumo TODO: doc, or hide doc? +#![doc(hidden)] +//! Types with stable memory layouts +//! +//! Internal use only; here be dragons! pub mod stable_instruction; pub mod stable_rc; diff --git a/sdk/program/src/stable_layout/stable_instruction.rs b/sdk/program/src/stable_layout/stable_instruction.rs index 108199305eceef..a54fadc4783256 100644 --- a/sdk/program/src/stable_layout/stable_instruction.rs +++ b/sdk/program/src/stable_layout/stable_instruction.rs @@ -1,4 +1,4 @@ -//! Instruction, with a stable memory layout +//! `Instruction`, with a stable memory layout use { crate::{ @@ -9,6 +9,25 @@ use { std::fmt::Debug, }; +/// `Instruction`, with a stable memory layout +/// +/// This is used within the runtime to ensure memory mapping and memory accesses are valid. We +/// rely on known addresses and offsets within the runtime, and since `Instruction`'s layout is +/// allowed to change, we must provide a way to lock down the memory layout. `StableInstruction` +/// reimplements the bare minimum of `Instruction`'s API sufficient only for the runtime's needs. +/// +/// # Examples +/// +/// Creating a `StableInstruction` from an `Instruction` +/// +/// ``` +/// # use solana_program::{instruction::{AccountMeta, Instruction}, pubkey::Pubkey, stable_layout::stable_instruction::StableInstruction}; +/// # let program_id = Pubkey::default(); +/// # let accounts = Vec::default(); +/// # let data = Vec::default(); +/// let instruction = Instruction { program_id, accounts, data }; +/// let instruction = StableInstruction::from(instruction); +/// ``` #[derive(Debug, PartialEq)] #[repr(C)] pub struct StableInstruction { diff --git a/sdk/program/src/stable_layout/stable_vec.rs b/sdk/program/src/stable_layout/stable_vec.rs index ec29bafe9d9e1c..658453f864cb44 100644 --- a/sdk/program/src/stable_layout/stable_vec.rs +++ b/sdk/program/src/stable_layout/stable_vec.rs @@ -5,8 +5,8 @@ use std::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull}; /// `Vec`, with a stable memory layout /// /// This container is used within the runtime to ensure memory mapping and memory accesses are -/// valid. We rely on known addresses and offsets within the runtime, and since `Vec`'s layout is -/// allowed to change, we must provide a way to lock down the memory layout. `StableVec` +/// valid. We rely on known addresses and offsets within the runtime, and since `Vec`'s layout +/// is allowed to change, we must provide a way to lock down the memory layout. `StableVec` /// reimplements the bare minimum of `Vec`'s API sufficient only for the runtime's needs. /// /// To ensure memory allocation and deallocation is handled correctly, it is only possible to