Skip to content

Commit

Permalink
sdk: Implement Borsh 0.9 traits on Pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Jul 17, 2023
1 parent a2d9870 commit 3f202fa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust-version = "1.68.0" # solana platform-tools rust version
[dependencies]
bincode = { workspace = true }
blake3 = { workspace = true, features = ["digest", "traits-preview"] }
borsh0-9 = { package = "borsh", version = "0.9.3" }
borsh = { workspace = true }
bs58 = { workspace = true }
bv = { workspace = true, features = ["serde"] }
Expand Down
42 changes: 42 additions & 0 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,48 @@ impl fmt::Display for Pubkey {
}
}

impl borsh0_9::de::BorshDeserialize for Pubkey {
fn deserialize(buf: &mut &[u8]) -> ::core::result::Result<Self, borsh0_9::maybestd::io::Error> {
Ok(Self(borsh0_9::BorshDeserialize::deserialize(buf)?))
}
}
impl borsh0_9::BorshSchema for Pubkey
where
[u8; 32]: borsh0_9::BorshSchema,
{
fn declaration() -> borsh0_9::schema::Declaration {
"Pubkey".to_string()
}
fn add_definitions_recursively(
definitions: &mut borsh0_9::maybestd::collections::HashMap<
borsh0_9::schema::Declaration,
borsh0_9::schema::Definition,
>,
) {
let fields = borsh0_9::schema::Fields::UnnamedFields(<[_]>::into_vec(
borsh0_9::maybestd::boxed::Box::new([
<[u8; 32] as borsh0_9::BorshSchema>::declaration(),
]),
));
let definition = borsh0_9::schema::Definition::Struct { fields };
<Self as borsh0_9::BorshSchema>::add_definition(
<Self as borsh0_9::BorshSchema>::declaration(),
definition,
definitions,
);
<[u8; 32] as borsh0_9::BorshSchema>::add_definitions_recursively(definitions);
}
}
impl borsh0_9::ser::BorshSerialize for Pubkey {
fn serialize<W: borsh0_9::maybestd::io::Write>(
&self,
writer: &mut W,
) -> ::core::result::Result<(), borsh0_9::maybestd::io::Error> {
borsh0_9::BorshSerialize::serialize(&self.0, writer)?;
Ok(())
}
}

#[cfg(test)]
mod tests {
use {super::*, std::str::from_utf8};
Expand Down

0 comments on commit 3f202fa

Please sign in to comment.