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")]
32pub mod timeout;
33#[cfg(feature = "time")]
34mod timer;
35#[cfg(feature = "tracing")]
36pub mod trace;
37mod upload;
38
39pub use body::Body;
40pub use client::{Client, HttpService};
41pub use error::Error;
42pub use local_service::LocalBoxCloneService;
43pub use maybe::{MaybeSend, MaybeSync};
44pub use provider::ProviderLayer;
45pub use request::RequestBuilder;
46pub use request_options::RequestOptions;
47#[cfg(feature = "reqwest")]
48pub use reqwest_service::ReqwestService;
49pub use response::Response;
50pub use upload::Upload;
51
52pub use http;
53pub use http::{header, HeaderMap, Method, StatusCode};
54/// Credentials of a generated `Configuration` are [`SecretString`]s: their `Debug` is redacted, their memory is
55/// zeroed when dropped, and [`ExposeSecret::expose_secret`] reads them.
56pub use secrecy;
57pub use secrecy::{ExposeSecret, SecretString};
58pub use tower;
59
60/// The error type services and layers return: any error that can cross threads.
61pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
62
63/// Natively, what the crate hands out stays `Send` (and `Sync` where it was): [`MaybeSend`] relaxes bounds on
64/// WebAssembly only.
65#[cfg(all(test, not(target_family = "wasm")))]
66mod send_tests {
67    use super::*;
68
69    fn send_sync<T: Send + Sync>() {}
70    fn send<T: Send>(_: &T) {}
71
72    #[test]
73    fn natively_clients_bodies_and_futures_are_send() {
74        send_sync::<Client>();
75        send_sync::<HttpService>();
76        send_sync::<Body>();
77        send_sync::<Response>();
78        send_sync::<RequestBuilder>();
79        send_sync::<Upload>();
80        send_sync::<multipart::Form>();
81        send_sync::<ProviderLayer>();
82        let request = Client::default().request(Method::GET, "https://example.com/");
83        send(&request.send());
84        let response = Response::from(http::Response::new(Body::empty()));
85        let events = Response::from(http::Response::new(Body::empty())).events();
86        send(&events);
87        send(&events.json::<serde_json::Value>());
88        send(&response.bytes_stream());
89        #[cfg(feature = "time")]
90        send(
91            &sse::Events::from_bytes(futures_util::stream::empty::<Result<&'static [u8], Error>>())
92                .idle_timeout(std::time::Duration::from_secs(1)),
93        );
94    }
95}