Fix hazzer emission of Optional

We were always emitting Optional<T> for accessors, when they should've been behind `_opt`.

We've refactored our previous accessor into `getter` and `getter_opt`. We'll only emit `getter_opt` when we're dealing with optional fields.

PiperOrigin-RevId: 544087591
pull/12988/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent e2eae81b97
commit ead5d565e7
  1. 35
      rust/test/shared/accessors_test.rs
  2. 29
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc

@ -31,40 +31,57 @@
/// Tests covering accessors for singular bool, int64, and bytes fields.
use unittest_proto::proto2_unittest::TestAllTypes;
#[test]
fn test_default_accessors() {
// defaults are from here:
// http://google3/third_party/protobuf/unittest.proto section default_*
let msg = TestAllTypes::new();
assert_eq!(msg.default_int32(), 41);
assert_eq!(msg.default_int64(), 42);
assert_eq!(msg.default_bool(), true);
}
#[test]
fn test_optional_int32_accessors() {
let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_int32(), None);
assert_eq!(msg.optional_int32_opt(), None);
assert_eq!(msg.optional_int32(), 0);
msg.optional_int32_set(Some(1));
assert_eq!(msg.optional_int32(), Some(1));
assert_eq!(msg.optional_int32_opt(), Some(1));
assert_eq!(msg.optional_int32(), 1);
msg.optional_int32_set(None);
assert_eq!(msg.optional_int32(), None);
assert_eq!(msg.optional_int32_opt(), None);
assert_eq!(msg.optional_int32(), 0);
}
#[test]
fn test_optional_int64_accessors() {
let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_int64(), None);
assert_eq!(msg.optional_int64_opt(), None);
assert_eq!(msg.optional_int64(), 0);
msg.optional_int64_set(Some(42));
assert_eq!(msg.optional_int64(), Some(42));
assert_eq!(msg.optional_int64_opt(), Some(42));
assert_eq!(msg.optional_int64(), 42);
msg.optional_int64_set(None);
assert_eq!(msg.optional_int64(), None);
assert_eq!(msg.optional_int64_opt(), None);
assert_eq!(msg.optional_int64(), 0);
}
#[test]
fn test_optional_bool_accessors() {
let mut msg = TestAllTypes::new();
assert_eq!(msg.optional_bool(), None);
assert_eq!(msg.optional_bool_opt(), None);
msg.optional_bool_set(Some(true));
assert_eq!(msg.optional_bool(), Some(true));
assert_eq!(msg.optional_bool_opt(), Some(true));
msg.optional_bool_set(None);
assert_eq!(msg.optional_bool(), None);
assert_eq!(msg.optional_bool_opt(), None);
}
#[test]

@ -52,17 +52,34 @@ class SingularScalar final : public AccessorGenerator {
{"field", field.desc().name()},
{"Scalar", PrimitiveRsTypeName(field)},
{"hazzer_thunk", Thunk(field, "has")},
{"getter",
[&] {
field.Emit({}, R"rs(
pub fn r#$field$(&self) -> $Scalar$ {
unsafe { $getter_thunk$(self.msg) }
}
)rs");
}},
{"getter_opt",
[&] {
if (!field.desc().is_optional()) return;
field.Emit({}, R"rs(
pub fn r#$field$_opt(&self) -> Option<$Scalar$> {
if !unsafe { $hazzer_thunk$(self.msg) } {
return None;
}
Some(unsafe { $getter_thunk$(self.msg) })
}
)rs");
}},
{"getter_thunk", Thunk(field, "get")},
{"setter_thunk", Thunk(field, "set")},
{"clearer_thunk", Thunk(field, "clear")},
},
R"rs(
pub fn r#$field$(&self) -> Option<$Scalar$> {
if !unsafe { $hazzer_thunk$(self.msg) } {
return None;
}
Some(unsafe { $getter_thunk$(self.msg) })
}
$getter$
$getter_opt$
pub fn $field$_set(&mut self, val: Option<$Scalar$>) {
match val {
Some(val) => unsafe { $setter_thunk$(self.msg, val) },

Loading…
Cancel
Save