Skip to content

Commit b6d6be5

Browse files
committed
Add test for manual Encode/Decode impls
1 parent a25c0d8 commit b6d6be5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rust/bufferfish/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,4 +1084,41 @@ mod tests {
10841084
_ => panic!("Decoded complex type did not match expected variant"),
10851085
}
10861086
}
1087+
1088+
#[test]
1089+
fn test_manual_encode_decode_impl() {
1090+
use bufferfish_core as bufferfish;
1091+
use bufferfish_core::{Decodable, Encodable};
1092+
1093+
#[derive(Debug)]
1094+
struct CustomType {
1095+
value: u32,
1096+
}
1097+
1098+
impl bufferfish::Encodable for CustomType {
1099+
fn encode_value(
1100+
&self,
1101+
bf: &mut bufferfish::Bufferfish,
1102+
) -> Result<(), bufferfish::BufferfishError> {
1103+
bf.write_u32(self.value)
1104+
}
1105+
}
1106+
1107+
impl bufferfish::Decodable for CustomType {
1108+
fn decode_value(
1109+
bf: &mut bufferfish::Bufferfish,
1110+
) -> Result<Self, bufferfish::BufferfishError> {
1111+
let value = bf.read_u32()?;
1112+
Ok(CustomType { value })
1113+
}
1114+
}
1115+
1116+
let mut bf = bufferfish::Bufferfish::new();
1117+
let custom = CustomType { value: 42 };
1118+
custom.encode(&mut bf).unwrap();
1119+
assert_eq!(bf.as_ref(), &[0, 0, 0, 42]);
1120+
1121+
let decoded = CustomType::decode(&mut bf).unwrap();
1122+
assert_eq!(decoded.value, 42);
1123+
}
10871124
}

0 commit comments

Comments
 (0)