Skip to content

Commit 179b2b4

Browse files
committed
auto merge of #15411 : mitchmindtree/rust/master, r=alexcrichton
I ran `make check` and everything went smoothly. I also tested `#[deriving(Decodable, Encodable)]` on a struct containing both Cell<T> and RefCell<T> and everything now seems to work fine.
2 parents d9db7f6 + 0e84d6f commit 179b2b4

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/libserialize/serialize.rs

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Core encoding and decoding interfaces.
1717
use std::path;
1818
use std::rc::Rc;
1919
use std::gc::{Gc, GC};
20+
use std::cell::{Cell, RefCell};
2021

2122
pub trait Encoder<E> {
2223
// Primitive types:
@@ -536,6 +537,35 @@ impl<E, D: Decoder<E>> Decodable<D, E> for path::windows::Path {
536537
}
537538
}
538539

540+
impl<E, S: Encoder<E>, T: Encodable<S, E> + Copy> Encodable<S, E> for Cell<T> {
541+
fn encode(&self, s: &mut S) -> Result<(), E> {
542+
self.get().encode(s)
543+
}
544+
}
545+
546+
impl<E, D: Decoder<E>, T: Decodable<D, E> + Copy> Decodable<D, E> for Cell<T> {
547+
fn decode(d: &mut D) -> Result<Cell<T>, E> {
548+
Ok(Cell::new(try!(Decodable::decode(d))))
549+
}
550+
}
551+
552+
// FIXME: #15036
553+
// Should use `try_borrow`, returning a
554+
// `encoder.error("attempting to Encode borrowed RefCell")`
555+
// from `encode` when `try_borrow` returns `None`.
556+
557+
impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for RefCell<T> {
558+
fn encode(&self, s: &mut S) -> Result<(), E> {
559+
self.borrow().encode(s)
560+
}
561+
}
562+
563+
impl<E, D: Decoder<E>, T: Decodable<D, E>> Decodable<D, E> for RefCell<T> {
564+
fn decode(d: &mut D) -> Result<RefCell<T>, E> {
565+
Ok(RefCell::new(try!(Decodable::decode(d))))
566+
}
567+
}
568+
539569
// ___________________________________________________________________________
540570
// Helper routines
541571
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This briefuly tests the capability of `Cell` and `RefCell` to implement the
12+
// `Encodable` and `Decodable` traits via `#[deriving(Encodable, Decodable)]`
13+
14+
extern crate serialize;
15+
16+
use std::cell::{Cell, RefCell};
17+
use std::io::MemWriter;
18+
use serialize::{Encodable, Decodable};
19+
use serialize::ebml;
20+
use serialize::ebml::writer::Encoder;
21+
use serialize::ebml::reader::Decoder;
22+
23+
#[deriving(Encodable, Decodable)]
24+
struct A {
25+
baz: int
26+
}
27+
28+
#[deriving(Encodable, Decodable)]
29+
struct B {
30+
foo: Cell<bool>,
31+
bar: RefCell<A>,
32+
}
33+
34+
fn main() {
35+
let obj = B {
36+
foo: Cell::new(true),
37+
bar: RefCell::new( A { baz: 2 } )
38+
};
39+
let mut w = MemWriter::new();
40+
{
41+
let mut e = Encoder::new(&mut w);
42+
match obj.encode(&mut e) {
43+
Ok(()) => (),
44+
Err(e) => fail!("Failed to encode: {}", e)
45+
};
46+
}
47+
let doc = ebml::Doc::new(w.get_ref());
48+
let mut dec = Decoder::new(doc);
49+
let obj2: B = match Decodable::decode(&mut dec) {
50+
Ok(v) => v,
51+
Err(e) => fail!("Failed to decode: {}", e)
52+
};
53+
assert!(obj.foo.get() == obj2.foo.get());
54+
assert!(obj.bar.borrow().baz == obj2.bar.borrow().baz);
55+
}

0 commit comments

Comments
 (0)