Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions clients/rust/src/hooked/remainder_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl BorshSerialize for RemainderOptionBytes {
Self: Sized,
{
match &self {
Self::Some(item) => item.serialize(writer),
Self::Some(item) => writer.write_all(item),
Self::None => Ok(()),
}
}
Expand All @@ -137,9 +137,12 @@ impl BorshDeserialize for RemainderOptionBytes {
where
Self: Sized,
{
match Vec::<u8>::deserialize_reader(reader) {
Ok(item) => Ok(RemainderOptionBytes::Some(item)),
Err(_) => Ok(RemainderOptionBytes::None),
let mut item = Vec::new();
reader.read_to_end(&mut item)?;
if item.is_empty() {
Ok(RemainderOptionBytes::None)
} else {
Ok(RemainderOptionBytes::Some(item))
}
}
}
Expand All @@ -152,3 +155,61 @@ impl From<Option<Vec<u8>>> for RemainderOptionBytes {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::instructions::WriteInstructionArgs;

#[test]
fn remainder_option_bytes_some_is_serialized_without_prefix() {
let bytes = borsh::to_vec(&RemainderOptionBytes::Some(vec![1, 2, 3])).unwrap();
assert_eq!(bytes, vec![1, 2, 3]);
}

#[test]
fn remainder_option_bytes_none_is_serialized_as_empty() {
let bytes = borsh::to_vec(&RemainderOptionBytes::None).unwrap();
assert!(bytes.is_empty());
}

#[test]
fn remainder_option_bytes_deserializes_remaining_bytes() {
assert_eq!(
borsh::from_slice::<RemainderOptionBytes>(&[1, 2, 3]).unwrap(),
RemainderOptionBytes::Some(vec![1, 2, 3])
);
assert_eq!(
borsh::from_slice::<RemainderOptionBytes>(&[]).unwrap(),
RemainderOptionBytes::None
);
}

#[test]
fn remainder_option_bytes_round_trip() {
for value in [
RemainderOptionBytes::Some(vec![3, 0, 0, 0, 255]),
RemainderOptionBytes::None,
] {
let bytes = borsh::to_vec(&value).unwrap();
assert_eq!(
borsh::from_slice::<RemainderOptionBytes>(&bytes).unwrap(),
value
);
}
}

#[test]
fn write_instruction_args_append_data_after_offset() {
let args = WriteInstructionArgs {
offset: 7,
data: RemainderOptionBytes::Some(vec![1, 2, 3]),
};
let bytes = borsh::to_vec(&args).unwrap();
assert_eq!(bytes, vec![7, 0, 0, 0, 1, 2, 3]);
assert_eq!(
borsh::from_slice::<WriteInstructionArgs>(&bytes).unwrap(),
args
);
}
}
Loading