Skip to content

Commit 6f5d6be

Browse files
committed
Add COption pack/unpack helpers
1 parent ec82c2e commit 6f5d6be

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

interface/src/instruction.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,27 @@ impl<'a> TokenInstruction<'a> {
10881088
}
10891089
}
10901090

1091+
pub(crate) fn unpack_u64_option(input: &[u8]) -> Result<(COption<u64>, &[u8]), ProgramError> {
1092+
match input.split_first() {
1093+
Option::Some((&0, rest)) => Ok((COption::None, rest)),
1094+
Option::Some((&1, rest)) => {
1095+
let (value, rest) = Self::unpack_u64(rest)?;
1096+
Ok((COption::Some(value), rest))
1097+
}
1098+
_ => Err(TokenError::InvalidInstruction.into()),
1099+
}
1100+
}
1101+
1102+
pub(crate) fn pack_u64_option(value: &COption<u64>, buf: &mut Vec<u8>) {
1103+
match *value {
1104+
COption::Some(ref amount) => {
1105+
buf.push(1);
1106+
buf.extend_from_slice(&amount.to_le_bytes());
1107+
}
1108+
COption::None => buf.push(0),
1109+
}
1110+
}
1111+
10911112
pub(crate) fn unpack_u16(input: &[u8]) -> Result<(u16, &[u8]), ProgramError> {
10921113
let value = input
10931114
.get(..U16_BYTES)

0 commit comments

Comments
 (0)