Bring back 'flat' setters for singular scalars and strings.

The non-string field setters can bypass the vtables already, the string setters still go through the vtable path here because its more important to let them take an `impl SettableValue` to be able to set either a &str or a &ProtoStr already.

PiperOrigin-RevId: 609705233
pull/15896/head
Protobuf Team Bot 11 months ago committed by Copybara-Service
parent 4395d97f66
commit 5a0135e65a
  1. 28
      rust/test/cpp/interop/main.rs
  2. 68
      rust/test/shared/accessors_proto3_test.rs
  3. 54
      rust/test/shared/accessors_test.rs
  4. 12
      rust/test/shared/serialization_test.rs
  5. 32
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc
  6. 54
      src/google/protobuf/compiler/rust/accessors/singular_string.cc

@ -40,9 +40,9 @@ fn mutate_message_in_cpp() {
}
let mut msg2 = TestAllTypes::new();
msg2.optional_int64_mut().set(42);
msg2.optional_bytes_mut().set(b"something mysterious");
msg2.optional_bool_mut().set(false);
msg2.set_optional_int64(42);
msg2.set_optional_bytes(b"something mysterious");
msg2.set_optional_bool(false);
proto_assert_eq!(msg1, msg2);
}
@ -56,9 +56,9 @@ fn mutate_message_mut_in_cpp() {
}
let mut msg2 = TestAllTypes::new();
msg2.optional_int64_mut().set(42);
msg2.optional_bytes_mut().set(b"something mysterious");
msg2.optional_bool_mut().set(false);
msg2.set_optional_int64(42);
msg2.set_optional_bytes(b"something mysterious");
msg2.set_optional_bool(false);
proto_assert_eq!(msg1, msg2);
}
@ -66,8 +66,8 @@ fn mutate_message_mut_in_cpp() {
#[test]
fn deserialize_in_rust() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
msg1.set_optional_int64(-1);
msg1.set_optional_bytes(b"some cool data I guess");
let serialized = unsafe {
SerializeTestAllTypes(msg1.as_view().__unstable_cpp_repr_grant_permission_to_break())
};
@ -80,8 +80,8 @@ fn deserialize_in_rust() {
#[test]
fn deserialize_in_cpp() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
msg1.set_optional_int64(-1);
msg1.set_optional_bytes(b"some cool data I guess");
let data = msg1.serialize();
let msg2 = unsafe {
@ -97,8 +97,8 @@ fn deserialize_in_cpp() {
#[test]
fn deserialize_in_cpp_into_mut() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
msg1.set_optional_int64(-1);
msg1.set_optional_bytes(b"some cool data I guess");
let data = msg1.serialize();
let mut raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };
@ -115,8 +115,8 @@ fn deserialize_in_cpp_into_mut() {
#[test]
fn deserialize_in_cpp_into_view() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
msg1.set_optional_int64(-1);
msg1.set_optional_bytes(b"some cool data I guess");
let data = msg1.serialize();
let raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };

@ -19,13 +19,17 @@ fn test_fixed32_accessors() {
assert_that!(msg.optional_fixed32(), eq(0));
assert_that!(msg.optional_fixed32_mut().get(), eq(0));
msg.optional_fixed32_mut().set(42);
msg.set_optional_fixed32(42);
assert_that!(msg.optional_fixed32_mut().get(), eq(42));
assert_that!(msg.optional_fixed32(), eq(42));
msg.optional_fixed32_mut().set(u32::default());
msg.set_optional_fixed32(u32::default());
assert_that!(msg.optional_fixed32(), eq(0));
assert_that!(msg.optional_fixed32_mut().get(), eq(0));
msg.optional_fixed32_mut().set(43);
assert_that!(msg.optional_fixed32(), eq(43));
assert_that!(msg.optional_fixed32_mut().get(), eq(43));
}
#[test]
@ -34,13 +38,17 @@ fn test_bool_accessors() {
assert_that!(msg.optional_bool(), eq(false));
assert_that!(msg.optional_bool_mut().get(), eq(false));
msg.optional_bool_mut().set(true);
msg.set_optional_bool(true);
assert_that!(msg.optional_bool(), eq(true));
assert_that!(msg.optional_bool_mut().get(), eq(true));
msg.optional_bool_mut().set(bool::default());
msg.set_optional_bool(bool::default());
assert_that!(msg.optional_bool(), eq(false));
assert_that!(msg.optional_bool_mut().get(), eq(false));
msg.optional_bool_mut().set(true);
assert_that!(msg.optional_bool(), eq(true));
assert_that!(msg.optional_bool_mut().get(), eq(true));
}
#[test]
@ -51,13 +59,13 @@ fn test_bytes_accessors() {
assert_that!(*msg.optional_bytes(), empty());
assert_that!(*msg.optional_bytes_mut().get(), empty());
msg.optional_bytes_mut().set(b"accessors_test");
msg.set_optional_bytes(b"accessors_test");
assert_that!(msg.optional_bytes(), eq(b"accessors_test"));
assert_that!(msg.optional_bytes_mut().get(), eq(b"accessors_test"));
{
let s = Vec::from(&b"hello world"[..]);
msg.optional_bytes_mut().set(&s[..]);
msg.set_optional_bytes(&s[..]);
}
assert_that!(msg.optional_bytes(), eq(b"hello world"));
assert_that!(msg.optional_bytes_mut().get(), eq(b"hello world"));
@ -66,7 +74,7 @@ fn test_bytes_accessors() {
assert_that!(*msg.optional_bytes(), empty());
assert_that!(*msg.optional_bytes_mut().get(), empty());
msg.optional_bytes_mut().set(b"");
msg.set_optional_bytes(b"");
assert_that!(*msg.optional_bytes(), empty());
assert_that!(*msg.optional_bytes_mut().get(), empty());
}
@ -81,7 +89,7 @@ fn test_optional_bytes_accessors() {
{
let s = Vec::from(&b"hello world"[..]);
msg.optional_bytes_mut().set(&s[..]);
msg.set_optional_bytes(&s[..]);
}
assert_that!(msg.optional_bytes(), eq(b"hello world"));
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b"hello world"[..])));
@ -100,7 +108,7 @@ fn test_optional_bytes_accessors() {
assert_that!(msg.optional_bytes_opt(), eq(Optional::Unset(&b""[..])));
assert_that!(msg.optional_bytes_mut(), is_unset());
msg.optional_bytes_mut().set(b"");
msg.set_optional_bytes(b"");
assert_that!(*msg.optional_bytes(), empty());
assert_that!(msg.optional_bytes_opt(), eq(Optional::Set(&b""[..])));
@ -125,13 +133,13 @@ fn test_string_accessors() {
assert_that!(*msg.optional_string().as_bytes(), empty());
assert_that!(*msg.optional_string_mut().get().as_bytes(), empty());
msg.optional_string_mut().set("accessors_test");
msg.set_optional_string("accessors_test");
assert_that!(msg.optional_string(), eq("accessors_test"));
assert_that!(msg.optional_string_mut().get(), eq("accessors_test"));
{
let s = String::from("hello world");
msg.optional_string_mut().set(&s[..]);
msg.set_optional_string(&s[..]);
}
assert_that!(msg.optional_string(), eq("hello world"));
assert_that!(msg.optional_string_mut().get(), eq("hello world"));
@ -140,7 +148,7 @@ fn test_string_accessors() {
assert_that!(*msg.optional_string().as_bytes(), empty());
assert_that!(*msg.optional_string_mut().get().as_bytes(), empty());
msg.optional_string_mut().set("");
msg.set_optional_string("");
assert_that!(*msg.optional_string().as_bytes(), empty());
assert_that!(*msg.optional_string_mut().get().as_bytes(), empty());
}
@ -155,7 +163,7 @@ fn test_optional_string_accessors() {
{
let s = String::from("hello world");
msg.optional_string_mut().set(&s[..]);
msg.set_optional_string(&s[..]);
}
assert_that!(msg.optional_string(), eq("hello world"));
assert_that!(msg.optional_string_opt(), eq(Optional::Set("hello world".into())));
@ -174,7 +182,7 @@ fn test_optional_string_accessors() {
assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
assert_that!(msg.optional_string_mut(), is_unset());
msg.optional_string_mut().set("");
msg.set_optional_string("");
assert_that!(*msg.optional_string().as_bytes(), empty());
assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));
@ -192,11 +200,11 @@ fn test_nested_enum_accessors() {
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Zero));
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Zero));
msg.optional_nested_enum_mut().set(NestedEnum::Baz);
msg.set_optional_nested_enum(NestedEnum::Baz);
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Baz));
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Baz));
msg.optional_nested_enum_mut().set(NestedEnum::default());
msg.set_optional_nested_enum(NestedEnum::default());
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Zero));
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Zero));
}
@ -210,12 +218,12 @@ fn test_optional_nested_enum_accessors() {
assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Unspecified)));
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Unspecified));
msg.optional_nested_enum_mut().set(NestedEnum::Baz);
msg.set_optional_nested_enum(NestedEnum::Baz);
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Baz));
assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Set(NestedEnum::Baz)));
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Baz));
msg.optional_nested_enum_mut().set(NestedEnum::default());
msg.set_optional_nested_enum(NestedEnum::default());
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Unspecified));
assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Set(NestedEnum::Unspecified)));
assert_that!(msg.optional_nested_enum_mut().get(), eq(NestedEnum::Unspecified));
@ -241,11 +249,11 @@ fn test_foreign_enum_accessors() {
assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignZero));
assert_that!(msg.optional_foreign_enum_mut().get(), eq(ForeignEnum::ForeignZero));
msg.optional_foreign_enum_mut().set(ForeignEnum::ForeignBaz);
msg.set_optional_foreign_enum(ForeignEnum::ForeignBaz);
assert_that!(msg.optional_foreign_enum_mut().get(), eq(ForeignEnum::ForeignBaz));
assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignBaz));
msg.optional_foreign_enum_mut().set(ForeignEnum::default());
msg.set_optional_foreign_enum(ForeignEnum::default());
assert_that!(msg.optional_foreign_enum(), eq(ForeignEnum::ForeignZero));
assert_that!(msg.optional_foreign_enum_mut().get(), eq(ForeignEnum::ForeignZero));
}
@ -257,11 +265,11 @@ fn test_oneof_accessors() {
let mut msg = TestAllTypes::new();
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
msg.oneof_uint32_mut().set(7);
msg.set_oneof_uint32(7);
assert_that!(msg.oneof_uint32_opt(), eq(Optional::Set(7)));
assert_that!(msg.oneof_field(), matches_pattern!(OneofUint32(eq(7))));
msg.oneof_uint32_mut().clear();
msg.set_oneof_uint32_opt(None);
assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
@ -275,8 +283,8 @@ fn test_oneof_accessors() {
// eq(Optional::Unset(_))); assert_that!(msg.oneof_field(),
// matches_pattern!(OneofNestedMessage(_)));
msg.oneof_uint32_mut().set(7);
msg.oneof_bytes_mut().set(b"123");
msg.set_oneof_uint32(7);
msg.set_oneof_bytes(b"123");
assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.oneof_field(), matches_pattern!(OneofBytes(eq(b"123"))));
@ -295,7 +303,7 @@ fn test_oneof_enum_accessors() {
assert_that!(msg.foo_enum_opt(), eq(Optional::Unset(NestedEnum::Unknown)));
assert_that!(msg.foo(), matches_pattern!(Foo::not_set(_)));
msg.foo_enum_mut().set(NestedEnum::Bar);
msg.set_foo_enum(NestedEnum::Bar);
assert_that!(msg.foo_enum_opt(), eq(Optional::Set(NestedEnum::Bar)));
assert_that!(msg.foo(), matches_pattern!(Foo::FooEnum(eq(NestedEnum::Bar))));
}
@ -307,7 +315,7 @@ fn test_oneof_mut_accessors() {
let mut msg = TestAllTypes::new();
assert_that!(msg.oneof_field_mut(), matches_pattern!(not_set(_)));
msg.oneof_uint32_mut().set(7);
msg.set_oneof_uint32(7);
match msg.oneof_field_mut() {
OneofUint32(mut v) => {
@ -326,11 +334,11 @@ fn test_oneof_mut_accessors() {
matches_pattern!(TestAllTypes_::OneofField::OneofUint32(eq(8)))
);
msg.oneof_uint32_mut().clear();
msg.set_oneof_uint32_opt(None);
assert_that!(msg.oneof_field_mut(), matches_pattern!(not_set(_)));
msg.oneof_uint32_mut().set(7);
msg.oneof_bytes_mut().set(b"123");
msg.set_oneof_uint32_opt(Some(7));
msg.set_oneof_bytes(b"123");
assert_that!(msg.oneof_field_mut(), matches_pattern!(OneofBytes(_)));
}
@ -345,7 +353,7 @@ fn test_oneof_mut_enum_accessors() {
assert_that!(msg.foo_enum_opt(), eq(Optional::Unset(NestedEnum::Unknown)));
assert_that!(msg.foo_mut(), matches_pattern!(FooMut::not_set(_)));
msg.foo_enum_mut().set(NestedEnum::Bar);
msg.set_foo_enum(NestedEnum::Bar);
assert_that!(msg.foo_enum_opt(), eq(Optional::Set(NestedEnum::Bar)));
assert_that!(msg.foo_mut(), matches_pattern!(FooMut::FooEnum(_)));
}

@ -50,6 +50,18 @@ fn test_optional_fixed32_accessors() {
msg.optional_fixed32_mut().clear();
assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_fixed32(), eq(0));
msg.set_optional_fixed32(7);
assert_that!(msg.optional_fixed32_opt(), eq(Optional::Set(7)));
assert_that!(msg.optional_fixed32(), eq(7));
msg.set_optional_fixed32_opt(Some(8));
assert_that!(msg.optional_fixed32_opt(), eq(Optional::Set(8)));
assert_that!(msg.optional_fixed32(), eq(8));
msg.set_optional_fixed32_opt(None);
assert_that!(msg.optional_fixed32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_fixed32(), eq(0));
}
#[test]
@ -60,6 +72,12 @@ fn test_default_fixed32_accessors() {
assert_that!(msg.default_fixed32_mut().is_set(), eq(false));
assert_that!(msg.default_fixed32_opt(), eq(Optional::Unset(47)));
msg.set_default_fixed32(7);
assert_that!(msg.default_fixed32(), eq(7));
assert_that!(msg.default_fixed32_mut().get(), eq(7));
assert_that!(msg.default_fixed32_mut().is_set(), eq(true));
assert_that!(msg.default_fixed32_opt(), eq(Optional::Set(7)));
msg.default_fixed32_mut().set(999);
assert_that!(msg.default_fixed32(), eq(999));
assert_that!(msg.default_fixed32_mut().get(), eq(999));
@ -85,6 +103,10 @@ fn test_optional_fixed64_accessors() {
assert_that!(msg.optional_fixed64_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_fixed64(), eq(0));
msg.set_optional_fixed64(99);
assert_that!(msg.optional_fixed64_opt(), eq(Optional::Set(99)));
assert_that!(msg.optional_fixed64(), eq(99));
msg.optional_fixed64_mut().set(2000);
assert_that!(msg.optional_fixed64_opt(), eq(Optional::Set(2000)));
assert_that!(msg.optional_fixed64(), eq(2000));
@ -102,6 +124,12 @@ fn test_default_fixed64_accessors() {
assert_that!(msg.default_fixed64_mut().is_set(), eq(false));
assert_that!(msg.default_fixed64_opt(), eq(Optional::Unset(48)));
msg.set_default_fixed64(4);
assert_that!(msg.default_fixed64(), eq(4));
assert_that!(msg.default_fixed64_mut().get(), eq(4));
assert_that!(msg.default_fixed64_mut().is_set(), eq(true));
assert_that!(msg.default_fixed64_opt(), eq(Optional::Set(4)));
msg.default_fixed64_mut().set(999);
assert_that!(msg.default_fixed64(), eq(999));
assert_that!(msg.default_fixed64_mut().get(), eq(999));
@ -127,6 +155,10 @@ fn test_optional_int32_accessors() {
assert_that!(msg.optional_int32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.optional_int32(), eq(0));
msg.set_optional_int32(0);
assert_that!(msg.optional_int32_opt(), eq(Optional::Set(0)));
assert_that!(msg.optional_int32(), eq(0));
msg.optional_int32_mut().set(1);
assert_that!(msg.optional_int32_opt(), eq(Optional::Set(1)));
assert_that!(msg.optional_int32(), eq(1));
@ -144,6 +176,18 @@ fn test_default_int32_accessors() {
assert_that!(msg.default_int32_mut().is_set(), eq(false));
assert_that!(msg.default_int32_opt(), eq(Optional::Unset(41)));
msg.set_default_int32_opt(Some(41));
assert_that!(msg.default_int32(), eq(41));
assert_that!(msg.default_int32_mut().get(), eq(41));
assert_that!(msg.default_int32_mut().is_set(), eq(true));
assert_that!(msg.default_int32_opt(), eq(Optional::Set(41)));
msg.set_default_int32_opt(None);
assert_that!(msg.default_int32(), eq(41));
assert_that!(msg.default_int32_mut().get(), eq(41));
assert_that!(msg.default_int32_mut().is_set(), eq(false));
assert_that!(msg.default_int32_opt(), eq(Optional::Unset(41)));
msg.default_int32_mut().set(999);
assert_that!(msg.default_int32(), eq(999));
assert_that!(msg.default_int32_mut().get(), eq(999));
@ -571,7 +615,7 @@ fn test_nonempty_default_bytes_accessors() {
assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
assert_that!(msg.default_bytes_mut(), is_unset());
msg.default_bytes_mut().set(b"");
msg.set_default_bytes(b"");
assert_that!(*msg.default_bytes(), empty());
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b""[..])));
@ -580,6 +624,10 @@ fn test_nonempty_default_bytes_accessors() {
assert_that!(msg.default_bytes(), eq(b"world"));
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"world"[..])));
msg.set_default_bytes_opt(None::<&[u8]>);
assert_that!(msg.default_bytes_opt(), eq(Optional::Unset(&b"world"[..])));
assert_that!(msg.default_bytes_mut(), is_unset());
msg.default_bytes_mut().or_default().set(b"\xffbinary\x85non-utf8");
assert_that!(msg.default_bytes(), eq(b"\xffbinary\x85non-utf8"));
assert_that!(msg.default_bytes_opt(), eq(Optional::Set(&b"\xffbinary\x85non-utf8"[..])));
@ -617,11 +665,11 @@ fn test_optional_string_accessors() {
assert_that!(msg.optional_string_opt(), eq(Optional::Unset("".into())));
assert_that!(msg.optional_string_mut(), is_unset());
msg.optional_string_mut().set("");
msg.set_optional_string("");
assert_that!(msg.optional_string(), eq(""));
assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));
msg.optional_string_mut().clear();
msg.set_optional_string_opt(None::<&str>);
msg.optional_string_mut().or_default();
assert_that!(msg.optional_string(), eq(""));
assert_that!(msg.optional_string_opt(), eq(Optional::Set("".into())));

@ -25,9 +25,9 @@ fn serialize_zero_length() {
#[test]
fn serialize_deserialize_message() {
let mut msg = TestAllTypes::new();
msg.optional_int64_mut().set(42);
msg.optional_bool_mut().set(true);
msg.optional_bytes_mut().set(b"serialize deserialize test");
msg.set_optional_int64(42);
msg.set_optional_bool(true);
msg.set_optional_bytes(b"serialize deserialize test");
let serialized = msg.serialize();
@ -55,9 +55,9 @@ fn deserialize_error() {
#[test]
fn set_bytes_with_serialized_data() {
let mut msg = TestAllTypes::new();
msg.optional_int64_mut().set(42);
msg.optional_bool_mut().set(true);
msg.set_optional_int64(42);
msg.set_optional_bool(true);
let mut msg2 = TestAllTypes::new();
msg2.optional_bytes_mut().set(msg.serialize());
msg2.set_optional_bytes(msg.serialize());
assert_that!(msg2.optional_bytes(), eq(msg.serialize().as_ref()));
}

@ -26,6 +26,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
ctx.Emit(
{
{"field", RsSafeName(field.name())},
{"raw_field_name", field.name()}, // Never r# prefixed
{"view_self", ViewReceiver(accessor_case)},
{"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")},
@ -43,7 +44,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
if (!field.is_optional()) return;
if (!field.has_presence()) return;
ctx.Emit(R"rs(
pub fn $field$_opt($view_self$) -> $pb$::Optional<$Scalar$> {
pub fn $raw_field_name$_opt($view_self$) -> $pb$::Optional<$Scalar$> {
if !unsafe { $hazzer_thunk$(self.raw_msg()) } {
return $pb$::Optional::Unset($default_value$);
}
@ -52,6 +53,29 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
}},
{"setter",
[&] {
ctx.Emit({}, R"rs(
pub fn set_$raw_field_name$(&mut self, val: $Scalar$) {
unsafe { $setter_thunk$(self.raw_msg(), val) }
}
)rs");
}},
{"setter_opt",
[&] {
if (field.has_presence()) {
ctx.Emit({}, R"rs(
pub fn set_$raw_field_name$_opt(&mut self, val: Option<$Scalar$>) {
unsafe {
match val {
Some(val) => $setter_thunk$(self.raw_msg(), val),
None => $clearer_thunk$(self.raw_msg()),
}
}
}
)rs");
}
}},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")},
{"clearer_thunk", ThunkName(ctx, field, "clear")},
@ -90,7 +114,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
if (field.has_presence()) {
ctx.Emit(R"rs(
pub fn $field$_mut(&mut self) -> $pb$::FieldEntry<'_, $Scalar$> {
pub fn $raw_field_name$_mut(&mut self) -> $pb$::FieldEntry<'_, $Scalar$> {
unsafe {
let has = $hazzer_thunk$(self.raw_msg());
$pbi$::new_vtable_field_entry::<$Scalar$>(
@ -104,7 +128,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
)rs");
} else {
ctx.Emit(R"rs(
pub fn $field$_mut(&mut self) -> $pb$::Mut<'_, $Scalar$> {
pub fn $raw_field_name$_mut(&mut self) -> $pb$::Mut<'_, $Scalar$> {
// SAFETY:
// - The message is valid for the output lifetime.
// - The vtable is valid for the field.
@ -128,6 +152,8 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
R"rs(
$getter$
$getter_opt$
$setter$
$setter_opt$
$vtable$
$getter_mut$
)rs");

@ -24,6 +24,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
ctx.Emit(
{
{"field", RsSafeName(field.name())},
{"raw_field_name", field.name()},
{"hazzer_thunk", ThunkName(ctx, field, "has")},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")},
@ -55,12 +56,19 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)},
{"field_optional_getter",
{"getter",
[&] {
ctx.Emit(R"rs(
pub fn $field$($view_self$) -> &$view_lifetime$ $proxied_type$ {
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$transform_view$
})rs");
}},
{"getter_opt",
[&] {
if (!field.is_optional()) return;
if (!field.has_presence()) return;
ctx.Emit(R"rs(
pub fn $field$_opt($view_self$) -> $pb$::Optional<&$view_lifetime$ $proxied_type$> {
pub fn $raw_field_name$_opt($view_self$) -> $pb$::Optional<&$view_lifetime$ $proxied_type$> {
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$pb$::Optional::new(
$transform_view$,
@ -69,6 +77,32 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
}},
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) return;
ctx.Emit(R"rs(
pub fn set_$raw_field_name$(&mut self, val: impl $pb$::SettableValue<$proxied_type$>) {
//~ TODO: Optimize this to not go through the
//~ FieldEntry.
self.$raw_field_name$_mut().set(val);
}
)rs");
}},
{"setter_opt",
[&] {
if (accessor_case == AccessorCase::VIEW) return;
if (!field.has_presence()) return;
ctx.Emit(R"rs(
pub fn set_$raw_field_name$_opt(&mut self, val: Option<impl $pb$::SettableValue<$proxied_type$>>) {
//~ TODO: Optimize this to not go through the
//~ FieldEntry.
match val {
Some(val) => self.$raw_field_name$_mut().set(val),
None => self.$raw_field_name$_mut().clear(),
}
}
)rs");
}},
{"vtable_name", VTableName(field)},
{"vtable",
[&] {
@ -107,7 +141,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
if (field.has_presence()) {
ctx.Emit(R"rs(
pub fn $field$_mut(&mut self) -> $pb$::FieldEntry<'_, $proxied_type$> {
pub fn $raw_field_name$_mut(&mut self) -> $pb$::FieldEntry<'_, $proxied_type$> {
let out = unsafe {
let has = $hazzer_thunk$(self.raw_msg());
$pbi$::new_vtable_field_entry(
@ -122,7 +156,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
)rs");
} else {
ctx.Emit(R"rs(
pub fn $field$_mut(&mut self) -> $pb$::Mut<'_, $proxied_type$> {
pub fn $raw_field_name$_mut(&mut self) -> $pb$::Mut<'_, $proxied_type$> {
unsafe {
<$pb$::Mut<$proxied_type$>>::from_inner(
$pbi$::Private,
@ -139,12 +173,10 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}},
},
R"rs(
pub fn $field$($view_self$) -> &$view_lifetime$ $proxied_type$ {
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$transform_view$
}
$field_optional_getter$
$getter$
$getter_opt$
$setter$
$setter_opt$
$vtable$
$field_mutator_getter$
)rs");

Loading…
Cancel
Save