From f78a58948178ec81cd91cfa7c3cb3e090102a697 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 12 Nov 2024 06:12:40 -0800 Subject: [PATCH] Improve rustdoc in Rust OSS PiperOrigin-RevId: 695705103 --- .../Reflection/FeatureSetDescriptor.g.cs | 17 ------- rust/protobuf.rs | 29 ++++++++---- rust/shared.rs | 44 +++++++------------ rust/test/shared/BUILD | 24 ++++++++++ rust/test/shared/no_internal_access_test.rs | 11 +++++ 5 files changed, 73 insertions(+), 52 deletions(-) delete mode 100644 csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs create mode 100644 rust/test/shared/no_internal_access_test.rs diff --git a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs b/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs deleted file mode 100644 index 208ce1fcb6..0000000000 --- a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs +++ /dev/null @@ -1,17 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd -#endregion - -namespace Google.Protobuf.Reflection; - -internal sealed partial class FeatureSetDescriptor -{ - // Canonical serialized form of the edition defaults, generated by embed_edition_defaults. - private const string DefaultsBase64 = - "ChMYhAciACoMCAEQAhgCIAMoATACChMY5wciACoMCAIQARgBIAIoATABChMY6AciDAgBEAEYASACKAEwASoAIOYHKOgH"; -} diff --git a/rust/protobuf.rs b/rust/protobuf.rs index 13d0b5f14d..172739a365 100644 --- a/rust/protobuf.rs +++ b/rust/protobuf.rs @@ -7,12 +7,13 @@ //! Rust Protobuf Runtime //! -//! This file forwards to `shared.rs`, which exports a kernel-specific -//! `__runtime`. Rust Protobuf gencode actually depends directly on kernel -//! specific crates. This crate exists for two reasons: -//! - To be able to use `protobuf` as a crate name for both cpp and upb kernels -//! from user code. -//! - To make it more difficult to access internal-only items by default. +//! This file exists as the public entry point for the Rust Protobuf runtime. It +//! is a thin re-export of the `shared.rs` file but is needed for two reasons: +//! - To create a single `protobuf` crate name for either cpp and upb kernels +//! from user code (toggled at compile time). +//! - Blocks the __internal and __runtime modules from being re-exported to +//! application code, unless they use one of our visibility-restricted targets +//! (gencode does have access to them). #[cfg(cpp_kernel)] use protobuf_cpp as kernel; @@ -20,6 +21,18 @@ use protobuf_cpp as kernel; #[cfg(upb_kernel)] use protobuf_upb as kernel; -pub use kernel::__public::*; +/// Block these two mods from being re-exported by the `pub use` +/// below (glob use automatically only adds things that aren't otherwise +/// defined). +/// +/// By creating a const instead of an empty mod it is easier to have a test +/// that confirms this targeted 'blocking' is working as intended. +#[doc(hidden)] +#[allow(non_upper_case_globals)] +pub const __internal: () = (); -pub use kernel::prelude; +#[doc(hidden)] +#[allow(non_upper_case_globals)] +pub const __runtime: () = (); + +pub use kernel::*; diff --git a/rust/shared.rs b/rust/shared.rs index 6644fdf022..ff8c88a94e 100644 --- a/rust/shared.rs +++ b/rust/shared.rs @@ -18,33 +18,23 @@ use std::fmt; // This problem is referred to as "perfect derive". // https://smallcultfollowing.com/babysteps/blog/2022/04/12/implied-bounds-and-perfect-derive/ -/// Everything in `__public` is re-exported in `protobuf.rs`. -/// These are the items protobuf users can access directly. -#[doc(hidden)] -pub mod __public { - pub use crate::codegen_traits::{ - create::Parse, - interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop}, - read::Serialize, - write::{Clear, ClearAndParse, MergeFrom}, - Message, MessageMut, MessageView, - }; - pub use crate::cord::{ProtoBytesCow, ProtoStringCow}; - pub use crate::r#enum::{Enum, UnknownEnumValue}; - pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue}; - pub use crate::optional::Optional; - pub use crate::proto; - pub use crate::proxied::{ - AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, - View, ViewProxy, - }; - pub use crate::repeated::{ - ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView, - }; - pub use crate::string::{ProtoBytes, ProtoStr, ProtoString, Utf8Error}; - pub use crate::{ParseError, SerializeError}; -} -pub use __public::*; +pub use crate::codegen_traits::{ + create::Parse, + interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop}, + read::Serialize, + write::{Clear, ClearAndParse, MergeFrom}, + Message, MessageMut, MessageView, +}; +pub use crate::cord::{ProtoBytesCow, ProtoStringCow}; +pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue}; +pub use crate::optional::Optional; +pub use crate::proxied::{ + AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, View, + ViewProxy, +}; +pub use crate::r#enum::{Enum, UnknownEnumValue}; +pub use crate::repeated::{ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView}; +pub use crate::string::{ProtoBytes, ProtoStr, ProtoString, Utf8Error}; pub mod prelude; diff --git a/rust/test/shared/BUILD b/rust/test/shared/BUILD index 1261693cd9..90ddf5b4b8 100644 --- a/rust/test/shared/BUILD +++ b/rust/test/shared/BUILD @@ -517,3 +517,27 @@ rust_test( "@crate_index//:googletest", ], ) + +rust_test( + name = "no_internal_access_test_cpp", + srcs = ["no_internal_access_test.rs"], + aliases = { + "//rust:protobuf_cpp_export": "protobuf", + }, + deps = [ + "//rust:protobuf_cpp_export", + "@crate_index//:googletest", + ], +) + +rust_test( + name = "no_internal_access_test_upb", + srcs = ["no_internal_access_test.rs"], + aliases = { + "//rust:protobuf_upb_export": "protobuf", + }, + deps = [ + "//rust:protobuf_upb_export", + "@crate_index//:googletest", + ], +) diff --git a/rust/test/shared/no_internal_access_test.rs b/rust/test/shared/no_internal_access_test.rs new file mode 100644 index 0000000000..e110b86483 --- /dev/null +++ b/rust/test/shared/no_internal_access_test.rs @@ -0,0 +1,11 @@ +use googletest::gtest; +use protobuf::{__internal, __runtime}; + +#[gtest] +#[allow(clippy::unit_cmp)] +fn test_no_internal_access() { + // This test is to ensure that the `__internal` and `__runtime` mods are + // 'blocked' by instead being a unit type instead of a module. + assert_eq!(__internal, ()); + assert_eq!(__runtime, ()); +}