gpt4 book ai didi

arrays - 我如何使用 Serde 来(反)序列化大于 32 个元素的数组,例如 [u8; 128]?

转载 作者:行者123 更新时间:2023-11-29 07:43:20 24 4
gpt4 key购买 nike

我有一个包含字节数组的结构,我想将其序列化和反序列化为二进制,但它仅适用于数组 up to 32 elements .

这是我的最小示例代码

ma​​in.rs:

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate bincode;

use bincode::{serialize, deserialize, Infinite};

const BYTECOUNT: usize = 32; // 33 and more does not work, I need 128
type DataArr = [u8; BYTECOUNT];

#[derive(Serialize, Deserialize, Debug)]
struct Entry {
number: i64,
data: DataArr
}

fn main() {
let mut my_entry = Entry { number: 12345, data: [0; BYTECOUNT] };
my_entry.data[4] = 42;

// Convert the Entry to binary.
let serialized: Vec<u8> = serialize(&my_entry, Infinite).unwrap();
println!("serialized = {:?}", serialized);

// Convert the binary representation back to an Entry.
let deserialized: Entry = deserialize(&serialized).unwrap();
println!("deserialized = {:?}", deserialized);
}

Cargo.toml:

[package]
name = "array_serialization_test"
version = "0.1.0"

[dependencies]
serde = "*"
serde_derive = "*"
bincode = "*"

输出:

serialized = [57, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
deserialized = Entry { number: 12345, data: [0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

如何让它对数组中的 128 个元素起作用?我可以在我的用户代码中以某种方式手动扩展 array_impls! 吗?有替代方法吗?

我认为这个问题不同于How do I map a C struct with padding over 32 bytes using serde and bincode?因为我实际上需要数组的内容,因为它不只是用于填充。另外我想知道我是否可以在我的代码上扩展 array_impls!

最佳答案

目前,Serde 无法提供适用于每个 数组大小的SerializeDeserialize impls。这在 const generics 上被阻止正在开发中,有望在 2018 年晚些时候登陆。

现在您可以定义自己的“大数组”助手,它可以序列化和反序列化您的箱子中使用的任何特定大小的数组。您想要使用大数组助手的字段需要使用 #[serde(with = "BigArray")] 标记,否则 Serde 将查找不存在的 SerializeDeserialize 实现。

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate bincode;

mod big_array;
use big_array::BigArray;

const BYTECOUNT: usize = 128;
type DataArr = [u8; BYTECOUNT];

#[derive(Serialize, Deserialize)]
struct Entry {
number: i64,
#[serde(with = "BigArray")]
data: DataArr
}

fn main() {
let mut my_entry = Entry { number: 12345, data: [0; BYTECOUNT] };
my_entry.data[4] = 42;

// Convert the Entry to binary.
let serialized: Vec<u8> = bincode::serialize(&my_entry).unwrap();
println!("serialized = {:?}", serialized);

// Convert the binary representation back to an Entry.
let deserialized: Entry = bincode::deserialize(&serialized).unwrap();
println!("deserialized = {} {:?}", deserialized.number, &deserialized.data[..]);
}

大数组助手可以在 src/big_array.rs 中定义如下。如果您想拥有它,也许它本身就是一个很好的 crate !

use std::fmt;
use std::marker::PhantomData;
use serde::ser::{Serialize, Serializer, SerializeTuple};
use serde::de::{Deserialize, Deserializer, Visitor, SeqAccess, Error};

pub trait BigArray<'de>: Sized {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer;
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>;
}

macro_rules! big_array {
($($len:expr,)+) => {
$(
impl<'de, T> BigArray<'de> for [T; $len]
where T: Default + Copy + Serialize + Deserialize<'de>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
let mut seq = serializer.serialize_tuple(self.len())?;
for elem in &self[..] {
seq.serialize_element(elem)?;
}
seq.end()
}

fn deserialize<D>(deserializer: D) -> Result<[T; $len], D::Error>
where D: Deserializer<'de>
{
struct ArrayVisitor<T> {
element: PhantomData<T>,
}

impl<'de, T> Visitor<'de> for ArrayVisitor<T>
where T: Default + Copy + Deserialize<'de>
{
type Value = [T; $len];

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(concat!("an array of length ", $len))
}

fn visit_seq<A>(self, mut seq: A) -> Result<[T; $len], A::Error>
where A: SeqAccess<'de>
{
let mut arr = [T::default(); $len];
for i in 0..$len {
arr[i] = seq.next_element()?
.ok_or_else(|| Error::invalid_length(i, &self))?;
}
Ok(arr)
}
}

let visitor = ArrayVisitor { element: PhantomData };
deserializer.deserialize_tuple($len, visitor)
}
}
)+
}
}

big_array! {
40, 48, 50, 56, 64, 72, 96, 100, 128, 160, 192, 200, 224, 256, 384, 512,
768, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
}

关于arrays - 我如何使用 Serde 来(反)序列化大于 32 个元素的数组,例如 [u8; 128]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48782047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com