rust/src/test/run-pass/deriving-encodable-decodable-cell-refcell.rs
Daniel Micay 85c2c2e38c implement Writer for Vec<u8>
The trait has an obvious, sensible implementation directly on vectors so
the MemWriter wrapper is unnecessary. This will halt the trend towards
providing all of the vector methods on MemWriter along with eliminating
the noise caused by conversions between the two types. It also provides
the useful default Writer methods on Vec<u8>.

After the type is removed and code has been migrated, it would make
sense to add a new implementation of MemWriter with seeking support. The
simple use cases can be covered with vectors alone, and ones with the
need for seeks can use a new MemWriter implementation.
2014-11-18 01:09:46 -05:00

41 lines
1.2 KiB
Rust

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// This briefly tests the capability of `Cell` and `RefCell` to implement the
// `Encodable` and `Decodable` traits via `#[deriving(Encodable, Decodable)]`
extern crate serialize;
use std::cell::{Cell, RefCell};
use serialize::{Encodable, Decodable};
use serialize::json;
#[deriving(Encodable, Decodable)]
struct A {
baz: int
}
#[deriving(Encodable, Decodable)]
struct B {
foo: Cell<bool>,
bar: RefCell<A>,
}
fn main() {
let obj = B {
foo: Cell::new(true),
bar: RefCell::new( A { baz: 2 } )
};
let s = json::encode(&obj);
let obj2: B = json::decode(s.as_slice()).unwrap();
assert!(obj.foo.get() == obj2.foo.get());
assert!(obj.bar.borrow().baz == obj2.bar.borrow().baz);
}