1#![doc(html_root_url = "https://docs.rs/toad-async/0.0.0")]
5#![cfg_attr(any(docsrs, feature = "docs"), feature(doc_cfg))]
6#![allow(clippy::unused_unit)]
9#![deny(missing_docs)]
12#![deny(missing_debug_implementations)]
13#![deny(missing_copy_implementations)]
14#![cfg_attr(not(test), deny(unsafe_code))]
15#![cfg_attr(not(test), warn(unreachable_pub))]
18#![cfg_attr(not(feature = "std"), no_std)]
21
22#[cfg(feature = "alloc")]
23extern crate alloc as std_alloc;
24
25pub mod nb {
27 pub use crate::{Error, Result};
28}
29
30pub type Result<T, E> = core::result::Result<T, Error<E>>;
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub enum Error<E> {
36 WouldBlock,
38 Other(E),
40}
41
42impl<E> Error<E> {
43 pub fn map<F, R>(self, mut f: F) -> Error<R>
45 where F: FnMut(E) -> R
46 {
47 match self {
48 | Error::Other(e) => Error::Other(f(e)),
49 | Error::WouldBlock => Error::WouldBlock,
50 }
51 }
52}
53
54pub fn block<T, E, F>(f: F) -> core::result::Result<T, E>
60 where F: Fn() -> Result<T, E>
61{
62 loop {
63 match f() {
64 | Ok(ok) => break Ok(ok),
65 | Err(Error::Other(e)) => break Err(e),
66 | Err(Error::WouldBlock) => {
67 #[cfg(feature = "std")]
68 std::thread::sleep(std::time::Duration::from_nanos(500));
69 continue;
70 },
71 }
72 }
73}
74
75pub trait AsyncResult<T, E>
77 where Self: Sized
78{
79 fn async_map_err<F, R>(self, f: F) -> Result<T, R>
82 where F: FnMut(E) -> R;
83}
84
85impl<T, E> AsyncResult<T, E> for Result<T, E> {
86 fn async_map_err<F, R>(self, mut f: F) -> Result<T, R>
87 where F: FnMut(E) -> R
88 {
89 self.map_err(|e| match e {
90 | Error::WouldBlock => Error::WouldBlock,
91 | Error::Other(e) => Error::Other(f(e)),
92 })
93 }
94}
95
96pub trait AsyncPollable<T, E> {
98 fn block(self) -> core::result::Result<T, E>;
117}
118
119impl<F, T, E> AsyncPollable<T, E> for F where F: Fn() -> Result<T, E>
120{
121 fn block(self) -> core::result::Result<T, E> {
122 crate::block(self)
123 }
124}