vfs/test_utils/
node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Helper methods for the `NodeProxy` objects.

use fidl::endpoints::{create_proxy, ProtocolMarker, ServerEnd};
use fidl_fuchsia_io as fio;

pub fn open_get_proxy<M>(proxy: &fio::DirectoryProxy, flags: fio::OpenFlags, path: &str) -> M::Proxy
where
    M: ProtocolMarker,
{
    let (new_proxy, new_server_end) = create_proxy::<M>();

    proxy
        .open(
            flags,
            fio::ModeType::empty(),
            path,
            ServerEnd::<fio::NodeMarker>::new(new_server_end.into_channel()),
        )
        .unwrap();

    new_proxy
}

pub fn open3_get_proxy<M>(
    proxy: &fio::DirectoryProxy,
    flags: fio::Flags,
    options: &fio::Options,
    path: &str,
) -> M::Proxy
where
    M: ProtocolMarker,
{
    let (new_proxy, new_server_end) = create_proxy::<M>();

    proxy.open3(path, flags, options, new_server_end.into_channel()).unwrap();

    new_proxy
}

/// This trait repeats parts of the `NodeProxy` trait, and is implemented for `NodeProxy`,
/// `FileProxy`, and `DirectoryProxy`, which all share the same API.  FIDL currently does not
/// expose the API inheritance, so with this trait we have a workaround.  As soon as FIDL will
/// export the necessary information we should remove this trait, as it is just a workaround. The
/// downsides is that this mapping needs to be manually updated, and that it is not something all
/// the users of FIDL of `NodeProxy` would be familiar with - unnecessary complexity.
///
/// Add methods to this trait when they are necessary to reduce the maintenance effort.
pub trait NodeProxyApi {
    fn clone(
        &self,
        flags: fio::OpenFlags,
        server_end: ServerEnd<fio::NodeMarker>,
    ) -> Result<(), fidl::Error>;
}

/// Calls .clone() on the proxy object, and returns a client side of the connection passed into the
/// clone() method.
pub fn clone_get_proxy<M, Proxy>(proxy: &Proxy, flags: fio::OpenFlags) -> M::Proxy
where
    M: ProtocolMarker,
    Proxy: NodeProxyApi,
{
    let (new_proxy, new_server_end) = create_proxy::<M>();

    proxy.clone(flags, new_server_end.into_channel().into()).unwrap();

    new_proxy
}

impl NodeProxyApi for fio::NodeProxy {
    fn clone(
        &self,
        flags: fio::OpenFlags,
        server_end: ServerEnd<fio::NodeMarker>,
    ) -> Result<(), fidl::Error> {
        #[cfg(fuchsia_api_level_at_least = "NEXT")]
        return Self::deprecated_clone(self, flags, server_end);
        #[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
        return Self::clone(self, flags, server_end);
    }
}

impl NodeProxyApi for fio::FileProxy {
    fn clone(
        &self,
        flags: fio::OpenFlags,
        server_end: ServerEnd<fio::NodeMarker>,
    ) -> Result<(), fidl::Error> {
        #[cfg(fuchsia_api_level_at_least = "NEXT")]
        return Self::deprecated_clone(self, flags, server_end);
        #[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
        return Self::clone(self, flags, server_end);
    }
}

impl NodeProxyApi for fio::DirectoryProxy {
    fn clone(
        &self,
        flags: fio::OpenFlags,
        server_end: ServerEnd<fio::NodeMarker>,
    ) -> Result<(), fidl::Error> {
        #[cfg(fuchsia_api_level_at_least = "NEXT")]
        return Self::deprecated_clone(self, flags, server_end);
        #[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
        return Self::clone(self, flags, server_end);
    }
}