Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upDeserialize a big fixed size array #573
Comments
This comment has been minimized.
This comment has been minimized.
|
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)
}
} |
This comment has been minimized.
This comment has been minimized.
|
Cool, thanks! Maybe you should put it somewhere, it could be useful for someone else. |
Yamakaky
closed this
Oct 6, 2016
dtolnay
referenced this issue
Oct 6, 2016
Open
Example of deserializing arrays bigger than [T; 32] #23
This comment has been minimized.
This comment has been minimized.
|
I filed serde-rs/serde-rs.github.io#23 to add this example to the website. |
dtolnay
added
the
support
label
Nov 6, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yamakaky commentedOct 6, 2016
struct Screen([Pixel; SCREEN_SIZE]). I would like to implementDeserializeon 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.