Skip to content

Commit 3eb081d

Browse files
committed
feat: demonstrate rust client/server
1 parent 3599677 commit 3eb081d

File tree

19 files changed

+390
-69
lines changed

19 files changed

+390
-69
lines changed

examples/BUILD.bazel

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/MODULE.bazel

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ bazel_dep(name = "protobuf", version = "29.3")
66
bazel_dep(name = "rules_java", version = "8.6.3")
77
bazel_dep(name = "rules_proto", version = "7.1.0")
88
bazel_dep(name = "rules_python", version = "1.2.0-rc0")
9+
bazel_dep(name = "rules_rust", version = "0.59.1")
10+
bazel_dep(name = "rules_rust_prost", version = "0.59.1")
911
bazel_dep(name = "rules_go", version = "0.53.0")
1012
bazel_dep(name = "rules_uv", version = "0.56.0")
1113

@@ -72,3 +74,70 @@ http_jar(
7274
sha256 = "0532ad1024d62361561acaedb974d7d16889e7670b36e23e9321dd6b9d334ef9",
7375
urls = ["https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/4.27.0-RC3/protobuf-java-4.27.0-RC3.jar"],
7476
)
77+
78+
####### RUST ##########
79+
RUST_EDITION = "2021"
80+
81+
RUST_VERSION = "1.79.0"
82+
83+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
84+
rust.toolchain(
85+
edition = RUST_EDITION,
86+
versions = [RUST_VERSION],
87+
)
88+
use_repo(rust, "rust_toolchains")
89+
90+
register_toolchains("@rust_toolchains//:all")
91+
92+
# Proto toolchain
93+
94+
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
95+
96+
# protobuf / gRPC dependencies
97+
crate.spec(
98+
package = "prost",
99+
version = "0.13.1",
100+
)
101+
crate.spec(
102+
default_features = False,
103+
package = "prost-types",
104+
version = "0.13.1",
105+
)
106+
crate.spec(
107+
features = ["transport"],
108+
package = "tonic",
109+
version = "0.12.1",
110+
)
111+
crate.spec(
112+
package = "tonic-build",
113+
version = "0.12.1",
114+
)
115+
crate.spec(
116+
package = "protoc-gen-prost",
117+
version = "0.4.0",
118+
)
119+
crate.annotation(
120+
crate = "protoc-gen-prost",
121+
gen_binaries = ["protoc-gen-prost"],
122+
)
123+
crate.spec(
124+
package = "protoc-gen-tonic",
125+
version = "0.4.0",
126+
)
127+
crate.annotation(
128+
crate = "protoc-gen-tonic",
129+
gen_binaries = ["protoc-gen-tonic"],
130+
)
131+
crate.spec(
132+
default_features = False,
133+
features = [
134+
"macros",
135+
"net",
136+
"rt-multi-thread",
137+
"signal",
138+
],
139+
package = "tokio",
140+
version = "1.38",
141+
)
142+
crate.from_specs()
143+
use_repo(crate, "crates")

examples/WORKSPACE.bazel

Whitespace-only changes.

examples/foo.proto

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/go/BUILD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_go//go:def.bzl", "go_test")
22

33
go_test(
4-
name = "foo_proto_test",
5-
srcs = ["foo_proto_test.go"],
6-
deps = ["//:foo_go_proto"],
4+
name = "greeter_proto_test",
5+
srcs = ["greeter_proto_test.go"],
6+
deps = ["//proto:greeter_go_proto"],
77
)

examples/go/foo_proto_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/go/greeter_proto_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package proto_test
2+
3+
import (
4+
"testing"
5+
6+
"example.com/greeter_proto"
7+
)
8+
9+
func TestFoo(t *testing.T) {
10+
msg := &greeter_proto.HelloReply{
11+
Message: "hello world",
12+
}
13+
if msg.Message != "hello world" {
14+
t.Fail()
15+
}
16+
}

examples/proto/BUILD.bazel

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
load("@rules_go//proto:def.bzl", "go_proto_library")
2+
load("@rules_proto//proto:defs.bzl", "proto_library")
3+
load("@rules_rust_prost//:defs.bzl", "rust_prost_library")
4+
load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library")
5+
6+
package(default_visibility = ["//visibility:public"])
7+
8+
proto_library(
9+
name = "greeter_proto",
10+
srcs = ["greeter.proto"],
11+
deps = ["@com_google_protobuf//:any_proto"],
12+
)
13+
14+
py_proto_library(
15+
name = "greeter_py_proto",
16+
deps = [":greeter_proto"],
17+
)
18+
19+
# Broken by https://github.com/protocolbuffers/protobuf/pull/19679
20+
# which causes building C++ code from source.
21+
# TODO: re-enable once protobuf honors the toolchain
22+
# java_proto_library(
23+
# name = "greeter_java_proto",
24+
# deps = [":greeter_proto"],
25+
# )
26+
27+
go_proto_library(
28+
name = "greeter_go_proto",
29+
importpath = "example.com/greeter_proto",
30+
proto = ":greeter_proto",
31+
)
32+
33+
# Generate Rust bindings from the generated proto files
34+
# https://bazelbuild.github.io/rules_rust/rust_proto.html#rust_prost_library
35+
rust_prost_library(
36+
name = "greeter_rust_proto",
37+
proto = ":greeter_proto",
38+
visibility = ["//visibility:public"],
39+
)

examples/proto/greeter.proto

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
syntax = "proto3";
2+
3+
// https://github.com/hyperium/tonic/blob/master/examples/proto/helloworld/helloworld.proto
4+
package proto;
5+
option go_package = "example.com/greeter_proto";
6+
option java_package = "proto";
7+
8+
import "google/protobuf/any.proto";
9+
10+
// The greeting service definition.
11+
service Greeter {
12+
// Sends a greeting
13+
rpc SayHello(HelloRequest) returns (HelloReply) {}
14+
}
15+
16+
// The request message containing the user's name.
17+
message HelloRequest {
18+
string name = 1;
19+
repeated google.protobuf.Any details = 2;
20+
}
21+
22+
// The response message containing the greetings
23+
message HelloReply {
24+
string message = 1;
25+
}

examples/proto/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod proto {
2+
tonic::include_proto!("proto");
3+
}

0 commit comments

Comments
 (0)