-
-
Notifications
You must be signed in to change notification settings - Fork 777
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
Deserialize a big fixed size array #573
Comments
Lack of type level integers limits what we can provide out of the box when it comes to arrays. The standard library does the same thing for Default, Debug, Eq, PartialEq, Ord, PartialOrd, AsRef, AsMut, Borrow, BorrowMut, Clone, Hash, IntoIterator - they are implemented for Here is one way to handle use serde::{Deserialize, Deserializer};
use serde::de::{self, Visitor, SeqVisitor};
impl Deserialize for Screen {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer
{
struct ScreenVisitor;
impl Visitor for ScreenVisitor {
type Value = Screen;
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Screen, V::Error>
where V: SeqVisitor
{
let mut screen = Screen([0; SCREEN_SIZE]);
for i in 0..SCREEN_SIZE {
screen.0[i] = match try!(visitor.visit()) {
Some(val) => val,
None => { return Err(de::Error::end_of_stream()); }
};
}
try!(visitor.end());
Ok(screen)
}
}
deserializer.deserialize_seq_fixed_size(SCREEN_SIZE, ScreenVisitor)
}
} |
Cool, thanks! Maybe you should put it somewhere, it could be useful for someone else. |
I filed serde-rs/serde-rs.github.io#23 to add this example to the website. |
This repo is a response to the limitations found in the serde format and rust language when handling XDR serialization. The end goal is to allow a user to manipulate normal rust objects that can than be properly serialized into the XDR format. The primary hangup with serde and other implementations is the lack of support for fixed length xdr array values. The built in rust array type is quite limited. See: https://stackoverflow.com/questions/30901965/implement-debug-trait-for-large-array-type serde-rs/serde#573 To work around this I've used Vec<T> as the type for both fixed and variable length xdr arrays allowing a user to add a macro attribute to distinguish between fixed/variable length while defaulting to a max sized variable length array. Current progress is tracked in the README.md for now, with most of the serialization work completed and all of the deserialization work to go still. Breaks nothing.
struct Screen([Pixel; SCREEN_SIZE])
. I would like to implementDeserialize
on this type. How could I do this? I may not be a good idea to do 10000+ variables like for in https://github.com/serde-rs/serde/blob/master/serde/src/de/impls.rs#L557-L647.The text was updated successfully, but these errors were encountered: