From 0e84d6fc1ae1267eaf38b9c518a6d8f68ff6305c Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Fri, 4 Jul 2014 22:24:50 +1000 Subject: Implemented Decodable/Encodable for Cell and RefCell. Fixes #15395 Updated PR with fixme and test Updated PR with fixme and test --- src/libserialize/serialize.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/libserialize') diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 0ed555e392f..03d9445b9b9 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -17,6 +17,7 @@ Core encoding and decoding interfaces. use std::path; use std::rc::Rc; use std::gc::{Gc, GC}; +use std::cell::{Cell, RefCell}; pub trait Encoder { // Primitive types: @@ -536,6 +537,35 @@ impl> Decodable for path::windows::Path { } } +impl, T: Encodable + Copy> Encodable for Cell { + fn encode(&self, s: &mut S) -> Result<(), E> { + self.get().encode(s) + } +} + +impl, T: Decodable + Copy> Decodable for Cell { + fn decode(d: &mut D) -> Result, E> { + Ok(Cell::new(try!(Decodable::decode(d)))) + } +} + +// FIXME: #15036 +// Should use `try_borrow`, returning a +// `encoder.error("attempting to Encode borrowed RefCell")` +// from `encode` when `try_borrow` returns `None`. + +impl, T: Encodable> Encodable for RefCell { + fn encode(&self, s: &mut S) -> Result<(), E> { + self.borrow().encode(s) + } +} + +impl, T: Decodable> Decodable for RefCell { + fn decode(d: &mut D) -> Result, E> { + Ok(RefCell::new(try!(Decodable::decode(d)))) + } +} + // ___________________________________________________________________________ // Helper routines // -- cgit 1.4.1-3-g733a5