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

thread '<main>' panicked at 'called Result::unwrap() on an Err value: SyntaxError("expected value", 0, 0)' #95

Closed
linuxaged opened this issue Jun 30, 2015 · 7 comments
Labels

Comments

@linuxaged
Copy link

I written a serde example to do some deserializing, but got the error:

src/main.rs:12:5: 12:30 warning: unused result which must be used, #[warn(unused_must_use)] on by default
src/main.rs:12     f.read_to_string(&mut s);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:14:51: 14:58 warning: unused attribute, #[warn(unused_attributes)] on by default
src/main.rs:14     #[derive(Copy, Clone, Serialize, Deserialize, Display, Debug)]
                                                                 ^~~~~~~
     Running `target/debug/serde_example`
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: SyntaxError("expected value", 0, 0)', ../src/libcore/result.rs:731

what has happened? How to debug this?

@linuxaged
Copy link
Author

Sorry, it's my fault. The result can not map to Vec<Vertex>, i should deserialize to Vec<f64> then convert to Vec<Vertex>.

@erickt
Copy link
Member

erickt commented Jun 30, 2015

No problem! Going through a Vec<f64> is fine, but if you do want to cut down on that translation, you could instead use a custom deserializer to do like this:

impl serde::Deserialize for Vertex {
    #[inline]
    fn deserialize<D>(deserializer: &mut D) -> Result<Value, D::Error>
        where D: serde::Deserializer,

        let vertices: [f64, 16] = try!(serde::Deserialize::visit_seq(visitor));

        Ok(Vertex {
            position:[vertices[0], vertices[1],vertices[2]],
            normal:[vertices[3], vertices[4], vertices[5]],
            texcood:[vertices[6], vertices[7]],
            blendweight:[vertices[8], vertices[9], vertices[10], vertices[11]],
            blendindex:[vertices[12], vertices[13],vertices[14], vertices[15]]
        })
    }
}

It might be possible to do away with that inner [f64, 16] with a state machine, but I wouldn't be surprised if the compiler was able to optimize it away and just create the Vertex with no copy.

@linuxaged
Copy link
Author

try!(serde::Deserialize::visit_seq(visitor)); which visitor should i use here?
It is really tricky for a newcomer, any detailed document about how to write a custom deserializer for a struct ?

@linuxaged linuxaged reopened this Jul 3, 2015
@erickt
Copy link
Member

erickt commented Jul 7, 2015

@linuxaged: Yeah I'm slowly adding more docs, but there's plenty more I need to do.

You're right, I had a mistake, it really should be:

impl serde::Deserialize for Vertex {
    #[inline]
    fn deserialize<D>(deserializer: &mut D) -> Result<Value, D::Error>
        where D: serde::Deserializer,

        let vertices: [f64, 16] = try!(serde::Deserialize::deserialize(deserializer));

        Ok(Vertex {
            position:[vertices[0], vertices[1],vertices[2]],
            normal:[vertices[3], vertices[4], vertices[5]],
            texcood:[vertices[6], vertices[7]],
            blendweight:[vertices[8], vertices[9], vertices[10], vertices[11]],
            blendindex:[vertices[12], vertices[13],vertices[14], vertices[15]]
        })
    }
}

Sorry about that.

@linuxaged
Copy link
Author

Thanks for your support.
How about writing a custom deserializer deserializing a [f64; n * 16] array directly into Vec<Vertex> ?
We can only write custom deserializer for first order types, can not for two order types like Vec<Vertex>, right?

@erickt
Copy link
Member

erickt commented Jul 23, 2015

@linuxaged: Unfortunately we can't do specialization like that yet. Since serde has an impl for Vec<T> we can't also have a impl for Vec<Vertex>. However, you could create a newtyped struct as in struct Vertices(Vec<Vertex>) and then write an implementation for that. Then you could deserialize directly from a [f64; n * 16] into the Vertices type.

@tomprogrammer
Copy link
Contributor

For reference: RFC: impl specialization

@dtolnay dtolnay closed this as completed May 13, 2016
rubdos pushed a commit to rubdos/serde that referenced this issue Jun 20, 2017
Added the power method for rational numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants