Merge pull request #2282 from ChayimFriedman2/sized-mutex-refcell-rwlock

Serialize unsized `RefCell`, `Mutex` and `RwLock`
This commit is contained in:
David Tolnay 2022-09-22 10:48:54 -07:00 committed by GitHub
commit d99009f3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 4 deletions

View File

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

View File

@ -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)]
@ -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,
],
);
}