"Box< Fn()+发送+'static>"是什么意思?是指生锈? [英] What does "Box<Fn() + Send + 'static>" mean in rust?

查看:89
本文介绍了"Box< Fn()+发送+'static>"是什么意思?是指生锈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Box<Fn() + Send + 'static>在锈中是什么意思?

在阅读高级类型一章时,我偶然发现了这种语法. Send是一个特征,但是在类型参数化中,+对特征(在此情况下为'static)的生存期意味着什么?还有什么是Fn()?

I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ('static in this case) in type parametrization ? Also what is Fn() ?

推荐答案

让我们一一分解它.

Box<T>是指向堆分配的T的指针.我们在这里使用它是因为特征对象只能存在于指针后面.

Box<T> is a pointer to heap-allocated T. We use it here because trait objects can only exist behind pointers.

Box<Fn() + Send + 'static>中,Fn() + Send + 'static 特征对象 类型.将来,它将写成 以避免混淆.

In Box<Fn() + Send + 'static>, Fn() + Send + 'static is a trait object type. In future, it will be written Box<dyn (Fn() + Send + 'static)> to avoid confusion.

内部dyn是对原始类型的限制.仅当T: Fn() + Send + 'static 时, Box<T>才能被强制为Box<Fn() + Send + 'static>.因此,尽管我们不知道原始类型,但我们可以假定它是Fn()Send,并且具有'static生存期.

Inside dyn are restrictions to the original type. Box<T> can be coerced into Box<Fn() + Send + 'static> only when T: Fn() + Send + 'static. Therefore, although we don't know the original type, we can assume it was Fn() and Send and had 'static lifetime.

这是一个特征,就像CloneDefault一样.但是,它使用特殊的语法糖.

This is a trait, just like Clone or Default. However, it uses a special syntax sugar.

  • Fn(A1, ..., An)Fn<(A1, ..., An), Output=()>的语法糖.
  • Fn(A1, ..., An) -> RFn<(A1, ..., An), Output=R>的语法糖.
  • 此语法糖也适用于以下特征: Fn FnMut
  • Fn(A1, ..., An) is a syntax sugar for Fn<(A1, ..., An), Output=()>.
  • Fn(A1, ..., An) -> R is a syntax sugar for Fn<(A1, ..., An), Output=R>.
  • This syntax sugar also applies to the following traits: Fn, FnMut, FnOnce, and FnBox.

那么Fn是什么意思? T: Fn(A1, ..., An) -> R表示x: T是带有参数A1, ..., An并返回类型R的可调用对象.示例包括函数指针和闭包.

So what does Fn mean? T: Fn(A1, ..., An) -> R means x: T is a callable object with arguments A1, ..., An and return type R. Examples include function pointers and closures.

Send 表示该类型的值可以跨线程发送.由于这是自动特征,它可以可以指定为dyn类型的第二个边界 (特征对象类型).

Send means that values of this type can be sent across threads. Since this is an auto trait, it can be specified as the second bounds of dyn types (trait object types).

实际上, dyn类型(特征对象类型)必须精确地绑定一个生存期.省略时可以推断出来.推理规则在 RFC 0192

In fact, dyn types (trait object types) must have exactly one lifetime bound. It's inferred when omitted. The inference rule is described in RFC 0192 and RFC 1156. It's basically as follows:

  1. 如果明确给出,则使用该生存期.
  2. 否则,它是从内在特质推断出来的.例如,Box<Any>Box<Any + 'static>,因为 Any: 'static .
  3. 如果特征没有适当的生存期,则可以从外部类型推断出它.例如,&'a Fn()&'a (Fn() + 'a).
  4. 如果甚至失败,它会退回到'static(用于函数签名)或匿名生存期(用于函数主体).
  1. If explicitly given, use that lifetime.
  2. Otherwise, it is inferred from the inner trait. For example, Box<Any> is Box<Any + 'static> because Any: 'static.
  3. If the trait doesn't have an appropriate lifetime, it is inferred from the outer type. For example, &'a Fn() is &'a (Fn() + 'a).
  4. If that even failed, it falls back to 'static (for a function signature) or an anonymous lifetime (for a function body).

结论

f: Box<Fn() + Send + 'static>是指向可调用值(原始类型未知且动态更改)的专有指针,例如闭包(无自变量或无返回值),这些值可以在线程之间发送,并且只要程序存在就可以使用本身.

Conclusion

f: Box<Fn() + Send + 'static> is an owned pointer to a callable value (with the original type unknown and dynamically change) such as closures (with no argument or no return value), which can be sent across threads and lives as long as the program itself.

这篇关于"Box&lt; Fn()+发送+'static&gt;"是什么意思?是指生锈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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