Skip to main content

timvw_rust_runtime/
lib.rs

1#![cfg_attr(
2    all(feature = "reqwest", feature = "retry", feature = "tracing"),
3    doc = include_str!("../README.md")
4)]
5#![cfg_attr(
6    not(all(feature = "reqwest", feature = "retry", feature = "tracing")),
7    doc = "Runtime for Rust clients generated by [openapi-generator-timvw-rust](https://github.com/timvw/openapi-generator-timvw-rust): a tower-based HTTP client. See the README for the guide."
8)]
9#![cfg_attr(docsrs, feature(doc_cfg))]
10// On WebAssembly the shared pieces of bodies and uploads (`Arc`s) hold streams that are not `Send`: there is one
11// thread, and an `Arc` keeps the code the same as natively.
12#![cfg_attr(target_family = "wasm", allow(clippy::arc_with_non_send_sync))]
13
14mod body;
15mod client;
16#[cfg(all(feature = "fs", not(target_family = "wasm")))]
17mod download;
18mod error;
19mod local_service;
20mod maybe;
21pub mod multipart;
22pub mod provider;
23mod request;
24mod request_options;
25#[cfg(feature = "reqwest")]
26mod reqwest_service;
27mod response;
28#[cfg(feature = "retry")]
29pub mod retry;
30pub mod sse;
31#[cfg(feature = "time")]
32mod timer;
33#[cfg(feature = "tracing")]
34pub mod trace;
35mod upload;
36
37pub use body::Body;
38pub use client::{Client, HttpService};
39pub use error::Error;
40pub use local_service::LocalBoxCloneService;
41pub use maybe::{MaybeSend, MaybeSync};
42pub use provider::ProviderLayer;
43pub use request::RequestBuilder;
44pub use request_options::RequestOptions;
45#[cfg(feature = "reqwest")]
46pub use reqwest_service::ReqwestService;
47pub use response::Response;
48pub use upload::Upload;
49
50pub use http;
51pub use http::{header, HeaderMap, Method, StatusCode};
52/// Credentials of a generated `Configuration` are [`SecretString`]s: their `Debug` is redacted, their memory is
53/// zeroed when dropped, and [`ExposeSecret::expose_secret`] reads them.
54pub use secrecy;
55pub use secrecy::{ExposeSecret, SecretString};
56pub use tower;
57
58/// The error type services and layers return: any error that can cross threads.
59pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
60
61/// Natively, what the crate hands out stays `Send` (and `Sync` where it was): [`MaybeSend`] relaxes bounds on
62/// WebAssembly only.
63#[cfg(all(test, not(target_family = "wasm")))]
64mod send_tests {
65    use super::*;
66
67    fn send_sync<T: Send + Sync>() {}
68    fn send<T: Send>(_: &T) {}
69
70    #[test]
71    fn natively_clients_bodies_and_futures_are_send() {
72        send_sync::<Client>();
73        send_sync::<HttpService>();
74        send_sync::<Body>();
75        send_sync::<Response>();
76        send_sync::<RequestBuilder>();
77        send_sync::<Upload>();
78        send_sync::<multipart::Form>();
79        send_sync::<ProviderLayer>();
80        let request = Client::default().request(Method::GET, "https://example.com/");
81        send(&request.send());
82        let response = Response::from(http::Response::new(Body::empty()));
83        let events = Response::from(http::Response::new(Body::empty())).events();
84        send(&events);
85        send(&events.json::<serde_json::Value>());
86        send(&response.bytes_stream());
87        #[cfg(feature = "time")]
88        send(
89            &sse::Events::from_bytes(futures_util::stream::empty::<Result<&'static [u8], Error>>())
90                .idle_timeout(std::time::Duration::from_secs(1)),
91        );
92    }
93}