fidl_next_bind/
buffer.rs

1// Copyright 2024 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.
4
5use core::marker::PhantomData;
6
7use fidl_next_codec::{Decode, DecodeError, DecoderExt as _, Owned};
8use fidl_next_protocol::Transport;
9
10use super::Method;
11
12macro_rules! buffer {
13    ($name:ident, $trait:ident::$type:ident) => {
14        /// A strongly typed receive buffer.
15        pub struct $name<T: Transport, M> {
16            buffer: T::RecvBuffer,
17            _method: PhantomData<M>,
18        }
19
20        impl<T: Transport, M> $name<T, M> {
21            /// Creates a new strongly typed receive buffer from an untyped receive buffer.
22            pub fn from_untyped(buffer: T::RecvBuffer) -> Self {
23                Self { buffer, _method: PhantomData }
24            }
25
26            /// Returns the underlying untyped receive buffer.
27            pub fn into_untyped(self) -> T::RecvBuffer {
28                self.buffer
29            }
30
31            /// Decodes the buffer.
32            pub fn decode(&mut self) -> Result<Owned<'_, M::$type>, DecodeError>
33            where
34                M: $trait,
35                M::$type: Decode<T::RecvBuffer>,
36            {
37                (&mut self.buffer).decode_last::<M::$type>()
38            }
39        }
40    };
41}
42
43buffer!(RequestBuffer, Method::Request);
44buffer!(ResponseBuffer, Method::Response);