mneumann / rust-msgpack

[DEPRECATED] msgpack.org implementation for Rust language. Please use https://github.com/3Hren/msgpack-rust
122 stars 31 forks source link

task '<main>' failed at 'called `Option::unwrap()` on a `None` value' #28

Closed viperscape closed 9 years ago

viperscape commented 9 years ago

I'm probably doing this all wrong, but this fails:

extern crate serialize;
extern crate msgpack;

#[deriving(Encodable,Decodable)]
struct MyStruct {
  a: Vec<u32>,
  s: String
}

fn main() {
    let m = MyStruct{ a: vec![1u32, 5], s: "hi".to_string()};

    let enc =  msgpack::Encoder::to_msgpack(&m).ok().unwrap();
    println!("{}",&enc); //[130, 146, 1, 5, 162, 104, 105]
    let dec: msgpack::Value = msgpack::from_msgpack(enc).ok().unwrap();
    println!("{}", &dec);
}
viperscape commented 9 years ago

I updated rust to newest nightly (0.12->0.13) and reran this, now getting a new error. I'm on Windows..

  Compiling msgpack v0.0.1 (https://github.com/mneumann/rust-msgpack.git#cc990f56)
C:\Users\chris\.cargo\git\checkouts\rust-msgpack-437ef857d42dd556\master\src\lib.rs:96:18: 96:20 error: expected `=>`, found `..`
C:\Users\chris\.cargo\git\checkouts\rust-msgpack-437ef857d42dd556\master\src\lib.rs:96             0x00 .. 0x7f => Ok(c as u64),
                                                                                                        ^~
Could not compile `msgpack`.
thehydroimpulse commented 9 years ago

@viperscape Make sure you update the library version. If you're using cargo, you can run cargo update.

viperscape commented 9 years ago

okay, reran this cleanly and got same original error:

task '<main>' failed at 'called `Option::unwrap()` on a `None` value', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libcore/option.rs:347
An unknown error occurred
thehydroimpulse commented 9 years ago

@viperscape from_msgpack doesn't return a Value object but a T. unwrap is also extremely hard to debug things as it fails from within the Option implementation.

Here's a working example:

extern crate serialize;
extern crate msgpack;

use std::io::IoError;
use std::io::IoResult;

#[deriving(Show, Encodable,Decodable)]
struct MyStruct {
  a: Vec<u32>,
  s: String
}

fn main() {
    let m = MyStruct{ a: vec![1u32, 5], s: "hi".to_string()};

    let enc =  msgpack::Encoder::to_msgpack(&m).ok().unwrap();
    println!("1: {}",&enc); //[130, 146, 1, 5, 162, 104, 105]
    match msgpack::from_msgpack::<MyStruct>(enc) {
        Ok(s) => println!("success: {}", s),
        Err(err) => fail!("Error: {}", err)
    }
}
viperscape commented 9 years ago

great thanks, now I understand