cros_async/sys/linux/
async_types.rs

1// Copyright 2022 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::Tube;
8use base::TubeResult;
9use serde::de::DeserializeOwned;
10use serde::Serialize;
11
12use crate::Executor;
13use crate::IoSource;
14
15pub struct AsyncTube {
16    inner: IoSource<Tube>,
17}
18
19impl AsyncTube {
20    pub fn new(ex: &Executor, tube: Tube) -> io::Result<AsyncTube> {
21        Ok(AsyncTube {
22            inner: ex.async_from(tube)?,
23        })
24    }
25
26    pub async fn next<T: DeserializeOwned>(&self) -> TubeResult<T> {
27        self.inner.wait_readable().await.unwrap();
28        self.inner.as_source().recv()
29    }
30
31    pub async fn send<T: 'static + Serialize + Send + Sync>(&self, msg: T) -> TubeResult<()> {
32        self.inner.as_source().send(&msg)
33    }
34}
35
36impl From<AsyncTube> for Tube {
37    fn from(at: AsyncTube) -> Tube {
38        at.inner.into_source()
39    }
40}