< 返回版块

tokyohuang123 发表于 2021-05-19 16:42

Tags:怎么为async_std::path::PathBuf`实现Serialize

大佬们 src/entity/file.rs:12:5 | 12 | pub filepath: PathBuf, | ^^^ the trait Serialize is not implemented for async_std::path::PathBuf | = note: required by user::_::_serde::ser::SerializeStruct::serialize_field 这个async_std::path::PathBuf 其实还是用std::path::PathBuf 这种怎么serde实现Serialize

评论区

写评论
作者 tokyohuang123 2021-05-20 12:55

好的 感谢

--
👇
Grobycn: wrap一下,再参考 std::path::PathBuf 的方法

struct PathBuf(async_std::path::PathBuf);

impl Serialize for PathBuf {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self.0.as_path().to_str() {
            Some(s) => s.serialize(serializer),
            None => Err(Error::custom("path contains invalid UTF-8 characters")),
        }
    }
}
Grobycn 2021-05-19 19:48

wrap一下,再参考 std::path::PathBuf 的方法

struct PathBuf(async_std::path::PathBuf);

impl Serialize for PathBuf {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self.0.as_path().to_str() {
            Some(s) => s.serialize(serializer),
            None => Err(Error::custom("path contains invalid UTF-8 characters")),
        }
    }
}
1 共 2 条评论, 1 页