fidl_next/bind/
endpoint.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 2024 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.

use core::marker::PhantomData;

use crate::{
    munge, Decode, DecodeError, Encodable, EncodableOption, Encode, EncodeError, EncodeOption,
    Slot, TakeFrom,
};

macro_rules! endpoint {
    (
        #[doc = $doc:literal]
        $name:ident
    ) => {
        #[doc = $doc]
        #[derive(Debug)]
        #[repr(transparent)]
        pub struct $name<T, P> {
            transport: T,
            _protocol: PhantomData<P>,
        }

        impl<T, P> $name<T, P> {
            /// Returns a new endpoint over the given transport.
            pub fn from_untyped(transport: T) -> Self {
                Self { transport, _protocol: PhantomData }
            }

            /// Returns the underlying transport.
            pub fn into_untyped(self) -> T {
                self.transport
            }
        }

        unsafe impl<D, T, P> Decode<D> for $name<T, P>
        where
            D: ?Sized,
            T: Decode<D>,
        {
            fn decode(slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
                munge!(let Self { transport, _protocol: _ } = slot);
                T::decode(transport, decoder)
            }
        }

        impl<T, P> Encodable for $name<T, P>
        where
            T: Encodable,
        {
            type Encoded<'buf> = $name<T::Encoded<'buf>, P>;
        }

        impl<T, P> EncodableOption for $name<T, P>
        where
            T: EncodableOption,
        {
            type EncodedOption<'buf> = $name<T::EncodedOption<'buf>, P>;
        }

        impl<E, T, P> Encode<E> for $name<T, P>
        where
            E: ?Sized,
            T: Encode<E>,
        {
            fn encode(
                &mut self,
                encoder: &mut E,
                slot: Slot<'_, Self::Encoded<'_>>,
            ) -> Result<(), EncodeError> {
                munge!(let Self::Encoded { transport, _protocol: _ } = slot);
                self.transport.encode(encoder, transport)
            }
        }

        impl<E, T, P> EncodeOption<E> for $name<T, P>
        where
            E: ?Sized,
            T: EncodeOption<E>,
        {
            fn encode_option(
                this: Option<&mut Self>,
                encoder: &mut E,
                slot: Slot<'_, Self::EncodedOption<'_>>,
            ) -> Result<(), EncodeError> {
                munge!(let Self::EncodedOption { transport, _protocol: _ } = slot);
                T::encode_option(this.map(|this| &mut this.transport), encoder, transport)
            }
        }

        impl<T, P, U> TakeFrom<$name<U, P>> for $name<T, P>
        where
            T: TakeFrom<U>,
        {
            fn take_from(from: &mut $name<U, P>) -> Self {
                Self { transport: T::take_from(&mut from.transport), _protocol: PhantomData }
            }
        }
    };
}

endpoint! {
    /// The client end of a protocol.
    ClientEnd
}

endpoint! {
    /// The server end of a protocol.
    ServerEnd
}