Skip to main content

tonic/
lib.rs

1/*
2 *
3 * Copyright 2025 gRPC authors.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25//! A Rust implementation of [gRPC], a high performance, open source, general
26//! RPC framework that puts mobile and HTTP/2 first.
27//!
28//! [`tonic`] is a gRPC over HTTP/2 implementation focused on **high
29//! performance**, **interoperability**, and **flexibility**. This library was
30//! created to have first class support of async/await and to act as a core building
31//! block for production systems written in Rust.
32//!
33//! # Examples
34//!
35//! Examples can be found in the [`tonic-examples`] crate.
36//!
37//! # Getting Started
38//!
39//! Follow the instructions in the [`tonic-build`] crate documentation.
40//!
41//! # Feature Flags
42//!
43//! - `transport`: Enables the fully featured, batteries included client and server
44//!   implementation based on [`hyper`], [`tower`] and [`tokio`]. This enables `server`
45//!   and `channel` features. Enabled by default.
46//! - `server`: Enables just the full featured server portion of the `transport` feature.
47//! - `channel`: Enables just the full featured channel portion of the `transport` feature.
48//! - `router`: Enables the [`axum`] based service router. Enabled by default.
49//! - `codegen`: Enables all the required exports and optional dependencies required
50//!   for [`tonic-build`]. Enabled by default.
51//! - `tls-ring`: Enables the [`rustls`] based TLS options for the `transport` feature using
52//!   the [`ring`] libcrypto provider. Not enabled by default.
53//! - `tls-aws-lc`: Enables the [`rustls`] based TLS options for the `transport` feature using
54//!   the [`aws-lc-rs`] libcrypto provider. Not enabled by default.
55//! - `tls-native-roots`: Adds system trust roots to [`rustls`]-based gRPC clients using the
56//!   [`rustls-native-certs`] crate. Not enabled by default.
57//! - `tls-webpki-roots`: Add the standard trust roots from the [`webpki-roots`] crate to
58//!   `rustls`-based gRPC clients. Not enabled by default.
59//! - `tls-connect-info`: Adds additional implementations of [`Connected`]
60//!   on common TLS connectors. Not enabled by default, unless any of the other `tls-*`
61//!   features are enabled. This feature is useful for when trying to use a custom
62//!   TLS connector with `connect_with_connector` without enabling any `tls-*` features.
63//! - `gzip`: Enables compressing requests, responses, and streams. Depends on [`flate2`].
64//!   Not enabled by default.
65//! - `deflate`: Enables compressing requests, responses, and streams. Depends on [`flate2`].
66//!   Not enabled by default.
67//! - `zstd`: Enables compressing requests, responses, and streams. Depends on [`zstd`].
68//!   Not enabled by default.
69//!
70//! # Structure
71//!
72//! ## Generic implementation
73//!
74//! The main goal of [`tonic`] is to provide a generic gRPC implementation over HTTP/2
75//! framing. This means at the lowest level this library provides the ability to use
76//! a generic HTTP/2 implementation with different types of gRPC encodings formats. Generally,
77//! some form of codegen should be used instead of interacting directly with the items in
78//! [`client`] and [`server`].
79//!
80//! ## Transport
81//!
82//! The [`transport`] module contains a fully featured HTTP/2.0 [`Channel`] (gRPC terminology)
83//! and [`Server`]. These implementations are built on top of [`tokio`], [`hyper`] and [`tower`].
84//! It also provides many of the features that the core gRPC libraries provide such as load balancing,
85//! tls, timeouts, and many more. This implementation can also be used as a reference implementation
86//! to build even more feature rich clients and servers. This module also provides the ability to
87//! enable TLS using [`rustls`], via the `tls` feature flag.
88//!
89//! # Code generated client/server configuration
90//!
91//! ## Max Message Size
92//!
93//! Currently, both servers and clients can be configured to set the max message encoding and
94//! decoding size. This will ensure that an incoming gRPC message will not exhaust the systems
95//! memory. By default, the decoding message limit is `4MB` and the encoding limit is `usize::MAX`.
96//!
97//! [gRPC]: https://grpc.io
98//! [`tonic`]: https://github.com/grpc/grpc-rust
99//! [`tokio`]: https://docs.rs/tokio
100//! [`hyper`]: https://docs.rs/hyper
101//! [`tower`]: https://docs.rs/tower
102//! [`tonic-build`]: https://docs.rs/tonic-build
103//! [`ring`]: https://docs.rs/ring
104//! [`aws-lc-rs`]: https://docs.rs/aws-lc-rs
105//! [`tonic-examples`]: https://github.com/grpc/grpc-rust/tree/master/examples
106//! [`Codec`]: codec/trait.Codec.html
107//! [`Channel`]: transport/struct.Channel.html
108//! [`Server`]: transport/struct.Server.html
109//! [`Connected`]: transport/server/trait.Connected.html
110//! [`rustls`]: https://docs.rs/rustls
111//! [`client`]: client/index.html
112//! [`transport`]: transport/index.html
113//! [`rustls-native-certs`]: https://docs.rs/rustls-native-certs
114//! [`webpki-roots`]: https://docs.rs/webpki-roots
115//! [`flate2`]: https://docs.rs/flate2
116//! [`zstd`]: https://docs.rs/zstd
117
118#![recursion_limit = "256"]
119#![doc(
120    html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg"
121)]
122#![doc(issue_tracker_base_url = "https://github.com/grpc/grpc-rust/issues/")]
123#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
124#![cfg_attr(docsrs, feature(doc_cfg))]
125
126pub mod body;
127pub mod client;
128pub mod codec;
129pub mod metadata;
130pub mod server;
131pub mod service;
132
133#[cfg(any(feature = "server", feature = "channel"))]
134pub mod transport;
135
136mod extensions;
137mod macros;
138mod request;
139mod response;
140mod status;
141mod util;
142
143/// A re-export of [`async-trait`](https://docs.rs/async-trait) for use with codegen.
144#[cfg(feature = "codegen")]
145pub use async_trait::async_trait;
146
147#[doc(inline)]
148pub use codec::Streaming;
149pub use extensions::GrpcMethod;
150pub use http::Extensions;
151pub use request::{CancellationHandle, IntoRequest, IntoStreamingRequest, Request};
152pub use response::Response;
153pub use status::{Code, ConnectError, Status, TimeoutExpired};
154
155pub(crate) type BoxError = Box<dyn std::error::Error + Send + Sync>;
156
157#[doc(hidden)]
158#[cfg(feature = "codegen")]
159pub mod codegen;
160
161/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
162/// By default, the Err value is of type [`Status`] but this can be overridden if desired.
163pub type Result<T, E = Status> = std::result::Result<T, E>;