serde

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

天涯浪子 提交于 2020-08-27 06:43:54
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

耗尽温柔 提交于 2020-08-27 06:42:31
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be

非 Y 不嫁゛ 提交于 2020-08-27 06:42:02
问题 My type A , which can contain anything that implements trait Trait , is serialisable, although the type implementing the trait Trait might not be. In my case, it cannot be - it's a private asymmetric key: extern crate serde; #[macro_use] extern crate serde_derive; use serde::de::DeserializeOwned; use serde::Serialize; trait Trait { type SerialisableType: Clone + Serialize + DeserializeOwned; fn inner(&self) -> &Self::SerialisableType; } #[derive(Serialize, Deserialize)] enum A<T: Trait> {

How to use a custom serde deserializer for chrono timestamps?

六眼飞鱼酱① 提交于 2020-07-30 05:58:07
问题 I'm trying to parse JSON into a struct which has a chrono::DateTime field. The JSON has the timestamps saved in a custom format that I wrote a deserializer for. How do I connect the two and get it working using #[serde(deserialize_with)] ? I'm using NaiveDateTime for simpler code extern crate serde; extern crate serde_json; use serde::Deserialize; extern crate chrono; use chrono::NaiveDateTime; fn from_timestamp(time: &String) -> NaiveDateTime { NaiveDateTime::parse_from_str(time, "%Y-%m-%dT

How do I use Serde to (de)serialize arrays greater than 32 elements, such as [u8; 128]?

不羁的心 提交于 2020-07-03 05:55:07
问题 I have a struct containing a byte array that I would like to serialize and deserialize to and from binary, but it only works for arrays up to 32 elements. Here is my minimal example code main.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