What safety guarantees of Rust would be lost if not for object safety?

前端 未结 1 1135
别那么骄傲
别那么骄傲 2021-01-19 09:31

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

  • The trait does not require th
相关标签:
1条回答
  • 2021-01-19 10:06

    From Where Self Meets Sized: Revisiting Object Safety:

    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.

    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.

    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.

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

    0 讨论(0)
提交回复
热议问题