Skip to content

Commit

Permalink
Merge pull request #2282 from ChayimFriedman2/sized-mutex-refcell-rwlock
Browse files Browse the repository at this point in the history
Serialize unsized `RefCell`, `Mutex` and `RwLock`
  • Loading branch information
dtolnay committed Sep 22, 2022
2 parents f0346ae + be3c37e commit d99009f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
6 changes: 3 additions & 3 deletions serde/src/ser/impls.rs
Expand Up @@ -522,7 +522,7 @@ where
}
}

impl<T> Serialize for RefCell<T>
impl<T: ?Sized> Serialize for RefCell<T>
where
T: Serialize,
{
Expand All @@ -538,7 +538,7 @@ where
}

#[cfg(feature = "std")]
impl<T> Serialize for Mutex<T>
impl<T: ?Sized> Serialize for Mutex<T>
where
T: Serialize,
{
Expand All @@ -554,7 +554,7 @@ where
}

#[cfg(feature = "std")]
impl<T> Serialize for RwLock<T>
impl<T: ?Sized> Serialize for RwLock<T>
where
T: Serialize,
{
Expand Down
38 changes: 37 additions & 1 deletion test_suite/tests/test_ser.rs
Expand Up @@ -13,7 +13,7 @@ use std::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
AtomicUsize,
};
use std::sync::{Arc, Weak as ArcWeak};
use std::sync::{Arc, Mutex, RwLock, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH};

#[cfg(unix)]
Expand Down Expand Up @@ -847,3 +847,39 @@ fn test_integer128() {

assert_ser_tokens_error(&1u128, &[], "u128 is not supported");
}

#[test]
fn test_refcell_dst() {
assert_ser_tokens(
&RefCell::new([true]) as &RefCell<[bool]>,
&[
Token::Seq { len: Some(1) },
Token::Bool(true),
Token::SeqEnd,
],
);
}

#[test]
fn test_mutex_dst() {
assert_ser_tokens(
&Mutex::new([true]) as &Mutex<[bool]>,
&[
Token::Seq { len: Some(1) },
Token::Bool(true),
Token::SeqEnd,
],
);
}

#[test]
fn test_rwlock_dst() {
assert_ser_tokens(
&RwLock::new([true]) as &RwLock<[bool]>,
&[
Token::Seq { len: Some(1) },
Token::Bool(true),
Token::SeqEnd,
],
);
}

0 comments on commit d99009f

Please sign in to comment.