特征“Encodable”是私有的

Trait `Encodable` is private

我在使我的泛型 InMemoryColumn<T> 可序列化时遇到问题。它抱怨 'Encodable' 和 'Decodable' 特征是私有的,但我看到它是 public here。我怎样才能实现这些特征,以便我可以编码和解码底层 Vec<T>.

这是带有导入的代码:

extern crate bincode;
extern crate libc;
extern crate "rustc-serialize" as rustc_serialize;

use rustc_serialize::serialize::{Encodable,Decodable};
//import other libs

pub struct InMemoryColumn<T> {
    name: String,
    data: Vec<T>,
}

impl<T: Eq + Ord + Hash + Encodable + Decodable> InMemoryColumn<T> {
    fn save(&self, tbl_name: &str) {
        //encode self.data and write to disk 
    }

    fn load(path: &str, name: &str) -> Result<InMemoryColumn<T>,String> {
        //decode from disk and populate InMemoryColumn<T>
    }
}

EncodableDecodable 特征仅 public 相对于 serialize 模块。 That module is private though. As you can see in the mod.rs fileEncodableDecodable 直接在 rustc_serialize 箱子中重新导出。因此,您可以使用 EncodableDecodable 特征,如下所示:

use rustc_serialize::{Encodable,Decodable};