reqwest_cross/lib.rs
1#![deny(missing_docs)]
2#![deny(missing_debug_implementations)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![forbid(unsafe_code)]
5// dox - used as documentation for duplicate wasm functions (Uncertain if this will cause problems
6// but seen this in Reqwest)
7
8//! # reqwest-cross
9//!
10//! The `reqwest-cross` crate (inspired by [ehttp][ehttp-url]) provides a
11//! wrapper around [reqwest][reqwest-url] for ease of use in applications that
12//! target BOTH native and wasm and do not want to block in the calling
13//! task/thread, for example in a UI task/thread or game loop. This is achieved
14//! by using callbacks. This crate provides a few options to choose from and the
15//! one that fits best for you depends on what you need. A good way to get an
16//! idea what level of abstraction would work for you is by looking at the
17//! [examples][#examples]. I would say if you're writing a larger application
18//! then [DataState] can abstract away a lot of the boiler plate. In addition I
19//! would prefer [fetch_plus] over [fetch] unless you don't need the UI
20//! callback and [fetch_plus] ends up as the one with more boiler plate. If
21//! automated retires are desired see [DataStateRetry] which exposes similar
22//! methods but with retry built in.
23//!
24//! NOTE: At least 1 [feature flag](#feature-flags) for
25//! native MUST be set to choose which runtime to use. Currently only Tokio is
26//! supported but if you want to use another runtime please open an issue on
27//! github and we'd be happy to add it. To communicate between the callback and
28//! the caller you can use various approaches such as:
29//!
30//! - The helper type in this crate [DataState] see [examples
31//! folder][examples_folder]
32//! - channels (used in [examples](#examples))
33//! - `Arc<Mutex<_>>`
34//! - promises and so on.
35//!
36//! # Examples
37//!
38//! For examples of how to use this crate see [fetch], [fetch_plus] and the
39//! [examples folder][examples_folder] in the repo
40//!
41//! # Feature Flags
42#![doc = document_features::document_features!()]
43//!
44//! Exactly 1 of the "native-*" flags MUST be enabled to select which runtime to
45//! use for native. If one of the other options needs to be used instead of
46//! tokio then defaults must be disabled.
47//!
48//! # Tradeoffs
49//!
50//! ## Exposing underlying framework that actually sends the requests
51//! The currently selected approach of exposing and using
52//! [reqwest::RequestBuilder](https://docs.rs/reqwest/latest/reqwest/struct.RequestBuilder.html) is much more flexible than the fully isolated
53//! approach used by [ehttp][ehttp-url] and is generally desirable as it allows
54//! for reuse of the same [reqwest::Client][reqwest_client] as recommended by
55//! [reqwest][reqwest-url]. However, since [reqwest::Client][reqwest_client] is
56//! asynchronous it requires an available runtime. For wasm the spawning of the
57//! futures is handled by [wasm-bindgen-futures](https://docs.rs/wasm-bindgen-futures/latest/wasm_bindgen_futures/)
58//! but for local which runtime is specified using [feature
59//! flags](#feature-flags).
60//!
61//! # How to run tokio on "secondary" thread
62//!
63//! If you want to use the main thread for your UI and need to run
64//! [tokio][tokio-url] on a "secondary" thread I found this
65//! [example](https://github.com/parasyte/egui-tokio-example) helpful. I found it in this
66//! [discussion](https://github.com/emilk/egui/discussions/521), which had other suggested
67//! examples as well.
68//!
69//! [reqwest-url]: https://docs.rs/reqwest/latest/reqwest/
70//! [ehttp-url]: https://docs.rs/ehttp/0.2.0/ehttp/
71//! [tokio-url]: https://docs.rs/tokio/latest/tokio/
72//! [reqwest_client]:https://docs.rs/reqwest/latest/reqwest/struct.Client.html
73//! [examples_folder]: https://github.com/c-git/reqwest-cross/tree/main/examples
74
75mod data_state;
76mod data_state_retry;
77mod platform;
78mod traits;
79#[cfg(feature = "yield_now")]
80mod yield_;
81
82pub use data_state::{Awaiting, CanMakeProgress, DataState, DataStateError, ErrorBounds};
83pub use data_state_retry::DataStateRetry;
84pub use platform::{fetch, fetch_plus, spawn};
85pub use traits::{BoundedFuture, DoneHandler, ResponseHandler, UiCallBack, ValidReturn};
86#[cfg(feature = "yield_now")]
87pub use yield_::yield_now;
88
89// Exported to ensure version used matches
90pub use futures::channel::oneshot;
91pub use reqwest;
92
93// TODO 4: Add browser test to ensure we don't break WASM by accident. Even if
94// it can compile it might not be browser safe