1// Copyright 2019 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//! Helper methods for the `NodeProxy` objects.
67use fidl::endpoints::{create_proxy, ProtocolMarker, ServerEnd};
8use fidl_fuchsia_io as fio;
910pub fn open_get_proxy<M>(proxy: &fio::DirectoryProxy, flags: fio::OpenFlags, path: &str) -> M::Proxy
11where
12M: ProtocolMarker,
13{
14let (new_proxy, new_server_end) = create_proxy::<M>();
1516#[cfg(fuchsia_api_level_at_least = "NEXT")]
17proxy
18 .deprecated_open(
19 flags,
20 fio::ModeType::empty(),
21 path,
22 ServerEnd::<fio::NodeMarker>::new(new_server_end.into_channel()),
23 )
24 .unwrap();
25#[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
26proxy
27 .open(
28 flags,
29 fio::ModeType::empty(),
30 path,
31 ServerEnd::<fio::NodeMarker>::new(new_server_end.into_channel()),
32 )
33 .unwrap();
3435 new_proxy
36}
3738pub fn open3_get_proxy<M>(
39 proxy: &fio::DirectoryProxy,
40 flags: fio::Flags,
41 options: &fio::Options,
42 path: &str,
43) -> M::Proxy
44where
45M: ProtocolMarker,
46{
47let (new_proxy, new_server_end) = create_proxy::<M>();
4849#[cfg(fuchsia_api_level_at_least = "NEXT")]
50proxy.open(path, flags, options, new_server_end.into_channel()).unwrap();
51#[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
52proxy.open3(path, flags, options, new_server_end.into_channel()).unwrap();
5354 new_proxy
55}
5657/// This trait repeats parts of the `NodeProxy` trait, and is implemented for `NodeProxy`,
58/// `FileProxy`, and `DirectoryProxy`, which all share the same API. FIDL currently does not
59/// expose the API inheritance, so with this trait we have a workaround. As soon as FIDL will
60/// export the necessary information we should remove this trait, as it is just a workaround. The
61/// downsides is that this mapping needs to be manually updated, and that it is not something all
62/// the users of FIDL of `NodeProxy` would be familiar with - unnecessary complexity.
63///
64/// Add methods to this trait when they are necessary to reduce the maintenance effort.
65pub trait NodeProxyApi {
66fn clone(
67&self,
68 flags: fio::OpenFlags,
69 server_end: ServerEnd<fio::NodeMarker>,
70 ) -> Result<(), fidl::Error>;
71}
7273/// Calls .clone() on the proxy object, and returns a client side of the connection passed into the
74/// clone() method.
75pub fn clone_get_proxy<M, Proxy>(proxy: &Proxy, flags: fio::OpenFlags) -> M::Proxy
76where
77M: ProtocolMarker,
78 Proxy: NodeProxyApi,
79{
80let (new_proxy, new_server_end) = create_proxy::<M>();
8182 proxy.clone(flags, new_server_end.into_channel().into()).unwrap();
8384 new_proxy
85}
8687impl NodeProxyApi for fio::NodeProxy {
88fn clone(
89&self,
90 flags: fio::OpenFlags,
91 server_end: ServerEnd<fio::NodeMarker>,
92 ) -> Result<(), fidl::Error> {
93#[cfg(fuchsia_api_level_at_least = "26")]
94return Self::deprecated_clone(self, flags, server_end);
95#[cfg(not(fuchsia_api_level_at_least = "26"))]
96return Self::clone(self, flags, server_end);
97 }
98}
99100impl NodeProxyApi for fio::FileProxy {
101fn clone(
102&self,
103 flags: fio::OpenFlags,
104 server_end: ServerEnd<fio::NodeMarker>,
105 ) -> Result<(), fidl::Error> {
106#[cfg(fuchsia_api_level_at_least = "26")]
107return Self::deprecated_clone(self, flags, server_end);
108#[cfg(not(fuchsia_api_level_at_least = "26"))]
109return Self::clone(self, flags, server_end);
110 }
111}
112113impl NodeProxyApi for fio::DirectoryProxy {
114fn clone(
115&self,
116 flags: fio::OpenFlags,
117 server_end: ServerEnd<fio::NodeMarker>,
118 ) -> Result<(), fidl::Error> {
119#[cfg(fuchsia_api_level_at_least = "26")]
120return Self::deprecated_clone(self, flags, server_end);
121#[cfg(not(fuchsia_api_level_at_least = "26"))]
122return Self::clone(self, flags, server_end);
123 }
124}