Skip to content

Commit

Permalink
Sketch of v0.6 SingularMessageField
Browse files Browse the repository at this point in the history
This CL sets up the basic plumbing end-to-end for singular message fields.
We add skeletonized support for `Proxied` messages. This is done
by creating structs for $Msg$View and $Msg$Mut, and providing
stubbed impls.

PiperOrigin-RevId: 552609955
  • Loading branch information
honglooker authored and copybara-github committed Jul 31, 2023
1 parent 1ce24c5 commit 87e6b7f
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use std::ptr::{self, NonNull};
/// dropped.
///
/// Note that this type is neither `Sync` nor `Send`.
#[derive(Debug)]
pub struct Arena {
#[allow(dead_code)]
ptr: NonNull<u8>,
Expand Down
10 changes: 10 additions & 0 deletions rust/test/shared/accessors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,13 @@ fn test_optional_bytes_accessors() {
msg.optional_bytes_set(Some(b""));
assert_eq!(msg.optional_bytes().unwrap(), b"");
}

#[test]
#[should_panic = "b/285309454"]
#[allow(unreachable_code)]
fn test_singular_msg_field() {
let msg = TestAllTypes::new();
// TODO("b/285309454"): fetch the inner integer `bb`
// call should look like msg.optional_nested_message().bb()
match msg.optional_nested_message() {}
}
1 change: 1 addition & 0 deletions rust/upb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct RawArenaData {
/// dropped.
///
/// Note that this type is neither `Sync` nor `Send`.
#[derive(Debug)]
pub struct Arena {
// Safety invariant: this must always be a valid arena
raw: RawArena,
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cc_library(
srcs = [
"accessors/accessors.cc",
"accessors/singular_bytes.cc",
"accessors/singular_message.cc",
"accessors/singular_scalar.cc",
],
hdrs = ["accessors/accessors.h"],
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/rust/accessors/accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ std::unique_ptr<AccessorGenerator> AccessorGenerator::For(
case FieldDescriptor::TYPE_BYTES:
if (field.desc().is_repeated()) return nullptr;
return ForSingularBytes(field);
case FieldDescriptor::TYPE_MESSAGE:
if (field.desc().is_repeated()) return nullptr;
return ForSingularMessage(field);

default:
return nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/rust/accessors/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class AccessorGenerator {
Context<FieldDescriptor> field);
static std::unique_ptr<AccessorGenerator> ForSingularBytes(
Context<FieldDescriptor> field);
static std::unique_ptr<AccessorGenerator> ForSingularMessage(
Context<FieldDescriptor> field);
};

} // namespace rust
Expand Down
82 changes: 82 additions & 0 deletions src/google/protobuf/compiler/rust/accessors/singular_message.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/rust/accessors/accessors.h"
#include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/compiler/rust/naming.h"
#include "google/protobuf/descriptor.h"

namespace google {
namespace protobuf {
namespace compiler {
namespace rust {
namespace {
class SingularMessage final : public AccessorGenerator {
public:
~SingularMessage() override = default;

void InMsgImpl(Context<FieldDescriptor> field) const override {
field.Emit(
{
{"field", field.desc().name()},
},
R"rs(
// inMsgImpl
pub fn $field$(&self) -> std::convert::Infallible {
todo!("b/285309454")
}
)rs");
}

void InExternC(Context<FieldDescriptor> field) const override {
field.Emit({},
R"rs(
// inExternC
)rs");
}

void InThunkCc(Context<FieldDescriptor> field) const override {
field.Emit({},
R"cc(
// inThunkCC
)cc");
}
};
} // namespace

std::unique_ptr<AccessorGenerator> AccessorGenerator::ForSingularMessage(
Context<FieldDescriptor> field) {
return std::make_unique<SingularMessage>();
}
} // namespace rust
} // namespace compiler
} // namespace protobuf
} // namespace google
51 changes: 51 additions & 0 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,61 @@ void MessageGenerator::GenerateRs(Context<Descriptor> msg) {
},
R"rs(
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub struct $Msg$ {
$Msg.fields$
}
unsafe impl Sync for $Msg$ {}
unsafe impl Sync for $Msg$View<'_> {}
unsafe impl Send for $Msg$View<'_> {}
impl $pb$::Proxied for $Msg$ {
type View<'a> = $Msg$View<'a>;
type Mut<'a> = $Msg$Mut<'a>;
}
#[derive(Debug, Copy, Clone)]
pub struct $Msg$View<'a> {
msg: $NonNull$<u8>,
_phantom: std::marker::PhantomData<&'a ()>,
}
impl<'a> $pb$::ViewProxy<'a> for $Msg$View<'a> {
type Proxied = $Msg$;
fn as_view(&self) -> $pb$::View<'a, $Msg$> {
todo!("b/285309454")
}
fn into_view<'shorter>(self) -> $pb$::View<'shorter, $Msg$> where 'a: 'shorter { todo!("b/285309454") }
}
impl<'a> $pb$::SettableValue<$Msg$> for $Msg$View<'a> {
fn set_on(self, _private: $pb$::__internal::Private, _mutator: $pb$::Mut<$Msg$>) {
todo!("b/285309454")
}
}
#[derive(Debug)]
pub struct $Msg$Mut<'a> {
_phantom: std::marker::PhantomData<&'a mut ()>,
}
impl<'a> $pb$::MutProxy<'a> for $Msg$Mut<'a> {
fn as_mut(&mut self) -> $pb$::Mut<'_, $Msg$> {
todo!("b/285309454")
}
fn into_mut<'shorter>(self) -> $pb$::Mut<'shorter, $Msg$> where 'a : 'shorter { todo!("b/285309454") }
}
impl<'a> $pb$::ViewProxy<'a> for $Msg$Mut<'a> {
type Proxied = $Msg$;
fn as_view(&self) -> $pb$::View<'_, $Msg$> {
todo!("b/285309454")
}
fn into_view<'shorter>(self) -> $pb$::View<'shorter, $Msg$> where 'a: 'shorter { todo!("b/285309454") }
}
impl $Msg$ {
pub fn new() -> Self {
$Msg::new$
Expand Down

0 comments on commit 87e6b7f

Please sign in to comment.