Minimize the scope of unsafe blocks in gencode

Before this change we would generate nested unsafe blocks that led to compiler warnings. This change minimizes each unsafe block and thereby avoids the nesting and compiler warnings. It's generally a best practice to keep unsafe blocks minimal to avoid the situation where a currently safe function is marked unsafe at a later point and all existing callsides don't need to be updated.

PiperOrigin-RevId: 582993146
pull/14710/head
Jakob Buchgraber 1 year ago committed by Copybara-Service
parent ba7067c9fa
commit 24dfcacc68
  1. 6
      src/google/protobuf/compiler/rust/accessors/singular_string.cc

@ -52,14 +52,12 @@ void SingularString::InMsgImpl(Context<FieldDescriptor> field) const {
{"transform_view", transform_view}},
R"rs(
pub fn $field$_opt(&self) -> $pb$::Optional<&$proxied_type$> {
unsafe {
let view = $getter_thunk$(self.inner.msg).as_ref();
let view = unsafe { $getter_thunk$(self.inner.msg).as_ref() };
$pb$::Optional::new(
$transform_view$ ,
$hazzer_thunk$(self.inner.msg)
unsafe { $hazzer_thunk$(self.inner.msg) }
)
}
}
)rs");
}},
{"field_mutator_getter",

Loading…
Cancel
Save