如何忽略“#[derive(debug)]``''的通用参数?
这是一个最小的
use core::fmt::Debug;
pub trait Config {
type A: Debug;
}
#[derive(Debug)]
pub struct Info<T: Config> {
pub field: T::A,
}
pub struct Conf;
impl Config for Conf {
type A = i128;
}
fn main() {
let s = Info::<Conf> {
field: 123
};
dbg!(s);
}
我正在使用的框架( subtrate )使用了config> config
的概念汇总了模块(托盘)的所有通用类型的特征。
问题是尝试#[derive(debug)]
仅使用对象的相关类型t
实现配置
仍然要求t
实现debug
本身。
error[E0277]: `Conf` doesn't implement `Debug`
--> src/main.rs:22:5
|
22 | dbg!(s);
| ^^^^^^^ `Conf` cannot be formatted using `{:?}`
|
= help: the trait `Debug` is not implemented for `Conf`
= note: add `#[derive(Debug)]` to `Conf` or manually `impl Debug for Conf`
此外,我无法控制conf
对象的实现。无论如何,我并不是要打印有关conf
对象本身的任何内容。
有没有一种方法可以使#[derive(debug)]
info> info 忽略t
?
Here's a minimal example representing the type of problem I'm running into.
use core::fmt::Debug;
pub trait Config {
type A: Debug;
}
#[derive(Debug)]
pub struct Info<T: Config> {
pub field: T::A,
}
pub struct Conf;
impl Config for Conf {
type A = i128;
}
fn main() {
let s = Info::<Conf> {
field: 123
};
dbg!(s);
}
The framework that I'm using (Substrate) uses this concept of a Config
trait that aggregates all generic types for a module (pallet).
The problem is that trying to #[derive(Debug)]
for a struct that only uses the associated types of the object T
implementing Config
still requires that T
implements Debug
itself.
error[E0277]: `Conf` doesn't implement `Debug`
--> src/main.rs:22:5
|
22 | dbg!(s);
| ^^^^^^^ `Conf` cannot be formatted using `{:?}`
|
= help: the trait `Debug` is not implemented for `Conf`
= note: add `#[derive(Debug)]` to `Conf` or manually `impl Debug for Conf`
Moreover, I don't have control of the implementation of the Conf
object. Regardless, I'm not trying to print anything about the Conf
object itself.
Is there a way to make #[derive(Debug)]
for Info
ignore T
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,不是今天。您必须手动impl
debug
:有一种目的是使创建所谓的“ perfect derive”:基于所需的内容而不是通用参数来衍生界限。例如,请参见这个Lang Team Design Design会议建议。但是现在什么都没有。
Unfortunately, not as of today. You have to impl
Debug
manually:There is an intent to make it possible to create what is called "perfect derive": derive bounds based on what needed and not the generic parameters. See for example this lang team design meeting proposal. But for now there is nothing.
derive_where
crate 可以用来实现这一点:The
derive_where
crate can be used to achieve this: