1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45//! Fuchsia-specific FIDL bindings.
67use fuchsia_async::Task;
8use zx::Channel;
910use fidl_next_protocol::Transport;
1112use crate::{
13 Client, ClientEnd, ClientProtocol, ClientSender, Server, ServerEnd, ServerProtocol,
14 ServerSender,
15};
1617/// Creates a `ClientEnd` and `ServerEnd` for the given protocol over Zircon channels.
18pub fn create_channel<P>() -> (ClientEnd<zx::Channel, P>, ServerEnd<zx::Channel, P>) {
19let (client_end, server_end) = Channel::create();
20 (ClientEnd::from_untyped(client_end), ServerEnd::from_untyped(server_end))
21}
2223/// Creates a `Client` from the given `ClientEnd` and spawns it on the current fuchsia-async
24/// executor.
25///
26/// The spawned client will handle any incoming events with `handler`.
27///
28/// Returns a `ClientSender` for the spawned client.
29pub fn spawn_client_detached<T, P, H>(client_end: ClientEnd<T, P>, handler: H) -> ClientSender<T, P>
30where
31T: Transport + 'static,
32 P: ClientProtocol<T, H> + 'static,
33 H: Send + 'static,
34{
35let mut client = Client::new(client_end);
36let sender = client.sender().clone();
37 Task::spawn(async move { client.run(handler).await }).detach_on_drop();
38 sender
39}
4041/// Creates a `Client` from the given `ClientEnd` and spawns it on the current fuchsia-async
42/// executor.
43///
44/// The spawned client will ignore any incoming events.
45///
46/// Returns a `ClientSender` for the spawned client.
47pub fn spawn_client_sender_detached<T, P>(client_end: ClientEnd<T, P>) -> ClientSender<T, P>
48where
49T: Transport + 'static,
50 P: 'static,
51{
52let mut client = Client::new(client_end);
53let sender = client.sender().clone();
54 Task::spawn(async move { client.run_sender().await }).detach_on_drop();
55 sender
56}
5758/// Creates a `Server` from the given `ServerEnd` and spawns it on the current fuchsia-async
59/// executor.
60///
61/// The spawned server will handle any incoming requests with the provided handler.
62///
63/// Returns a `ServerSender` for the spawned server.
64pub fn spawn_server_detached<T, P, H>(server_end: ServerEnd<T, P>, handler: H) -> ServerSender<T, P>
65where
66T: Transport + 'static,
67 P: ServerProtocol<T, H> + 'static,
68 H: Send + 'static,
69{
70let mut server = Server::new(server_end);
71let sender = server.sender().clone();
72 Task::spawn(async move { server.run(handler).await }).detach_on_drop();
73 sender
74}