Skip to content

Commit

Permalink
Move __runtime into __internal::runtime
Browse files Browse the repository at this point in the history
Clarify the rustdoc on __internal

PiperOrigin-RevId: 703541883
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 6, 2024
1 parent f10826e commit b025398
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 42 deletions.
13 changes: 10 additions & 3 deletions rust/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ pub use crate::ProtoStr;
use crate::Proxied;
pub use std::fmt::Debug;

#[cfg(all(bzl, cpp_kernel))]
#[path = "cpp.rs"]
pub mod runtime;
#[cfg(any(not(bzl), upb_kernel))]
#[path = "upb.rs"]
pub mod runtime;

// TODO: Temporarily re-export these symbols which are now under
// __runtime under __internal since some external callers using it through
// __internal.
pub use crate::__runtime::{PtrAndLen, RawMap, RawMessage, RawRepeatedField};
// runtime under __internal directly since some external callers using it
// through __internal.
pub use runtime::{PtrAndLen, RawMap, RawMessage, RawRepeatedField};

/// Used to protect internal-only items from being used accidentally.
#[derive(Debug)]
Expand Down
14 changes: 11 additions & 3 deletions rust/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use crate::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, MutProxied, MutProxy, Proxied, Proxy, View,
ViewProxy,
__internal::runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
__internal::{Private, SealedInternal},
__runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
};
use std::marker::PhantomData;

Expand Down Expand Up @@ -104,7 +104,11 @@ where
}

impl<K: Proxied, V: ProxiedInMapValue<K>> Proxied for Map<K, V> {
type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg;
type View<'msg>
= MapView<'msg, K, V>
where
K: 'msg,
V: 'msg;
}

impl<K: Proxied, V: ProxiedInMapValue<K>> AsView for Map<K, V> {
Expand All @@ -116,7 +120,11 @@ impl<K: Proxied, V: ProxiedInMapValue<K>> AsView for Map<K, V> {
}

impl<K: Proxied, V: ProxiedInMapValue<K>> MutProxied for Map<K, V> {
type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg;
type Mut<'msg>
= MapMut<'msg, K, V>
where
K: 'msg,
V: 'msg;
}

impl<K: Proxied, V: ProxiedInMapValue<K>> AsMut for Map<K, V> {
Expand Down
10 changes: 3 additions & 7 deletions rust/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! 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).
//! - Blocks the __internal module 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;
Expand All @@ -31,8 +31,4 @@ use protobuf_upb as kernel;
#[allow(non_upper_case_globals)]
pub const __internal: () = ();

#[doc(hidden)]
#[allow(non_upper_case_globals)]
pub const __runtime: () = ();

pub use kernel::*;
14 changes: 10 additions & 4 deletions rust/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use std::fmt::{self, Debug};
use std::iter;
use std::iter::FusedIterator;
/// Repeated scalar fields are implemented around the runtime-specific
/// `RepeatedField` struct. `RepeatedField` stores an opaque pointer to the
/// runtime-specific representation of a repeated scalar (`upb_Array*` on upb,
/// and `RepeatedField<T>*` on cpp).
use std::marker::PhantomData;
use std::fmt::{self, Debug};

use crate::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, View,
ViewProxy,
__internal::runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
__internal::{Private, SealedInternal},
__runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
};

/// Views the elements in a `repeated` field of `T`.
Expand Down Expand Up @@ -379,7 +379,10 @@ impl<T> Proxied for Repeated<T>
where
T: ProxiedInRepeated,
{
type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg;
type View<'msg>
= RepeatedView<'msg, T>
where
Repeated<T>: 'msg;
}

impl<T> SealedInternal for Repeated<T> where T: ProxiedInRepeated {}
Expand All @@ -399,7 +402,10 @@ impl<T> MutProxied for Repeated<T>
where
T: ProxiedInRepeated,
{
type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg;
type Mut<'msg>
= RepeatedMut<'msg, T>
where
Repeated<T>: 'msg;
}

impl<T> AsMut for Repeated<T>
Expand Down
32 changes: 15 additions & 17 deletions rust/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

