dfinity / candid

Candid Library for the Internet Computer
Apache License 2.0
277 stars 82 forks source link

Newtype structs serialized with `Encode!` fail to deserialize with `Decode!` #97

Closed alin-at-dfinity closed 4 years ago

alin-at-dfinity commented 4 years ago

Minimal repro:

use candid::{CandidType, Decode, Deserialize, Encode};
fn main() {
    #[derive(CandidType, Deserialize)]
    struct Wrapper(Vec<usize>);
    let w = Wrapper(vec![1]);
    let b = candid::Encode!(&w).expect("failed to encode");
    candid::Decode!(&b[..], Wrapper).expect("failed to decode");
}

fails with:

thread 'X' panicked at 'failed to decode: 
Message: "Type mismatch. Type on the wire: Vec; Provided type: Nat64"
States:
Trailing type: [I(-8)]
Trailing value: [01, 01, 00, 00, 00, 00, 00, 00, 00]
Type table: [[I(-20), U(1), U(0), I(1)], [I(-19), I(-8)]]
Remaining value types: []
chenyan-dfinity commented 4 years ago

Duplicate of https://github.com/dfinity/candid/issues/43

This was fixed in 0.5.3, and I just confirmed the example above works on master.