如果不是为了对象安全,Rust 的哪些安全保证会丢失? [英] What safety guarantees of Rust would be lost if not for object safety?

查看:26
本文介绍了如果不是为了对象安全,Rust 的哪些安全保证会丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解对象安全的概念.我从文档中知道对象安全性如下:

I am trying to understand the idea of being object safe. I know from the documentation that object safety is that following hold:

  • 特征不要求 Self: Sized
  • 它的所有方法都是对象安全的

如果放弃对象安全所需的两个条件中的任何一个,哪些事情可能会导致安全 Rust 提供的安全保证失败?

What things that conceivably could lead to the failure of the safety guarantees that safe Rust makes can be accomplished if either one of the two conditions required for object safety are dropped?

推荐答案

来自 自我满足大小的地方:重新审视对象安全:

仅当编译器可以通过存储在 trait 对象中的 vtable 将每个方法实现为动态函数调用时,特征才是对象安全的.

A trait is object safe only if the compiler can automatically implement it for itself, by implementing each method as a dynamic function call through the vtable stored in a trait object.

如果没有对象安全规则,人们可以编写具有由 trait 对象满足的类型签名的函数,而内部结构使其无法实际与 trait 对象一起使用.

Without the object safety rules one can write functions with type signatures satisfied by trait objects, where the internals make it impossible to actually use with trait objects.

我相信对象安全"的措辞选择回想起来可能很糟糕,因为它似乎与内存安全没有任何关系,术语(不)安全"的正常使用与内存安全无关.在 Rust 中.

I believe that the choice of phrasing of "object safety" may be a poor one in retrospect as it doesn't appear to have anything to do with memory safety, the normal use of the term "(un)safe" in Rust.

对象能力"可能更接近真相;可以通过 trait 对象 引用的 trait.

Object "ability" may be closer to the truth; a trait that has the ability to be referred to via a trait object.

这篇关于如果不是为了对象安全,Rust 的哪些安全保证会丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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