fidl_next/bind/
client.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
112
113
114
115
116
117
118
119
120
// 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::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll};

use crate::protocol::{self, DispatcherError, Transport};

use super::{ClientEnd, Method, ResponseBuffer};

/// A strongly typed client.
pub struct Client<T: Transport, P> {
    client: protocol::Client<T>,
    _protocol: PhantomData<P>,
}

impl<T: Transport, P> Client<T, P> {
    /// Creates a new client and dispatcher from a client end.
    pub fn new(client_end: ClientEnd<T, P>) -> (Self, ClientDispatcher<T, P>) {
        let (client, dispatcher) = protocol::Client::new(client_end.into_untyped());
        (Self::from_untyped(client), ClientDispatcher::from_untyped(dispatcher))
    }

    /// Creates a new strongly typed client from an untyped client.
    pub fn from_untyped(client: protocol::Client<T>) -> Self {
        Self { client, _protocol: PhantomData }
    }

    /// Returns the underlying untyped client.
    pub fn untyped(&self) -> &protocol::Client<T> {
        &self.client
    }
}

impl<T: Transport, P> Clone for Client<T, P> {
    fn clone(&self) -> Self {
        Self { client: self.client.clone(), _protocol: PhantomData }
    }
}

/// A protocol which supports clients.
pub trait ClientProtocol<T: Transport, H> {
    /// Handles a received client event with the given handler.
    fn on_event(handler: &mut H, ordinal: u64, buffer: T::RecvBuffer);
}

/// An adapter for a client protocol handler.
pub struct ClientAdapter<P, H> {
    handler: H,
    _protocol: PhantomData<P>,
}

impl<P, H> ClientAdapter<P, H> {
    /// Creates a new protocol client handler from a supported handler.
    pub fn from_untyped(handler: H) -> Self {
        Self { handler, _protocol: PhantomData }
    }
}

impl<T, P, H> protocol::ClientHandler<T> for ClientAdapter<P, H>
where
    T: Transport,
    P: ClientProtocol<T, H>,
{
    fn on_event(&mut self, ordinal: u64, buffer: T::RecvBuffer) {
        P::on_event(&mut self.handler, ordinal, buffer)
    }
}

/// A strongly typed client dispatcher.
pub struct ClientDispatcher<T: Transport, P> {
    dispatcher: protocol::ClientDispatcher<T>,
    _protocol: PhantomData<P>,
}

impl<T: Transport, P> ClientDispatcher<T, P> {
    /// Creates a new client dispathcer from an untyped client dispatcher.
    pub fn from_untyped(dispatcher: protocol::ClientDispatcher<T>) -> Self {
        Self { dispatcher, _protocol: PhantomData }
    }

    /// Runs the dispatcher with the provided handler.
    pub async fn run<H>(&mut self, handler: H) -> Result<(), DispatcherError<T::Error>>
    where
        P: ClientProtocol<T, H>,
    {
        self.dispatcher.run(ClientAdapter { handler, _protocol: PhantomData::<P> }).await
    }
}

/// A strongly typed transaction future.
pub struct TransactionFuture<'a, T: Transport, M> {
    future: protocol::TransactionFuture<'a, T>,
    _method: PhantomData<M>,
}

impl<'a, T: Transport, M> TransactionFuture<'a, T, M> {
    /// Returns a strongly typed `TransactionFuture` wrapping the given transaction future.
    pub fn from_untyped(future: protocol::TransactionFuture<'a, T>) -> Self {
        Self { future, _method: PhantomData }
    }
}

impl<T, M> Future for TransactionFuture<'_, T, M>
where
    T: Transport,
    M: Method,
{
    type Output = Result<ResponseBuffer<T, M>, T::Error>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // SAFETY: `self` is pinned, and `future` is a subfield of `self`, so `future` will not be
        // moved.
        let future = unsafe { self.map_unchecked_mut(|this| &mut this.future) };
        future.poll(cx).map_ok(ResponseBuffer::from_untyped)
    }
}