Use try_borrow for serializing RefCell

This commit is contained in:
Konrad Borowski 2018-06-01 09:08:16 +02:00
parent dbaf2893e3
commit c3b9ee314b
2 changed files with 12 additions and 1 deletions

View File

@ -459,7 +459,10 @@ where
where
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]
extern crate serde_derive;
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ffi::CString;
use std::mem;
@ -563,6 +564,13 @@ fn test_cannot_serialize_paths() {
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]
fn test_enum_skipped() {
assert_ser_tokens_error(