Todos los libros

Profesional

Apps Acerca de Coach Iniciar sesión Empezar a leer

Computer Science · Glosario

¿Qué es Object safety?

Definition 8.8 University Computer Science — Year 2 · Capítulo 8 — Traits and Generics

A trait is object safe, or dyn compatible, if dyn Trait can be formed: its methods can be called through a vtable. Methods that return Self by value, or have type parameters of their own, break this (the caller does not know the size of Self; a generic method would need one table entry per type argument), unless they are excluded with where Self: Sized.

Static and dynamic dispatch. Above, a generic is copied per type and each copy calls its methods directly, where they can be inlined. Below, a function on trait objects exists once, and each call goes through the vtable of the value at hand.
Static and dynamic dispatch. Above, a generic is copied per type and each copy calls its methods directly, where they can be inlined. Below, a function on trait objects exists once, and each call goes through the vtable of the value at hand.
Leer en el capítulo →