From 7949a62bf88e1d4e97c93b3a7b8a8cfb201b713a Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Fri, 16 Feb 2024 00:54:55 -0800 Subject: [PATCH] #rust #protobuf #concurrency Make messages Send PiperOrigin-RevId: 607600614 --- src/google/protobuf/compiler/rust/message.cc | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index a44a4b94f1..65c176bc3d 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -875,12 +875,15 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } // SAFETY: - // - `$Msg$` does not provide shared mutation with its arena. - // - `$Msg$Mut` is not `Send`, and so even in the presence of mutator - // splitting, synchronous access of an arena that would conflict with - // field access is impossible. + // - `$Msg$` is `Sync` because it does not implement interior mutability. + // Neither does `$Msg$Mut`. unsafe impl Sync for $Msg$ {} + // SAFETY: + // - `$Msg$` is `Send` because it uniquely owns its arena and does + // not use thread-local data. + unsafe impl Send for $Msg$ {} + impl $pb$::Proxied for $Msg$ { type View<'msg> = $Msg$View<'msg>; type Mut<'msg> = $Msg$Mut<'msg>; @@ -912,12 +915,12 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { } // SAFETY: - // - `$Msg$View` does not perform any mutation. - // - While a `$Msg$View` exists, a `$Msg$Mut` can't exist to mutate - // the arena that would conflict with field access. - // - `$Msg$Mut` is not `Send`, and so even in the presence of mutator - // splitting, synchronous access of an arena is impossible. + // - `$Msg$View` is `Sync` because it does not support mutation. unsafe impl Sync for $Msg$View<'_> {} + + // SAFETY: + // - `$Msg$View` is `Send` because while its alive a `$Msg$Mut` cannot. + // - `$Msg$View` does not use thread-local data. unsafe impl Send for $Msg$View<'_> {} impl<'msg> $pb$::ViewProxy<'msg> for $Msg$View<'msg> {