Files
neon/libs/utils/tests/bin_ser_test.rs
Arpad Müller a22be5af72 Migrate the last crates to edition 2024 (#10998)
Migrates the remaining crates to edition 2024. We like to stay on the
latest edition if possible. There is no functional changes, however some
code changes had to be done to accommodate the edition's breaking
changes.

Like the previous migration PRs, this is comprised of three commits:

* the first does the edition update and makes `cargo check`/`cargo
clippy` pass. we had to update bindgen to make its output [satisfy the
requirements of edition
2024](https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-extern.html)
* the second commit does a `cargo fmt` for the new style edition.
* the third commit reorders imports as a one-off change. As before, it
is entirely optional.

Part of #10918
2025-02-27 09:40:40 +00:00

43 lines
1.0 KiB
Rust

use std::io::Read;
use bytes::{Buf, BytesMut};
use hex_literal::hex;
use serde::Deserialize;
use utils::bin_ser::LeSer;
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct HeaderData {
magic: u16,
info: u16,
tli: u32,
pageaddr: u64,
len: u32,
}
// A manual implementation using BytesMut, just so we can
// verify that we decode the same way.
pub fn decode_header_data(buf: &mut BytesMut) -> HeaderData {
HeaderData {
magic: buf.get_u16_le(),
info: buf.get_u16_le(),
tli: buf.get_u32_le(),
pageaddr: buf.get_u64_le(),
len: buf.get_u32_le(),
}
}
pub fn decode2<R: Read>(reader: &mut R) -> HeaderData {
HeaderData::des_from(reader).unwrap()
}
#[test]
fn test1() {
let raw1 = hex!("8940 7890 5534 7890 1289 5379 8378 7893 4207 8923 4712 3218");
let mut buf1 = BytesMut::from(&raw1[..]);
let mut buf2 = &raw1[..];
let dec1 = decode_header_data(&mut buf1);
let dec2 = decode2(&mut buf2);
assert_eq!(dec1, dec2);
assert_eq!(buf1, buf2);
}