-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang: Add
InstructionData::write_to
implementation (#2733)
Co-authored-by: acheron <98934430+acheroncrypto@users.noreply.github.com> Co-authored-by: acheron <acheroncrypto@gmail.com>
- Loading branch information
1 parent
250fa8c
commit c2b5472
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
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,36 @@ | ||
use anchor_lang::{AnchorDeserialize, AnchorSerialize, Discriminator, InstructionData}; | ||
|
||
#[test] | ||
fn test_instruction_data() { | ||
// Define some test type and implement ser/de, discriminator, and ix data | ||
#[derive(Default, AnchorSerialize, AnchorDeserialize, PartialEq, Eq)] | ||
struct MyType { | ||
foo: [u8; 8], | ||
bar: String, | ||
} | ||
impl Discriminator for MyType { | ||
const DISCRIMINATOR: [u8; 8] = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
} | ||
impl InstructionData for MyType {} | ||
|
||
// Initialize some instance of the type | ||
let instance = MyType { | ||
foo: [0, 2, 4, 6, 8, 10, 12, 14], | ||
bar: "sharding sucks".into(), | ||
}; | ||
|
||
// Serialize using both methods | ||
let data = instance.data(); | ||
let mut write = vec![]; | ||
instance.write_to(&mut write); | ||
|
||
// Check that one is correct and that they are equal (implies other is correct) | ||
let correct_disc = data[0..8] == MyType::DISCRIMINATOR; | ||
let correct_data = MyType::deserialize(&mut &data[8..]).is_ok_and(|result| result == instance); | ||
let correct_serialization = correct_disc & correct_data; | ||
assert!(correct_serialization, "serialization was not correct"); | ||
assert_eq!( | ||
&data, &write, | ||
"the different methods produced different serialized representations" | ||
); | ||
} |
c2b5472
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
anchor-docs – ./
anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com