-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to serialize and Deserialize closure? #24
Comments
Hi @TrionProg, thanks for filing an issue! I think the problem is that the closure here needs to be a This should work: let plus_one = Fn!(move |x: usize| x + one + two);
Boxing the closure's trait object, i.e. struct MultipleClosures {
a: Box<dyn st::Fn(usize) -> usize>,
b: Box<dyn st::Fn(usize) -> usize>,
} |
Hello. Unfortunately I need use 2 boxes in the struct, + 1 box for this struct. Later, I hope, I will try to optimize it to MultipleClosures<F1,F2>, this should be simmilar to error_impl in failure library:
Thank you for your fantastic library! |
Hello. Ohh, not all is right. I can not show the code -- it has a lot of garbage and I can not understand anything that happens -- All works, but then I implement Trait, I have a lot of problems like Self: traitobject::Sealed is not satisfied. Can you give me an example how to: |
Okey. I am making experiment. Minimal example of same error. #![feature(unboxed_closures)]
extern crate serde;
extern crate serde_traitobject;
#[macro_use]
extern crate serde_closure;
#[macro_use]
extern crate serde_derive;
use serde_traitobject::FnOnce as FnOnceTO;
use serde_traitobject::Box as BoxTO;
use serde_traitobject::{
Serialize as SerializeTO,
Deserialize as DeserializeTO
};
use serde_derive::{Serialize, Deserialize};
use std::marker::PhantomData;
pub trait BlockTrait: SerializeTO + DeserializeTO{
fn exec(&mut self) -> bool;
}
#[derive(Serialize, Deserialize)]
struct TwoClosures<T> where
T:'static
{
inner: Option<TwoClosuresInner<T>>
}
#[derive(Serialize, Deserialize)]
struct TwoClosuresInner<T> where
T:'static
{
#[serde(with = "serde_traitobject")]
a: Box<FnOnceTO() -> T>,
#[serde(with = "serde_traitobject")]
b: Box<FnOnceTO(T) -> bool>,
_phantom_data:PhantomData<T>
}
impl<T> TwoClosures<T> where
{
fn new<A, B>(a:A, b:B) -> Self where
A: FnOnceTO() -> T + 'static,
B: FnOnceTO(T) -> bool + 'static
{
let inner = TwoClosuresInner {
a: Box::new(a),
b: Box::new(b),
_phantom_data: PhantomData
};
TwoClosures {
inner: Some(inner)
}
}
}
impl<T> BlockTrait for TwoClosures<T> {
fn exec(&mut self) -> bool {
let inner = self.inner.extract();
let t = (inner.a)();
(inner.b)(t)
}
}
fn main() {
let tc = TwoClosures::new(
FnOnce!(|| 8usize),
FnOnce!(move|v|v==8),
);
let tc_box:BoxTO<dyn BlockTrait> = BoxTO::new(tc);
let serialized = serde_json::to_string(&tc_box).unwrap();
println!("{}",serialized);
} And errors. It wants T to be serializable, but it is 'static.
UPD. I want to do not haul this 'de, but I try: pub trait BlockTrait<'de>: Serialize + Deserialize<'de>{
fn exec(&mut self) -> bool;
}
impl<'de, T> BlockTrait<'de> for TwoClosures<T> {
fn exec(&mut self) -> bool {
let inner = self.inner.extract();
let t = (inner.a)();
(inner.b)(t)
}
} And it wants T:Serialize, T:Deserialize =( |
And second question: how to serialize struct TwoClosures by may hands without derive? |
To make that minimal example work, you need to add #[derive(Serialize, Deserialize)]
#[serde(bound = "")]
struct TwoClosures<T> where
T:'static
{
inner: Option<TwoClosuresInner<T>>
} Without it, serde is generating: impl<T> Serialize for TwoClosures<T> where T: Serialize { ... } Though as actually impl<T> Serialize for TwoClosures<T> { ... } which then enables your program to compile. |
The serde guide to implementing Serialize and Deserialize are helpful here. The easiest way to serialize/deserialize them manually is as tuples. |
Hello. Hm.. It works! Thanks. About Box in structures. What I should to use std::Box or s_to::Box? What is differense? Now it is using std::Box and it works, but maybe I should use s_to::Box. But how I remember I was unable to serialize std::Box manually. |
Long live to new problems! My manual serialization worked. I am adding new generic M pub trait BlockTrait<M:MyTrait>: SerializeTO + DeserializeTO{
fn exec(&mut self, m:&mut M) -> bool;
}
pub trait MyTrait {}
struct TwoClosures<T, M> where
T:'static,
M:MyTrait + 'static
{
inner: DataContainer<TwoClosuresInner<T, M>>
}
struct TwoClosuresInner<T, M> where
T:'static,
M:MyTrait + 'static
{
a: BoxTO<FnOnceTO(&mut M) -> T>,
b: BoxTO<FnOnceTO(T) -> bool>,
} And I have this problem:
And something about Deser. The fragment of code: impl<T,M> Serialize for TwoClosures<T,M> where
//T:'static,
M:MyTrait
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use crate::serde::ser::SerializeStruct;
let inner = self.inner.get();
let mut s = serializer.serialize_struct("TwoClosures", 2)?;
s.serialize_field("a", &inner.a)?;
s.serialize_field("b", &inner.b)?;
s.end()
}
} It may be solved by using BoxTO instead of Box, but without M it worked! Very strange... And I want M to be Serial and Deserial -able. How to mark it in |
Hello, I am trying to do it.
You wrote:
How to do it(to deserialize)?
I use serde_traitobject and write this code
And I have error:
the trait bound `&usize: main::_IMPL_DESERIALIZE_FOR_MyStruct::_serde::Deserialize<'_>` is not satisfied
What should I do?
Can I create struct with two or more closures and serialize and deserialize it? Without Box<dyn ... Fn> just Box?
The text was updated successfully, but these errors were encountered: