zenoh/api/builders/
info.rs1use std::future::{IntoFuture, Ready};
16
17use zenoh_config::wrappers::ZenohId;
18use zenoh_core::{Resolvable, Wait};
19use zenoh_protocol::core::WhatAmI;
20
21use crate::net::runtime::Runtime;
22
23#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
36pub struct ZenohIdBuilder<'a> {
37 runtime: &'a Runtime,
38}
39
40impl<'a> ZenohIdBuilder<'a> {
41 pub(crate) fn new(runtime: &'a Runtime) -> Self {
42 Self { runtime }
43 }
44}
45
46impl Resolvable for ZenohIdBuilder<'_> {
47 type To = ZenohId;
48}
49
50impl Wait for ZenohIdBuilder<'_> {
51 fn wait(self) -> Self::To {
52 self.runtime.zid()
53 }
54}
55
56impl IntoFuture for ZenohIdBuilder<'_> {
57 type Output = <Self as Resolvable>::To;
58 type IntoFuture = Ready<<Self as Resolvable>::To>;
59
60 fn into_future(self) -> Self::IntoFuture {
61 std::future::ready(self.wait())
62 }
63}
64
65#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
80pub struct RoutersZenohIdBuilder<'a> {
81 runtime: &'a Runtime,
82}
83
84impl<'a> RoutersZenohIdBuilder<'a> {
85 pub(crate) fn new(runtime: &'a Runtime) -> Self {
86 Self { runtime }
87 }
88}
89
90impl Resolvable for RoutersZenohIdBuilder<'_> {
91 type To = Box<dyn Iterator<Item = ZenohId> + Send + Sync>;
92}
93
94impl Wait for RoutersZenohIdBuilder<'_> {
95 fn wait(self) -> Self::To {
96 Box::new(
97 zenoh_runtime::ZRuntime::Application
98 .block_in_place(self.runtime.manager().get_transports_unicast())
99 .into_iter()
100 .filter_map(|s| {
101 s.get_whatami()
102 .ok()
103 .and_then(|what| (what == WhatAmI::Router).then_some(()))
104 .and_then(|_| s.get_zid().map(Into::into).ok())
105 }),
106 )
107 }
108}
109
110impl IntoFuture for RoutersZenohIdBuilder<'_> {
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
119#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
134pub struct PeersZenohIdBuilder<'a> {
135 runtime: &'a Runtime,
136}
137
138impl<'a> PeersZenohIdBuilder<'a> {
139 pub(crate) fn new(runtime: &'a Runtime) -> Self {
140 Self { runtime }
141 }
142}
143
144impl Resolvable for PeersZenohIdBuilder<'_> {
145 type To = Box<dyn Iterator<Item = ZenohId> + Send + Sync>;
146}
147
148impl Wait for PeersZenohIdBuilder<'_> {
149 fn wait(self) -> <Self as Resolvable>::To {
150 Box::new(
151 zenoh_runtime::ZRuntime::Application
152 .block_in_place(self.runtime.manager().get_transports_unicast())
153 .into_iter()
154 .filter_map(|s| {
155 s.get_whatami()
156 .ok()
157 .and_then(|what| (what == WhatAmI::Peer).then_some(()))
158 .and_then(|_| s.get_zid().map(Into::into).ok())
159 }),
160 )
161 }
162}
163
164impl IntoFuture for PeersZenohIdBuilder<'_> {
165 type Output = <Self as Resolvable>::To;
166 type IntoFuture = Ready<<Self as Resolvable>::To>;
167
168 fn into_future(self) -> Self::IntoFuture {
169 std::future::ready(self.wait())
170 }
171}