cros_async/
async_types.rs

1// Copyright 2020 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::io;
6
7use base::RecvTube;
8use base::SendTube;
9use base::Tube;
10use base::TubeResult;
11use serde::de::DeserializeOwned;
12use serde::Serialize;
13
14pub use crate::sys::async_types::*;
15use crate::Executor;
16use crate::IntoAsync;
17
18// NOTE: A StreamChannel can either be used fully in async mode, or not in async
19// mode. Mixing modes will break StreamChannel's internal read/write
20// notification system.
21//
22// TODO(b/213153157): this type isn't properly available upstream yet. Once it
23// is, we can re-enable these implementations.
24// impl IntoAsync for StreamChannel {}
25// impl IntoAsync for &StreamChannel {}
26
27impl IntoAsync for Tube {}
28impl IntoAsync for SendTube {}
29impl IntoAsync for RecvTube {}
30
31pub struct RecvTubeAsync(AsyncTube);
32#[allow(dead_code)]
33impl RecvTubeAsync {
34    pub fn new(tube: RecvTube, ex: &Executor) -> io::Result<Self> {
35        Ok(Self(AsyncTube::new(
36            ex,
37            #[allow(deprecated)]
38            tube.into_tube(),
39        )?))
40    }
41
42    /// TODO(b/145998747, b/184398671): this async approach needs to be refactored
43    /// upstream, but for now is implemented to work using simple blocking futures
44    /// (avoiding the unimplemented wait_readable).
45    pub async fn next<T: 'static + DeserializeOwned + Send>(&self) -> TubeResult<T> {
46        self.0.next().await
47    }
48}
49
50pub struct SendTubeAsync(AsyncTube);
51#[allow(dead_code)]
52impl SendTubeAsync {
53    pub fn new(tube: SendTube, ex: &Executor) -> io::Result<Self> {
54        Ok(Self(AsyncTube::new(
55            ex,
56            #[allow(deprecated)]
57            tube.into_tube(),
58        )?))
59    }
60
61    pub async fn send<T: 'static + Serialize + Send + Sync>(&self, msg: T) -> TubeResult<()> {
62        self.0.send(msg).await
63    }
64}