What does the exclamation point mean in a trait implementation?

二次信任 提交于 2019-12-04 17:49:38

问题


I found in the library reference for std::rc::Rc this trait implementation

impl<T> !Send for Rc<T> 
where
    T: ?Sized, 

What does the exclamation point in front of Send mean?

I consulted both The Rust Programming Language¹ book and The Rust Reference², but didn't find an explanation. Please give a reference in your answer.


¹ especially the [section 3.19 Traits
² and sections 5.1 Traits and 5.1 Implementations

回答1:


It's a negative trait implementation for the Send trait as described in RFC 19.

As a summary: The Send trait is an unsafe trait. The RFC says:

[It] is unsafe to implement, because implementing it carries semantic guarantees that, if compromised, threaten memory safety in a deep way.

However, it is implemented by default for all types. Currently the syntax is:

unsafe impl Send for .. { }

Note .. as the syntax for a default implementation. The trait must also not define any methods. A default implementation is a marker trait implemented by all types

There is new syntax on its way for default traits, and they will be called auto trait, but as of now those are in experimental and possibly buggy. But here's is the syntax for future use:

unsafe auto trait Send { }

Therefore, to opt out of Send, write a negative trait implementation. This syntax will stay the same, even once auto traits are stable:

impl !Send for MyType { }

See also the answer to another question: What is an auto trait in Rust?




回答2:


This is a negative trait impl, so you can read it as opting out of the Send trait.



来源:https://stackoverflow.com/questions/30291217/what-does-the-exclamation-point-mean-in-a-trait-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!