]> git.lizzy.rs Git - rust.git/blob - tests/ui-fulldeps/deriving-encodable-decodable-cell-refcell.rs
Fix problem noticed in PR106859 with char -> u8 suggestion
[rust.git] / tests / ui-fulldeps / deriving-encodable-decodable-cell-refcell.rs
1 // run-pass
2
3 #![allow(unused_imports)]
4 // This briefly tests the capability of `Cell` and `RefCell` to implement the
5 // `Encodable` and `Decodable` traits via `#[derive(Encodable, Decodable)]`
6 #![feature(rustc_private)]
7
8 extern crate rustc_macros;
9 extern crate rustc_serialize;
10
11 // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
12 // files.
13 #[allow(unused_extern_crates)]
14 extern crate rustc_driver;
15
16 use rustc_macros::{Decodable, Encodable};
17 use rustc_serialize::opaque::{MemDecoder, MemEncoder};
18 use rustc_serialize::{Decodable, Encodable, Encoder};
19 use std::cell::{Cell, RefCell};
20
21 #[derive(Encodable, Decodable)]
22 struct A {
23     baz: isize,
24 }
25
26 #[derive(Encodable, Decodable)]
27 struct B {
28     foo: Cell<bool>,
29     bar: RefCell<A>,
30 }
31
32 fn main() {
33     let obj = B { foo: Cell::new(true), bar: RefCell::new(A { baz: 2 }) };
34
35     let mut encoder = MemEncoder::new();
36     obj.encode(&mut encoder);
37     let data = encoder.finish();
38
39     let mut decoder = MemDecoder::new(&data, 0);
40     let obj2 = B::decode(&mut decoder);
41
42     assert_eq!(obj.foo.get(), obj2.foo.get());
43     assert_eq!(obj.bar.borrow().baz, obj2.bar.borrow().baz);
44 }