//! Kernel-agnostic logic for the Rust Protobuf Runtime.
//!
//! For kernel-specific logic this crate delegates to the respective `__runtime`
//! crate.
#![deny(unsafe_op_in_unsafe_fn)]

use std::fmt;
Expand Down Expand Up @@ -38,22 +34,24 @@ pub use crate::string::{ProtoBytes, ProtoStr, ProtoString, Utf8Error};

pub mod prelude;

/// Everything in `__internal` is allowed to change without it being considered
/// a breaking change for the protobuf library. Nothing in here should be
/// exported in `protobuf.rs`.
/// The `__internal` module is for necessary encapsulation breaks between
/// generated code and the runtime.
///
/// These symbols are never intended to be used by application code under any
/// circumstances.
///
/// In blaze/bazel builds, this symbol is actively hidden from application
/// code by having a shim crate in front that does not re-export this symbol,
/// and a different BUILD visibility-restricted target that is used by the
/// generated code.
///
/// In Cargo builds we have no good way to technically hide this
/// symbol while still allowing it from codegen, so it is only by private by
/// convention. As application code should never use this module, anything
/// changes under `__internal` is not considered a semver breaking change.
#[path = "internal.rs"]
pub mod __internal;

/// Everything in `__runtime` is allowed to change without it being considered
/// a breaking change for the protobuf library. Nothing in here should be
/// exported in `protobuf.rs`.
#[cfg(all(bzl, cpp_kernel))]
#[path = "cpp.rs"]
pub mod __runtime;
#[cfg(any(not(bzl), upb_kernel))]
#[path = "upb.rs"]
pub mod __runtime;

mod codegen_traits;
mod cord;
#[path = "enum.rs"]
Expand Down
2 changes: 1 addition & 1 deletion rust/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#![allow(dead_code)]
#![allow(unused)]

use crate::__internal::runtime::{InnerProtoString, PtrAndLen, RawMessage};
use crate::__internal::{Private, SealedInternal};
use crate::__runtime::{InnerProtoString, PtrAndLen, RawMessage};
use crate::{
utf8::Utf8Chunks, AsView, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Optional, Proxied,
Proxy, View, ViewProxy,
Expand Down
6 changes: 4 additions & 2 deletions rust/test/cpp/interop/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use googletest::prelude::*;
use protobuf_cpp::prelude::*;

use protobuf_cpp::__runtime::PtrAndLen;
use protobuf_cpp::__internal::runtime::PtrAndLen;
use protobuf_cpp::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop};
use std::ffi::c_void;

Expand All @@ -31,7 +31,9 @@ extern "C" {
fn TakeOwnershipAndGetOptionalInt64(msg: *mut c_void) -> i64;
fn DeserializeInteropTestMessage(data: *const u8, len: usize) -> *mut c_void;
fn MutateInteropTestMessage(msg: *mut c_void);
fn SerializeInteropTestMessage(msg: *const c_void) -> protobuf_cpp::__runtime::SerializedData;
fn SerializeInteropTestMessage(
msg: *const c_void,
) -> protobuf_cpp::__internal::runtime::SerializedData;
fn DeleteInteropTestMessage(msg: *mut c_void);

fn NewWithExtension() -> *mut c_void;
Expand Down
7 changes: 3 additions & 4 deletions rust/test/shared/no_internal_access_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

use protobuf::{__internal, __runtime};
use protobuf::__internal;

#[googletest::test]
#[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.
// This test is to ensure that the `__internal` is 'blocked' by instead being a
// unit type instead of a module.
assert_eq!(__internal, ());
assert_eq!(__runtime, ());
}
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool RustGenerator::Generate(const FileDescriptor* file,
{"std", "::std"},
{"pb", "::protobuf"},
{"pbi", "::protobuf::__internal"},
{"pbr", "::protobuf::__runtime"},
{"pbr", "::protobuf::__internal::runtime"},
{"NonNull", "::std::ptr::NonNull"},
{"Phantom", "::std::marker::PhantomData"},
{"Result", "::std::result::Result"},
Expand Down

0 comments on commit b025398

Please sign in to comment.