-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Add structs with stable layouts for types that are passed to run…
…time
- Loading branch information
1 parent
10dde65
commit edd379b
Showing
16 changed files
with
396 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! bprumo TODO: doc, or hide doc? | ||
pub mod stable_instruction; | ||
pub mod stable_rc; | ||
pub mod stable_ref_cell; | ||
pub mod stable_slice; | ||
pub mod stable_vec; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! Instruction, with a stable memory layout | ||
use { | ||
crate::{ | ||
instruction::{AccountMeta, Instruction}, | ||
pubkey::Pubkey, | ||
stable_layout::stable_vec::StableVec, | ||
}, | ||
std::fmt::Debug, | ||
}; | ||
|
||
#[derive(Debug)] | ||
#[repr(C)] | ||
pub struct StableInstruction { | ||
pub accounts: StableVec<AccountMeta>, | ||
pub data: StableVec<u8>, | ||
pub program_id: Pubkey, | ||
} | ||
|
||
impl From<Instruction> for StableInstruction { | ||
fn from(other: Instruction) -> Self { | ||
Self { | ||
accounts: StableVec::from(other.accounts), | ||
data: StableVec::from(other.data), | ||
program_id: other.program_id, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use { | ||
super::*, | ||
memoffset::offset_of, | ||
std::mem::{align_of, size_of}, | ||
}; | ||
|
||
#[test] | ||
fn test_memory_layout() { | ||
assert_eq!(offset_of!(StableInstruction, accounts), 0); | ||
assert_eq!(offset_of!(StableInstruction, data), 24); | ||
assert_eq!(offset_of!(StableInstruction, program_id), 48); | ||
assert_eq!(align_of::<StableInstruction>(), 8); | ||
assert_eq!(size_of::<StableInstruction>(), 24 + 24 + 32); | ||
|
||
let program_id = Pubkey::new_unique(); | ||
let account_meta1 = AccountMeta { | ||
pubkey: Pubkey::new_unique(), | ||
is_signer: true, | ||
is_writable: false, | ||
}; | ||
let account_meta2 = AccountMeta { | ||
pubkey: Pubkey::new_unique(), | ||
is_signer: false, | ||
is_writable: true, | ||
}; | ||
let accounts = vec![account_meta1, account_meta2]; | ||
let data = vec![1, 2, 3, 4, 5]; | ||
let instruction = Instruction { | ||
program_id, | ||
accounts: accounts.clone(), | ||
data: data.clone(), | ||
}; | ||
let instruction = StableInstruction::from(instruction); | ||
|
||
let instruction_addr = &instruction as *const _ as u64; | ||
|
||
let accounts_ptr = instruction_addr as *const &[AccountMeta; 2]; | ||
assert_eq!(unsafe { *accounts_ptr }, accounts.as_slice()); | ||
|
||
let data_ptr = (instruction_addr + 24) as *const &[u8; 5]; | ||
assert_eq!(unsafe { *data_ptr }, data.as_slice()); | ||
|
||
let pubkey_ptr = (instruction_addr + 48) as *const Pubkey; | ||
assert_eq!(unsafe { *pubkey_ptr }, program_id); | ||
} | ||
} |
Oops, something went wrong.