diff --git a/Cargo.lock b/Cargo.lock index d463a2a4ba..386b3deb39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4882,6 +4882,7 @@ dependencies = [ "memmap2 0.5.3", "rustc_version 0.4.0", "serde", + "serde_bytes", "serde_derive", "sha2 0.10.2", "solana-frozen-abi-macro 1.10.4", diff --git a/frozen-abi/Cargo.toml b/frozen-abi/Cargo.toml index f165be5615..6959480653 100644 --- a/frozen-abi/Cargo.toml +++ b/frozen-abi/Cargo.toml @@ -16,6 +16,7 @@ lazy_static = "1.4.0" log = "0.4.14" serde = "1.0.136" serde_derive = "1.0.103" +serde_bytes = "0.11" sha2 = "0.10.2" solana-frozen-abi-macro = { path = "macro", version = "=1.10.4" } thiserror = "1.0" diff --git a/frozen-abi/src/abi_digester.rs b/frozen-abi/src/abi_digester.rs index 5a041321f4..66047f2feb 100644 --- a/frozen-abi/src/abi_digester.rs +++ b/frozen-abi/src/abi_digester.rs @@ -562,6 +562,17 @@ mod tests { #[derive(Serialize, AbiExample)] struct TestNewtypeStruct(i8); + #[frozen_abi(digest = "Hbs1X2X7TF2gFEfsspwfZ1JKr8ZGbLY3uidQBebqcMYt")] + #[derive(Serialize, AbiExample)] + struct Foo<'a> { + #[serde(with = "serde_bytes")] + data1: Vec, + #[serde(with = "serde_bytes")] + data2: &'a [u8], + #[serde(with = "serde_bytes")] + data3: &'a Vec, + } + #[frozen_abi(digest = "5qio5qYurHDv6fq5kcwP2ue2RBEazSZF8CPk2kUuwC2j")] #[derive(Serialize, AbiExample)] struct TestStructReversed { diff --git a/frozen-abi/src/abi_example.rs b/frozen-abi/src/abi_example.rs index ae8b48ddd6..43d721535c 100644 --- a/frozen-abi/src/abi_example.rs +++ b/frozen-abi/src/abi_example.rs @@ -415,6 +415,13 @@ impl AbiExample for &Vec { } } +impl AbiExample for &[u8] { + fn example() -> Self { + info!("AbiExample for (&[u8]): {}", type_name::()); + &VEC_U8[..] + } +} + impl AbiExample for VecDeque { fn example() -> Self { info!("AbiExample for (Vec): {}", type_name::()); diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index ff8f4fd544..a16d050501 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -3370,6 +3370,7 @@ dependencies = [ "memmap2 0.5.3", "rustc_version 0.4.0", "serde", + "serde_bytes", "serde_derive", "sha2 0.10.2", "solana-frozen-abi-macro 1.10.4", diff --git a/sdk/src/account.rs b/sdk/src/account.rs index 65c03814bd..a480cfa0a6 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -46,8 +46,8 @@ mod account_serialize { struct Account<'a> { lamports: u64, #[serde(with = "serde_bytes")] - // a ref so we don't have to make a copy just to serialize this - data: &'a Vec, + // a slice so we don't have to make a copy just to serialize this + data: &'a [u8], // can't be &pubkey because abi example doesn't support it owner: Pubkey, executable: bool, @@ -57,7 +57,6 @@ mod account_serialize { /// allows us to implement serialize on AccountSharedData that is equivalent to Account::serialize without making a copy of the Vec pub fn serialize_account( account: &(impl ReadableAccount + Serialize), - data: &Vec, serializer: S, ) -> Result where @@ -65,8 +64,7 @@ mod account_serialize { { let temp = Account { lamports: account.lamports(), - // note this is a ref, which is the whole point of 'account_serialize' - data, + data: account.data(), owner: *account.owner(), executable: account.executable(), rent_epoch: account.rent_epoch(), @@ -80,7 +78,7 @@ impl Serialize for Account { where S: Serializer, { - crate::account::account_serialize::serialize_account(self, &self.data, serializer) + crate::account::account_serialize::serialize_account(self, serializer) } } @@ -89,7 +87,7 @@ impl Serialize for AccountSharedData { where S: Serializer, { - crate::account::account_serialize::serialize_account(self, &self.data, serializer) + crate::account::account_serialize::serialize_account(self, serializer) } }