tonic/
lib.rs

1//! A Rust implementation of [gRPC], a high performance, open source, general
2//! RPC framework that puts mobile and HTTP/2 first.
3//!
4//! [`tonic`] is a gRPC over HTTP/2 implementation focused on **high
5//! performance**, **interoperability**, and **flexibility**. This library was
6//! created to have first class support of async/await and to act as a core building
7//! block for production systems written in Rust.
8//!
9//! # Examples
10//!
11//! Examples can be found in the [`tonic-examples`] crate.
12//!
13//! # Getting Started
14//!
15//! Follow the instructions in the [`tonic-build`] crate documentation.
16//!
17//! # Feature Flags
18//!
19//! - `transport`: Enables the fully featured, batteries included client and server
20//!   implementation based on [`hyper`], [`tower`] and [`tokio`]. This enables `server`
21//!   and `channel` features. Enabled by default.
22//! - `server`: Enables just the full featured server portion of the `transport` feature.
23//! - `channel`: Enables just the full featured channel portion of the `transport` feature.
24//! - `router`: Enables the [`axum`] based service router. Enabled by default.
25//! - `codegen`: Enables all the required exports and optional dependencies required
26//!   for [`tonic-build`]. Enabled by default.
27//! - `tls`: Enables the [`rustls`] based TLS options for the `transport` feature. Not
28//!   enabled by default.
29//! - `tls-roots`: Deprecated. An alias to `tls-native-roots` feature.
30//! - `tls-native-roots`: Adds system trust roots to [`rustls`]-based gRPC clients using the
31//!   [`rustls-native-certs`] crate. Not enabled by default.
32//! - `tls-webpki-roots`: Add the standard trust roots from the [`webpki-roots`] crate to
33//!   `rustls`-based gRPC clients. Not enabled by default.
34//! - `prost`: Enables the [`prost`] based gRPC [`Codec`] implementation. Enabled by default.
35//! - `gzip`: Enables compressing requests, responses, and streams. Depends on [`flate2`].
36//!   Not enabled by default.
37//! - `zstd`: Enables compressing requests, responses, and streams. Depends on [`zstd`].
38//!   Not enabled by default.
39//!
40//! # Structure
41//!
42//! ## Generic implementation
43//!
44//! The main goal of [`tonic`] is to provide a generic gRPC implementation over HTTP/2
45//! framing. This means at the lowest level this library provides the ability to use
46//! a generic HTTP/2 implementation with different types of gRPC encodings formats. Generally,
47//! some form of codegen should be used instead of interacting directly with the items in
48//! [`client`] and [`server`].
49//!
50//! ## Transport
51//!
52//! The [`transport`] module contains a fully featured HTTP/2.0 [`Channel`] (gRPC terminology)
53//! and [`Server`]. These implementations are built on top of [`tokio`], [`hyper`] and [`tower`].
54//! It also provides many of the features that the core gRPC libraries provide such as load balancing,
55//! tls, timeouts, and many more. This implementation can also be used as a reference implementation
56//! to build even more feature rich clients and servers. This module also provides the ability to
57//! enable TLS using [`rustls`], via the `tls` feature flag.
58//!
59//! # Code generated client/server configuration
60//!
61//! ## Max Message Size
62//!
63//! Currently, both servers and clients can be configured to set the max message encoding and
64//! decoding size. This will ensure that an incoming gRPC message will not exhaust the systems
65//! memory. By default, the decoding message limit is `4MB` and the encoding limit is `usize::MAX`.
66//!
67//! [gRPC]: https://grpc.io
68//! [`tonic`]: https://github.com/hyperium/tonic
69//! [`tokio`]: https://docs.rs/tokio
70//! [`prost`]: https://docs.rs/prost
71//! [`hyper`]: https://docs.rs/hyper
72//! [`tower`]: https://docs.rs/tower
73//! [`tonic-build`]: https://docs.rs/tonic-build
74//! [`tonic-examples`]: https://github.com/hyperium/tonic/tree/master/examples
75//! [`Codec`]: codec/trait.Codec.html
76//! [`Channel`]: transport/struct.Channel.html
77//! [`Server`]: transport/struct.Server.html
78//! [`rustls`]: https://docs.rs/rustls
79//! [`client`]: client/index.html
80//! [`transport`]: transport/index.html
81//! [`rustls-native-certs`]: https://docs.rs/rustls-native-certs
82//! [`webpki-roots`]: https://docs.rs/webpki-roots
83//! [`flate2`]: https://docs.rs/flate2
84//! [`zstd`]: https://docs.rs/zstd
85
86#![recursion_limit = "256"]
87#![warn(
88    missing_debug_implementations,
89    missing_docs,
90    rust_2018_idioms,
91    unreachable_pub
92)]
93#![deny(rustdoc::broken_intra_doc_links)]
94#![doc(
95    html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg"
96)]
97#![doc(html_root_url = "https://docs.rs/tonic/0.12.3")]
98#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
99#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
100#![cfg_attr(docsrs, feature(doc_auto_cfg))]
101
102pub mod body;
103pub mod client;
104pub mod codec;
105pub mod metadata;
106pub mod server;
107pub mod service;
108
109#[cfg(any(feature = "server", feature = "channel"))]
110pub mod transport;
111
112mod extensions;
113mod macros;
114mod request;
115mod response;
116mod status;
117mod util;
118
119/// A re-export of [`async-trait`](https://docs.rs/async-trait) for use with codegen.
120#[cfg(feature = "codegen")]
121pub use async_trait::async_trait;
122
123#[doc(inline)]
124pub use codec::Streaming;
125pub use extensions::GrpcMethod;
126pub use http::Extensions;
127pub use request::{IntoRequest, IntoStreamingRequest, Request};
128pub use response::Response;
129pub use status::{Code, ConnectError, Status, TimeoutExpired};
130
131pub(crate) type Error = Box<dyn std::error::Error + Send + Sync>;
132
133#[doc(hidden)]
134#[cfg(feature = "codegen")]
135pub mod codegen;
136
137/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
138/// By default, the Err value is of type [`Status`] but this can be overridden if desired.
139pub type Result<T, E = Status> = std::result::Result<T, E>;