From be3c37eb8be65a472250e9893694ac02c474e06e Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Thu, 22 Sep 2022 09:57:55 +0300 Subject: [PATCH] Serialize unsized `RefCell`, `Mutex` and `RwLock` --- serde/src/ser/impls.rs | 6 +++--- test_suite/tests/test_ser.rs | 38 +++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index e13c739fe..8e8655582 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -522,7 +522,7 @@ where } } -impl Serialize for RefCell +impl Serialize for RefCell where T: Serialize, { @@ -538,7 +538,7 @@ where } #[cfg(feature = "std")] -impl Serialize for Mutex +impl Serialize for Mutex where T: Serialize, { @@ -554,7 +554,7 @@ where } #[cfg(feature = "std")] -impl Serialize for RwLock +impl Serialize for RwLock where T: Serialize, { diff --git a/test_suite/tests/test_ser.rs b/test_suite/tests/test_ser.rs index 003cdb5f0..ecaf125ec 100644 --- a/test_suite/tests/test_ser.rs +++ b/test_suite/tests/test_ser.rs @@ -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, + ], + ); +}