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

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

问题描述

我想写一个这样的宏:

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,
}

const _: () = {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}

    // RFC 2056
    fn assert_all() {
        assert_send::<MyType>();
        assert_sync::<MyType>();
    }
};

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

Then, modify the simple solution to use macros:

macro_rules! example {
    ($name:ident, $field:ty) => {
        struct $name {
            x: $field,
        }

        const _: () = {
            fn assert_add<T: std::ops::Add<$field, Output = $field>>() {}
            fn assert_mul<T: std::ops::Mul<$field, Output = $field>>() {}

            // RFC 2056
            fn assert_all() {
                assert_add::<$field>();
                assert_mul::<$field>();
            }
        };
    };
}

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

在这两种情况下,我们创建一个虚拟 const 值来限定函数及其调用,避免名称冲突.

In both cases, we create a dummy const value to scope the functions and their calls, avoiding name clashes.

然后我会相信优化器会在编译时删除代码,所以我不会期望任何额外的膨胀.

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

非常感谢 Chris Morgan 提供了一个更好的版本,支持非对象安全的特征.

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

值得强调 RFC 2056 这将允许琐碎"的where 子句中的约束.一旦实施,这样的条款将被接受:

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 将其固定下来.为了在这种情况下保持我们想要的行为,我们需要从另一个没有约束的函数调用断言函数(因此必须始终为真).

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天全站免登陆