• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium OS 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 
5 use std::sync::Arc;
6 
7 use sys_util::SafeDescriptor;
8 
9 use super::io_driver;
10 
11 /// An async version of SafeDescriptor.
12 // TODO: Remove this once we have converted all users to use the appropriate concrete async types.
13 pub struct Descriptor(Arc<SafeDescriptor>);
14 
15 impl Descriptor {
16     /// Creates a new `AsyncDescriptor` in a nonblocking state.
new(fd: SafeDescriptor) -> anyhow::Result<Descriptor>17     pub fn new(fd: SafeDescriptor) -> anyhow::Result<Descriptor> {
18         io_driver::prepare(&fd)?;
19         Ok(Descriptor(Arc::new(fd)))
20     }
21 
22     /// Waits for `self` to become readable. This function is edge-triggered rather than
23     /// level-triggered so callers should make sure that the underlying descriptor has been fully
24     /// drained before calling this method.
wait_readable(&self) -> anyhow::Result<()>25     pub async fn wait_readable(&self) -> anyhow::Result<()> {
26         io_driver::wait_readable(&self.0).await
27     }
28 
29     /// Waits for `self` to become writable. This function is edge-triggered rather than
30     /// level-triggered so callers should make sure that the underlying descriptor is actually full
31     /// before calling this method.
wait_writable(&self) -> anyhow::Result<()>32     pub async fn wait_writable(&self) -> anyhow::Result<()> {
33         io_driver::wait_writable(&self.0).await
34     }
35 }
36