Skip to content

Commit e2f3351

Browse files
committed
client-rust: use a normal vector for ConfigKeys
1 parent f7f19f6 commit e2f3351

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

clients/rust/src/hooked/short_vec.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use serde::{Deserialize, Deserializer, Serialize, Serializer};
33
use {
44
borsh::{BorshDeserialize, BorshSerialize},
5-
solana_program::pubkey::Pubkey,
5+
solana_program::{pubkey::Pubkey, short_vec},
66
};
77

88
struct ShortU16(u16);
@@ -79,7 +79,7 @@ impl<T: Serialize> Serialize for ShortVec<T> {
7979
where
8080
S: Serializer,
8181
{
82-
solana_program::short_vec::serialize(&self.0, serializer)
82+
short_vec::serialize(&self.0, serializer)
8383
}
8484
}
8585

@@ -89,16 +89,31 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for ShortVec<T> {
8989
where
9090
D: Deserializer<'de>,
9191
{
92-
solana_program::short_vec::deserialize(deserializer).map(ShortVec)
92+
short_vec::deserialize(deserializer).map(ShortVec)
9393
}
9494
}
9595

9696
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
97-
#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Eq, PartialEq)]
97+
#[derive(Clone, Debug, Eq, PartialEq)]
9898
pub struct ConfigKeys {
9999
/// Each key tuple comprises a unique `Pubkey` identifier,
100100
/// and `bool` whether that key is a signer of the data.
101-
pub keys: ShortVec<(Pubkey, bool)>,
101+
#[serde(with = "short_vec")]
102+
pub keys: Vec<(Pubkey, bool)>,
103+
}
104+
105+
impl BorshSerialize for ConfigKeys {
106+
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
107+
BorshSerialize::serialize(&self.keys, writer)
108+
}
109+
}
110+
111+
impl BorshDeserialize for ConfigKeys {
112+
fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
113+
Ok(ConfigKeys {
114+
keys: ShortVec::deserialize_reader(reader)?.0,
115+
})
116+
}
102117
}
103118

104119
/// Utility for extracting the `ConfigKeys` data from the account data.
@@ -115,7 +130,7 @@ mod tests {
115130
use {super::*, assert_matches::assert_matches, bincode::serialize};
116131
use {
117132
bincode::deserialize,
118-
solana_program::short_vec::{decode_shortu16_len, ShortU16},
133+
short_vec::{decode_shortu16_len, ShortU16},
119134
};
120135

121136
/// Return the serialized length.

clients/rust/src/instructions_bincode.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Program instruction helpers.
22
33
use {
4-
crate::{ConfigKeys, ShortVec, ID},
4+
crate::{ConfigKeys, ID},
55
bincode::serialized_size,
66
solana_program::{
77
instruction::{AccountMeta, Instruction},
@@ -18,12 +18,7 @@ pub trait ConfigState: serde::Serialize + Default {
1818

1919
fn initialize_account<T: ConfigState>(config_pubkey: &Pubkey) -> Instruction {
2020
let account_metas = vec![AccountMeta::new(*config_pubkey, true)];
21-
let account_data = (
22-
ConfigKeys {
23-
keys: ShortVec(vec![]),
24-
},
25-
T::default(),
26-
);
21+
let account_data = (ConfigKeys { keys: vec![] }, T::default());
2722
Instruction::new_with_bincode(ID, &account_data, account_metas)
2823
}
2924

@@ -34,12 +29,7 @@ pub fn create_account<T: ConfigState>(
3429
lamports: u64,
3530
keys: Vec<(Pubkey, bool)>,
3631
) -> Vec<Instruction> {
37-
let space = T::max_space().saturating_add(
38-
serialized_size(&ConfigKeys {
39-
keys: ShortVec(keys),
40-
})
41-
.unwrap(),
42-
);
32+
let space = T::max_space().saturating_add(serialized_size(&ConfigKeys { keys }).unwrap());
4333
vec![
4434
system_instruction::create_account(
4535
from_account_pubkey,
@@ -65,11 +55,6 @@ pub fn store<T: ConfigState>(
6555
account_metas.push(AccountMeta::new(*signer_pubkey, true));
6656
}
6757
}
68-
let account_data = (
69-
ConfigKeys {
70-
keys: ShortVec(keys),
71-
},
72-
data,
73-
);
58+
let account_data = (ConfigKeys { keys }, data);
7459
Instruction::new_with_bincode(ID, &account_data, account_metas)
7560
}

0 commit comments

Comments
 (0)