Rust: ensure codegen traits cannot be used as trait objects

This change adds `Sized` as a supertrait of our `SealedInternal` trait, to
ensure that our codegen traits cannot be used as trait objects (i.e. with
`dyn`). I was thinking it would be prudent to do this early so that we don't
end up accidentally forced to support `dyn` usage.

Some of the traits already disallowed `dyn` as a result of having `Sized` as an
indirect supertrait or for other reasons, but a few traits did allow trait
objects.

If we ever do want to support some kind of type-erased message, I suspect we
will want to provide our own implementation. At least for C++, we already have
a vtable in the kernel, so it would seem wasteful to have Rust duplicate it.

PiperOrigin-RevId: 692195274
pull/19074/head
Adam Cozzette 3 weeks ago committed by Copybara-Service
parent d2f4410107
commit 51e2664f2f
  1. 7
      rust/internal.rs

@ -12,8 +12,8 @@
// Used by the proto! macro
pub use paste::paste;
pub use crate::r#enum::Enum;
use crate::map;
pub use crate::r#enum::Enum;
use crate::repeated;
pub use crate::ProtoStr;
use crate::Proxied;
@ -34,7 +34,10 @@ pub struct Private;
/// This is slightly less 'sealed' than the typical sealed trait pattern would
/// permit in other crates; this trait is intended to be available to crates
/// which were generated by protoc, but not to application code.
pub trait SealedInternal {}
///
/// We require Sized as a supertrait, because we generally do not want our
/// traits to support trait objects.
pub trait SealedInternal: Sized {}
/// A trait used by the proto_eq() gtest macro.
pub trait MatcherEq: SealedInternal + Debug {

Loading…
Cancel
Save