我尝试实现一个类似于 serdes 的Dump
proc_macro Serialize
。
为此,我有一个板条箱foo
,其中包含我的“原始”结构(P1
在P2
这种情况下),它应该只能是可转储的。
接下来我确实有一个foo_derive
包含程序宏本身的板条箱。
因为我想支持多种格式,所以我有第三个板条箱foo_dump
,其中包含Dump
(例如,可以转储此结构)和Dumper
(这是后端应该实现的东西)的特征定义。非常直截了当,直到这一点。
当我现在想编译它时,我得到了这个错误:
$ cargo build
error: cyclic package dependency: package `foo v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo)` depends on itself. Cycle:
package `foo v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo)`
... which is depended on by `foo_dump v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo_dump)`
... which is depended on by `foo_derive v0.1.0 (/tmp/tmp.u34pI5J6qd/example/foo_derive)`
我不知道正确的方法是什么,如何在这个 crate 中使用依赖项。我现在的一个是:
这当然是不可能的。
我错过了什么?我该怎么做才能打破依赖圈?
( mcve@github )
/货物.toml
[workspace]
members = [
"foo",
"foo_derive",
"foo_dump",
]
/foo/Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[dependencies]
foo_derive = { path = "../foo_derive" }
/foo/src/lib.rs
use foo_derive::Dump;
struct P1;
struct P2;
#[derive(Dump)]
struct Bar {
primitive_one: P1,
primitive_two: P2,
}
/foo_dump/Cargo.toml
[package]
name = "foo_dump"
version = "0.1.0"
edition = "2018"
[dependencies]
foo = { path = "../foo" }
/foo_dump/src/lib.rs
use foo::{P1, P2};
pub trait Dumper {
fn dump_p1(&mut self, value: &P1);
fn dump_p2(&mut self, value: &P2);
}
pub trait Dump {
fn dump<D: Dumper>(&self, d: D);
}
impl Dump for P1 {
fn dump<D: Dumper>(&self, d: D) {
d.dump_p1(self);
}
}
impl Dump for P2 {
fn dump<D: Dumper>(&self, d: D) {
d.dump_p2(self);
}
}
/foo_derive/Cargo.toml
[package]
name = "foo_derive"
version = "0.1.0"
edition = "2018"
[lib]
proc-macro = true
[dependencies]
syn = "*"
quote = "*"
foo_dump = { path = "../foo_dump" }
/foo_derive/src/lib.rs
extern crate proc_macro;
use quote::quote;
use proc_macro::TokenStream;
use syn::DeriveInput;
#[proc_macro_derive(Dump)]
pub fn derive_dump(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as DeriveInput);
let name = &input.ident;
quote!(
impl foo_dump::Dump for #name {
fn dump<D: foo_dump::Dumper>(&self, d: D) {
unimplemented!()
}
}
).into()
}