diff --git a/rust/BUILD b/rust/BUILD index f927c3daf3..bf2703a438 100644 --- a/rust/BUILD +++ b/rust/BUILD @@ -38,6 +38,18 @@ rust_library( "//conditions:default": ["--cfg=cpp_kernel"], }), visibility = ["//visibility:public"], + deps = [":protobuf_lite"], +) + +rust_library( + name = "protobuf_lite", + srcs = ["protobuf_lite.rs"], + edition = "2024", + rustc_flags = select({ + ":use_upb_kernel": ["--cfg=upb_kernel"], + "//conditions:default": ["--cfg=cpp_kernel"], + }), + visibility = ["//visibility:public"], deps = select({ ":use_upb_kernel": [":protobuf_upb"], "//conditions:default": [":protobuf_cpp"], @@ -83,6 +95,7 @@ ALL_RUST_SRCS = PROTOBUF_SHARED + [ "gtest_matchers.rs", "gtest_matchers_impl.rs", "protobuf.rs", + "protobuf_lite.rs", "upb_kernel/conversions.rs", "upb_kernel/extension.rs", "upb_kernel/interop.rs", @@ -146,15 +159,26 @@ rust_test( # This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` active. # This is only used for tests shared between runtimes. rust_library( - name = "protobuf_upb_export", + name = "protobuf_upb_lite_export", testonly = True, - srcs = ["protobuf.rs"], + srcs = ["protobuf_lite.rs"], edition = "2024", rustc_flags = ["--cfg=upb_kernel"], visibility = [":protobuf_internal"], deps = [":protobuf_upb"], ) +rust_library( + name = "protobuf_upb_export", + testonly = True, + srcs = ["protobuf.rs"], + aliases = {"//rust:protobuf_upb_lite_export": "protobuf_lite"}, + edition = "2024", + rustc_flags = ["--cfg=upb_kernel"], + visibility = [":protobuf_internal"], + deps = [":protobuf_upb_lite_export"], +) + # The Rust Protobuf runtime using the cpp kernel. # # `rust_cpp_proto_library` implicitly depends on this target. This target cannot depend on @@ -204,15 +228,26 @@ rust_test( # This provides an identical set of re-exports as `:protobuf` with `:use_upb_kernel` inactive. # This is only used for tests shared between runtimes. rust_library( - name = "protobuf_cpp_export", + name = "protobuf_cpp_lite_export", testonly = True, - srcs = ["protobuf.rs"], + srcs = ["protobuf_lite.rs"], edition = "2024", rustc_flags = ["--cfg=cpp_kernel"], visibility = [":protobuf_internal"], deps = [":protobuf_cpp"], ) +rust_library( + name = "protobuf_cpp_export", + testonly = True, + srcs = ["protobuf.rs"], + aliases = {"//rust:protobuf_cpp_lite_export": "protobuf_lite"}, + edition = "2024", + rustc_flags = ["--cfg=cpp_kernel"], + visibility = [":protobuf_internal"], + deps = [":protobuf_cpp_lite_export"], +) + cc_library( name = "cpp_api", srcs = [ diff --git a/rust/protobuf.rs b/rust/protobuf.rs index d7f9bff9a7..d2fa10a310 100644 --- a/rust/protobuf.rs +++ b/rust/protobuf.rs @@ -1,34 +1,13 @@ // Protocol Buffers - Google's data interchange format -// Copyright 2023 Google LLC. All rights reserved. +// Copyright 2024 Google LLC. 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 -//! Rust Protobuf Runtime +//! Rust Protobuf Full Runtime //! -//! 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 module from being re-exported to application code, -//! unless they use one of our visibility-restricted targets (gencode does -//! have access to them). +//! This crate re-exports the `protobuf` lite runtime and (in the future) adds reflection +//! traits and heavy APIs (like text formatting) that are banned in `lite` mode. -#[cfg(cpp_kernel)] -use protobuf_cpp as kernel; - -#[cfg(upb_kernel)] -use protobuf_upb as kernel; - -/// 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::*; +pub use protobuf_lite::*; diff --git a/rust/protobuf_lite.rs b/rust/protobuf_lite.rs new file mode 100644 index 0000000000..23f12f154b --- /dev/null +++ b/rust/protobuf_lite.rs @@ -0,0 +1,34 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2026 Google LLC. 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 + +//! Rust Protobuf Runtime +//! +//! 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 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; + +#[cfg(upb_kernel)] +use protobuf_upb as kernel; + +/// 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::*;