Forward-port Rust crate swap (#29397)

Cherrypick crate fixes from 36.x to main

Closes #29397

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/29397 from protocolbuffers:forward_port_crate_swap 2b5bb9fe65
PiperOrigin-RevId: 970589358
This commit is contained in:
Jason Lunn 2026-08-25 09:05:30 -07:00 committed by Copybara-Service
parent d17cf67434
commit 3df019d491
11 changed files with 90 additions and 56 deletions

View file

@ -100,17 +100,12 @@ const fn split_version(version: &str) -> (u32, u32, u32, u32) {
// Indicates whether the given gencode and runtime versions are compatible with
// each other. Generally they are compatible if the gencode is no newer than the
// runtime, but the exception is that we consider gencode at 4.33 and older to
// be incompatible no matter what.
// runtime.
#[cfg(not(bzl))]
const fn are_versions_compatible(gencode: &str, runtime: &str) -> bool {
let gencode = split_version(gencode);
let runtime = split_version(runtime);
if gencode.0 < 4 || (gencode.0 == 4 && gencode.1 <= 33) {
return false;
}
if gencode.0 != runtime.0 {
return gencode.0 < runtime.0;
}
@ -134,18 +129,19 @@ const fn are_versions_compatible(gencode: &str, runtime: &str) -> bool {
/// the current runtime version. We require that the generated code cannot be
/// newer than the runtime version.
///
/// 4.34 is the first stable release, so any gencode older than that is not
/// compatible going forward.
///
/// If you are seeing this fail, it means that your generated code was built
/// with a protoc version newer than the runtime crate version (or you have
/// pre-4.34 gencode).
/// with a protoc version newer than the runtime crate version.
#[cfg(not(bzl))]
pub const fn assert_compatible_gencode_version(gencode_version: &'static str) {
let runtime_version = env!("CARGO_PKG_VERSION");
assert!(
are_versions_compatible(gencode_version, runtime_version),
"Gencode version is not compatible with runtime version",
concat!(
"Gencode version (see callsite) is not compatible with runtime ",
"version (runtime is ",
env!("CARGO_PKG_VERSION"),
")"
),
)
}
@ -170,11 +166,7 @@ mod tests {
#[gtest]
fn test_are_versions_compatible() {
// Pre-4.34 gencode is never considered compatible.
expect_false!(are_versions_compatible("4.33.0", "4.33.1"));
expect_false!(are_versions_compatible("3.33.0", "3.33.0"));
// Otherwise, exact matches are always fine.
// Exact matches are always fine.
expect_true!(are_versions_compatible("4.34.0-rc.1", "4.34.0-rc.1"));
expect_true!(are_versions_compatible("4.34.1", "4.34.1"));

View file

@ -5,6 +5,8 @@ pkg_filegroup(
name = "google_protobuf_crate",
srcs = [
":crate_root_files",
"//rust:rust_protobuf_libupb_src",
"//rust:rust_protobuf_src_dir",
"//rust/release_crates:license",
],
prefix = "google_protobuf",
@ -24,6 +26,10 @@ substitute_rust_release_version(
filegroup(
name = "srcs",
srcs = ["Cargo.toml"] + glob(["**/*"]),
visibility = ["//rust:__subpackages__"],
srcs = [
"Cargo.toml",
"README.md",
"build.rs",
],
visibility = ["//visibility:private"],
)

View file

@ -1,9 +1,32 @@
# Protocol Buffers - Google's data interchange format
# 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
[package]
name = "google-protobuf"
version = "{PROTOBUF_RUST_VERSION}"
edition = "2021"
edition = "2021" # The Rust edition (not to be confused with Protobuf Edition).
links = "upb"
license = "BSD-3-Clause"
rust-version = "1.79"
description = "Protocol Buffers - Google's data interchange format"
[lib]
path = "src/shared.rs"
[dependencies]
protobuf = { version = "{PROTOBUF_LEGACY_VERSION}", path = "../protobuf" }
linkme = "0.3.35"
paste = { package = "paste-complete", version = "1.0.15"}
protobuf-macros = { version = "{PROTOBUF_RUST_VERSION}", path = "../protobuf_macros", package = "protobuf-macros" }
[dev-dependencies]
googletest = "0.14.2"
[build-dependencies]
cc = "1.1.6"
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bzl)', 'cfg(cpp_kernel)', 'cfg(upb_kernel)', 'cfg(lite_runtime)'] }

View file

@ -0,0 +1,39 @@
The runtime of the official Google Rust Protobuf implementation.
This is currently a beta release: the API is subject to change, and there may be
some rough edges, including missing documentation and features.
An example for how to use this crate can be found in the
[protobuf_example crate](http://crates.io/crates/protobuf_example)
# Ownership and implementation change
This crate is officially supported by the Protobuf team at Google.
This is a completely new implementation with a different API, as well as a
fundamentally different approach than prior versions of the `protobuf` crate. It
focuses on delivering a high-quality Rust API which is backed by either a pure C
implementation (upb) or the Protobuf C++ implementation. This choice was made
for performance, feature parity, development velocity, and security reasons.
It is not planned for the pure Rust lineage to be actively developed going
forward. While it is not expected to receive significant further development, as
a stable and high quality pure Rust implementation, many open source projects
may reasonably continue to stay on the `protobuf` crate.
Development was previously done under the `protobuf` crate by a community
project by [stepancheg](https://github.com/stepancheg) who generously donated
the crate name to Google. V4.36.0 of the `protobuf` crate corresponds to V0.36.0
of this crate.
# How to get a compatible version of protoc
The protoc binary that you use to generate code needs to have a version that
exactly matches the version of the protobuf crate you are using. More
specifically, if you are using Rust protobuf `x.y.z` then you need to use protoc
`y.z`. See [here](https://protobuf.dev/support/version-support/) for more
details on our versioning scheme.
The easiest way to get ahold of protoc is to download a prebuilt binary from the
matching release [here](https://github.com/protocolbuffers/protobuf/releases).
Just make sure protoc is on your `$PATH` when you run `cargo`.

View file

@ -1 +0,0 @@
pub use protobuf::*;

View file

@ -5,8 +5,6 @@ pkg_filegroup(
name = "protobuf_crate",
srcs = [
":crate_root_files",
"//rust:rust_protobuf_libupb_src",
"//rust:rust_protobuf_src_dir",
"//rust/release_crates:license",
],
prefix = "protobuf",
@ -26,10 +24,6 @@ substitute_rust_release_version(
filegroup(
name = "srcs",
srcs = [
"Cargo.toml",
"README.md",
"build.rs",
],
visibility = ["//visibility:private"],
srcs = ["Cargo.toml"] + glob(["**/*"]),
visibility = ["//rust:__subpackages__"],
)

View file

@ -1,32 +1,9 @@
# Protocol Buffers - Google's data interchange format
# 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
[package]
name = "protobuf"
version = "{PROTOBUF_LEGACY_VERSION}"
edition = "2021" # The Rust edition (not to be confused with Protobuf Edition).
links = "upb"
edition = "2021"
license = "BSD-3-Clause"
rust-version = "1.79"
description = "Protocol Buffers - Google's data interchange format"
[lib]
path = "src/shared.rs"
[dependencies]
linkme = "0.3.35"
paste = { package = "paste-complete", version = "1.0.15"}
protobuf-macros = { version = "{PROTOBUF_RUST_VERSION}", path = "../protobuf_macros", package = "protobuf-macros" }
[dev-dependencies]
googletest = "0.14.2"
[build-dependencies]
cc = "1.1.6"
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bzl)', 'cfg(cpp_kernel)', 'cfg(upb_kernel)', 'cfg(lite_runtime)'] }
google_protobuf = { package = "google-protobuf", version = "{PROTOBUF_RUST_VERSION}", path = "../google_protobuf" }

View file

@ -6,6 +6,9 @@ some rough edges, including missing documentation and features.
An example for how to use this crate can be found in the
[protobuf_example crate](http://crates.io/crates/protobuf_example)
As of V4.36.0, this crate is a shallow wrapper around the `google_protobuf`
crate that it reexports.
# V4 ownership and implementation change
V4 of this crate is officially supported by the Protobuf team at Google. Prior

View file

@ -0,0 +1 @@
pub use google_protobuf::*;

View file

@ -56,7 +56,7 @@
#define PROTOBUF_CPP_VERSION_STRING "7.37.0-dev"
#define PROTOBUF_JAVA_VERSION_STRING "4.37.0-dev"
#define PROTOBUF_PYTHON_VERSION_STRING "7.37.0-dev"
#define PROTOBUF_RUST_VERSION_STRING "4.37.0-dev"
#define PROTOBUF_RUST_VERSION_STRING "0.37.0-dev"
namespace google {