Skip to content
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

Can bincode be used to serialize and deserialize data which is meant to be sent and received using a socket? #257

Closed
nbro opened this issue Dec 1, 2018 · 1 comment

Comments

@nbro
Copy link

@nbro nbro commented Dec 1, 2018

I'm trying to send several types using sockets. So, to encode these several types, I am using bincode in conjunction with serde. I'm using rustc 1.30.1, bincode 1.0, serde_derive 1.0 and serde = 1.0.

The function I am using to send data using a socket can be found here. As you can see, I am actually trying to send a Msg<T>, where Msg is an enum, which contains a value that I want to send of type T. You can see the definition of the enum here.

The function I am using to receive data from the socket can be found here. As you can see, I am also trying to return a Msg<T> from this function.

Now, the problem is the following. If T is a f32, e.g. say 2.0, I am actually receiving 1073741824 inside the field of the enum value, in the receiving function. I have checked using the following code:

unsafe { std::mem::transmute::<u32, f32>(1073741824) }

And, apparently, 1073741824 is the unsigned representation of 2.0. If I do the same with a char, that is, if T is a char, e.g. 'a', I receive 97, which, again, after checking it with unsafe { std::mem::transmute::<u32, char>(97) }, is the unsigned representation of 'a'.

Btw, I have the following bounds on T, in the impl of the generic struct over T: T: Serialize + DeserializeOwned + Copy + Clone + Debug.

Questions

So, what's the problem with my code above? How can I receive a float and a char from a socket using bincode and serde?

Furthermore, if T is a tuple, say, (2, 1), I only receive the first element of the tuple, i.e. 2. So, what's going on here? How can I receive any type that has the bounds T: Serialize + DeserializeOwned + Copy + Clone + Debug?

Notes

Note: I am able to receive u32, i32 and usize values correctly.

Summary of the problems

  • I cannot receive a f32 or f64 that is sent using a socket
  • I cannot receive a tuple that is sent using a socket
  • I probably cannot receive anything, that is sent using a socket, apart from integers

Comments

@nbro
Copy link
Author

@nbro nbro commented Dec 1, 2018

The issue is not in those functions that I shared above. I have several structs that depend on the same T. When creating an object of those structs, I had to explicitly state the concrete type, e.g. let mut proposer = Proposer::<u32>::new(...). However, I was forgetting to change u32 to e.g. f32. Thus, this problem is not related to serde, bincode or the sockets. It was just related to the fact that I am a newbie in Rust and I had forgotten this part of my code.

@nbro nbro closed this Dec 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.