Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize unsized RefCell, Mutex and RwLock #2282

Merged
merged 1 commit into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
],
);
}