如何在编译时强制类型实现特征? [英] How to enforce that a type implements a trait at compile time?

查看:79
本文介绍了如何在编译时强制类型实现特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个这样的宏:

macro_rules! a {
    ( $n:ident, $t:ty ) => {
         struct $n {
             x: $t
         }
    }
}

但是$t应该实现AddSubMul特征.如何在编译时检查它?

But $t should implement Add, Sub and Mul traits. How can I check it at compile-time?

推荐答案

首先,解决没有宏的问题.一种解决方案是创建未记录的私有函数,如果不满足您的条件,这些私有函数将无法编译:

First, solve the problem without macros. One solution is to create undocumented private functions that will fail compilation if your conditions aren't met:

struct MyType {
    age: i32,
    name: String,
}

fn __assert_send()
where
    MyType: Send,
{}

fn __assert_sync()
where
    MyType: Sync,
{}

// RFC 2056
fn __assert_traits() {
    __assert_send();
    __assert_sync();
}

然后,修改简单的解决方案以使用宏:

Then, modify the simple solution to use macros:

macro_rules! a {
    ($n:ident, $t:ty) => {
        struct $n {
            x: $t
        }

        impl $n {
            fn __assert_add()
            where
                $t: std::ops::Add<$t, Output = $t>
            {}

            fn __assert_mul()
            where
                $t: std::ops::Mul<$t, Output = $t>
            {}

            // RFC 2056
            fn __assert_traits() {
                Self::__assert_add();
                Self::__assert_mul();
            }
        }
    }
}

a!(Moo, u8);
a!(Woof, bool);

fn main() {}

然后我会相信优化器会在编译时删除代码,因此我不会再有任何膨胀.

I would then trust in the optimizer to remove the code at compile time, so I wouldn't expect any additional bloat.

非常感谢

Major thanks to Chris Morgan for providing a better version of this that supports non-object-safe traits.

值得强调

It's worth highlighting RFC 2056 which will allow for "trivial" constraints in where clauses. Once implemented, clauses like this would be accepted:

impl Foo for Bar
where 
    i32: Iterator,
{}

在Rust的历史记录中,此确切的行为已多次更改,RFC 2056将其固定下来.为了保持这种情况下的行为,我们需要从没有约束的另一个函数中调用断言函数(因此必须始终为true).

This exact behavior has changed multiple times during Rust's history and RFC 2056 pins it down. To keep the behavior we want in this case, we need to call the assertion functions from another function which has no constraints (and thus must always be true).

这篇关于如何在编译时强制类型实现特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