Merge pull request #1300 from dtolnay/refcell

Use try_borrow for serializing RefCell
This commit is contained in:
David Tolnay 2018-06-01 12:58:03 -07:00 committed by GitHub
commit 71fc318474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -8,10 +8,7 @@
use lib::*; use lib::*;
use ser::{Serialize, SerializeTuple, Serializer}; use ser::{Error, Serialize, SerializeTuple, Serializer};
#[cfg(feature = "std")]
use ser::Error;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -459,7 +456,10 @@ where
where where
S: Serializer, S: Serializer,
{ {
self.borrow().serialize(serializer) match self.try_borrow() {
Ok(value) => value.serialize(serializer),
Err(_) => Err(S::Error::custom("already mutably borrowed")),
}
} }
} }

View File

@ -9,6 +9,7 @@
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::CString; use std::ffi::CString;
use std::mem; use std::mem;
@ -563,6 +564,13 @@ fn test_cannot_serialize_paths() {
assert_ser_tokens_error(&path_buf, &[], "path contains invalid UTF-8 characters"); assert_ser_tokens_error(&path_buf, &[], "path contains invalid UTF-8 characters");
} }
#[test]
fn test_cannot_serialize_mutably_borrowed_ref_cell() {
let ref_cell = RefCell::new(42);
let _reference = ref_cell.borrow_mut();
assert_ser_tokens_error(&ref_cell, &[], "already mutably borrowed");
}
#[test] #[test]
fn test_enum_skipped() { fn test_enum_skipped() {
assert_ser_tokens_error( assert_ser_tokens_error(