gpt4 book ai didi

generics - Rust 和 serde 使用泛型反序列化

转载 作者:行者123 更新时间:2023-11-29 08:05:53 53 4
gpt4 key购买 nike

我正在尝试使用泛型从文件中反序列化结构,以便与 Swagger 生成的 API 一起使用。所以我把这个几乎可以工作的东西拼凑在一起,但是我无法从“拥有”指针中解压外部结构对象,正如您在测试中看到的那样。

这可能是错误的策略,但问题是我有各种 yaml 文件,我想读入这些文件并反序列化以提示要反序列化为的正确结构。我不想为每个 Struct 实现一个“readfile”函数,因为有很多。所以我试图使这个通用库工作,它应该反序列化为正确的结构,并与 Swagger API 一起使用。

它非常接近工作,但我似乎无法打开 Outer<ExternalStructA>进入刚刚ExternalStructA .

Owned(ExternalStructA { x: 1, y: 2 })
Owned(ExternalStructB { a: 1, b: 2 })

lib.rs :

#[cfg(test)]
mod tests {
use crate::generics_yaml_deserializer::Outer;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalStructA {
x: u32,
y: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalStructB {
a: u64,
b: u64,
}

#[test]
fn deserialize() {
let a = r#"---
ptr:
x: 1
y: 2
"#;

let b = r#"---
ptr:
a: 1
b: 2
"#;

let resulta: Outer<ExternalStructA> = serde_yaml::from_str(a).unwrap();
assert_eq!(1, resulta.ptr.x); // I can't seem to get into ptr ExternalStructA
let resultb: Outer<ExternalStructB> = serde_yaml::from_str(b).unwrap();
assert_eq!(1, resultb.ptr.a); // I can't seem to get into ptr ExternalStructB
}
}

mod generics_yaml_deserializer {
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::error::Error;

// empty holding struct which owns a owned ptr
#[derive(Deserialize, Debug)]
pub struct Outer<'a, T: 'a + ?Sized> {
#[serde(bound(deserialize = "Ptr<'a, T>: Deserialize<'de>"))]
pub ptr: Ptr<'a, T>,
}

#[derive(Debug)]
pub enum Ptr<'a, T: 'a + ?Sized> {
Ref(&'a T),
Owned(Box<T>),
}

impl<'de, 'a, T: 'a + ?Sized> Deserialize<'de> for Ptr<'a, T>
where
Box<T>: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(deserializer).map(Ptr::Owned)
}
}
}

cargo 依赖:

serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
serde_yaml = "0.7.5"
serde_json = "1.0"

更新:

我已经部分成功地推出了 Struct:

let resulta: Outer<ExternalStructA> = serde_yaml::from_str(a).unwrap();
match resulta.ptr {
Ptr::Owned(e) => {assert_eq!(1, e.x);},
Ptr::Ref(e) => {println!("error")},
Ptr::Owned(_) => {println!("error")}
};
}

但是当我尝试使用通用类型将其实现为一个函数时,我遇到了很多错误,主要是:

the trait `for<'de> tests::_IMPL_DESERIALIZE_FOR_ExternalStructA::_serde::Deserialize<'de>` is not implemented for `T`

非工作代码添加到 mod generics_yaml_deserializer

fn readfile<T>(filename: String) -> Result<Box<T>, Box<std::error::Error>> {
let f = std::fs::File::open(filename)?;
let config_data: Outer<T> = serde_yaml::from_reader(f)?;
Ok(Box::new(config_data))
}

fn readconfig<T>(filename: String) -> Result<Box<T>, &'static str> {
// read the config file
let config_data = readfile(filename);
match config_data {
Ok(e) => {
Ok(Box::new(e))
},
Err(_) => {
Err("nadda")
}
}
}

最佳答案

只需声明TDeserializeOwned:

fn readfile<T: de::DeserializeOwned>(filename: String) -> Result<Box<T>, Box<std::error::Error>> {
let f = std::fs::File::open(filename)?;
let config_data: Outer<T> = serde_yaml::from_reader(f)?;
match config_data.ptr {
Ptr::Owned(data) => Ok(data),
_ => unimplemented!(),
}
}

readconfig相同

关于generics - Rust 和 serde 使用泛型反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54851996/

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