如何忽略“#[derive(debug)]``''的通用参数?

发布于 2025-02-01 13:19:12 字数 1197 浏览 3 评论 0原文

这是一个最小的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2025-02-08 13:19:12

不幸的是,不是今天。您必须手动impl debug

impl<T: Config> fmt::Debug for Info<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Info").field("field", &self.field).finish()
    }
}

有一种目的是使创建所谓的“ perfect derive”:基于所需的内容而不是通用参数来衍生界限。例如,请参见这个Lang Team Design Design会议建议。但是现在什么都没有。

Unfortunately, not as of today. You have to impl Debug manually:

impl<T: Config> fmt::Debug for Info<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Info").field("field", &self.field).finish()
    }
}

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.

找个人就嫁了吧 2025-02-08 13:19:12

derive_where crate 可以用来实现这一点:

#[derive_where(Clone, Debug)]
pub struct Info<T: Config> {
    pub field: T::A,
}

The derive_where crate can be used to achieve this:

#[derive_where(Clone, Debug)]
pub struct Info<T: Config> {
    pub field: T::A,
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文