如何使用Serde反序列化parking_lot :: Mutex?

时间:2019-05-15 20:07:13

标签: rust mutex serde

我有一个存储在obj中的存储结构(Arc<Mutex>)。 Mutex是Parking_lot板条箱中std::sync::Mutex的改进版本。我需要反序列化包装的对象(obj),但是,我得到一个错误:

the trait `_IMPL_DESERIALIZE_FOR_SecurityLevel::_serde::Deserialize<'_>` is not implemented for `lock_api::mutex::Mutex<parking_lot::raw_mutex::RawMutex, Obj>`

在我的Cargo.toml中,我有(尤其是):

serde = { version = "1.0.89", features = ["rc"] }
parking_lot = {version = "0.8", features = ["nightly"]}

我应该怎么做?有解决方法,还是我必须手动实现这些特征?

1 个答案:

答案 0 :(得分:3)

对Serde的支持已添加到serde feature flag下的parking_lot 0.8:

parking_lot = { version = "0.8", features = ["nightly", "serde"] }

使用此代码,您提供的代码将被编译。这也可以编译:

use parking_lot::Mutex;
use serde::{Deserialize, Serialize};

fn implements<T>()
where
    Mutex<T>: for<'a> Deserialize<'a> + Serialize,
{
}