Closed
Description
The following code should fail to compile:
extern mod extra;
use extra::json;
use extra::json::*;
use extra::serialize::{Decodable, Encodable};
use extra::treemap::TreeMap;
use std::io;
#[deriving(Encodable, Decodable)]
struct WorkMap(TreeMap<WorkKey, ~str>); //~ ERROR can't derive Encodable on this
#[deriving(Encodable, Decodable, Eq, TotalEq, TotalOrd)]
struct WorkKey { name: ~str, kind: ~str }
because there's only an Encodable/Decodable instance for TreeMaps that have ~strs as keys. However, it compiles successfully. Moreover, if I add the following main()
function:
fn main() {
let mut t = WorkMap(TreeMap::new());
t.insert(WorkKey{ name: ~"Tim", kind: ~"person"}, ~"tjc");
let encoded_str = do io::with_str_writer |wr| {
let mut encoder = json::Encoder(wr);
t.encode(&mut encoder);
};
let decoded = do io::with_str_reader(encoded_str) |rdr| {
let j = json::from_reader(rdr).unwrap();
let mut decoder = json::Decoder(j);
Decodable::decode(&mut decoder)
};
assert!(t.len() == decoded.len());
}
the encode/decode round-trip fails with a JSON decoding error, because the encoder happily prints out a representation of the struct key where a string is required.
It looks like maybe a bug in trait matching where the type parameters are getting ignored? I'm not sure.