1pub use lazy_static::lazy_static;
21pub mod macros;
22
23use std::future::{Future, IntoFuture, Ready};
24
25pub use zenoh_result::{bail, to_zerror, zerror};
27pub mod zresult {
28 pub use zenoh_result::*;
29}
30pub use zresult::{Error, ZResult as Result};
31
32pub trait Resolvable {
34 type To: Sized;
35}
36
37#[doc(hidden)]
39pub trait IntoSendFuture: Resolvable {
40 type IntoFuture: Future<Output = Self::To> + Send;
41}
42
43impl<T> IntoSendFuture for T
44where
45 T: Resolvable + IntoFuture<Output = Self::To>,
46 T::IntoFuture: Send,
47{
48 type IntoFuture = T::IntoFuture;
49}
50
51pub trait Wait: Resolvable {
53 fn wait(self) -> Self::To;
55}
56
57#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
63pub trait Resolve<Output>:
64 Resolvable<To = Output>
65 + Wait
66 + IntoSendFuture
67 + IntoFuture<IntoFuture = <Self as IntoSendFuture>::IntoFuture, Output = Output>
68 + Send
69{
70}
71
72impl<T, Output> Resolve<Output> for T where
73 T: Resolvable<To = Output>
74 + Wait
75 + IntoSendFuture
76 + IntoFuture<IntoFuture = <Self as IntoSendFuture>::IntoFuture, Output = Output>
77 + Send
78{
79}
80
81#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
83pub struct ResolveClosure<C, To>(C)
84where
85 To: Sized + Send,
86 C: FnOnce() -> To + Send;
87
88impl<C, To> ResolveClosure<C, To>
89where
90 To: Sized + Send,
91 C: FnOnce() -> To + Send,
92{
93 pub fn new(c: C) -> Self {
94 Self(c)
95 }
96}
97
98impl<C, To> Resolvable for ResolveClosure<C, To>
99where
100 To: Sized + Send,
101 C: FnOnce() -> To + Send,
102{
103 type To = To;
104}
105
106impl<C, To> IntoFuture for ResolveClosure<C, To>
107where
108 To: Sized + Send,
109 C: FnOnce() -> To + Send,
110{
111 type Output = <Self as Resolvable>::To;
112 type IntoFuture = Ready<<Self as Resolvable>::To>;
113
114 fn into_future(self) -> Self::IntoFuture {
115 std::future::ready(self.wait())
116 }
117}
118
119impl<C, To> Wait for ResolveClosure<C, To>
120where
121 To: Sized + Send,
122 C: FnOnce() -> To + Send,
123{
124 fn wait(self) -> <Self as Resolvable>::To {
125 self.0()
126 }
127}
128
129#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
131pub struct ResolveFuture<F, To>(F)
132where
133 To: Sized + Send,
134 F: Future<Output = To> + Send;
135
136impl<F, To> ResolveFuture<F, To>
137where
138 To: Sized + Send,
139 F: Future<Output = To> + Send,
140{
141 pub fn new(f: F) -> Self {
142 Self(f)
143 }
144}
145
146impl<F, To> Resolvable for ResolveFuture<F, To>
147where
148 To: Sized + Send,
149 F: Future<Output = To> + Send,
150{
151 type To = To;
152}
153
154impl<F, To> IntoFuture for ResolveFuture<F, To>
155where
156 To: Sized + Send,
157 F: Future<Output = To> + Send,
158{
159 type Output = To;
160 type IntoFuture = F;
161
162 fn into_future(self) -> Self::IntoFuture {
163 self.0
164 }
165}
166
167impl<F, To> Wait for ResolveFuture<F, To>
168where
169 To: Sized + Send,
170 F: Future<Output = To> + Send,
171{
172 fn wait(self) -> <Self as Resolvable>::To {
173 zenoh_runtime::ZRuntime::Application.block_in_place(self.0)
174 }
175}
176
177pub use zenoh_result::{likely, unlikely};