• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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::{
6     mem,
7     ops::Deref,
8     os::unix::io::{AsRawFd, FromRawFd, IntoRawFd},
9     ptr,
10     time::Duration,
11 };
12 
13 use serde::{Deserialize, Serialize};
14 
15 use crate::descriptor::{AsRawDescriptor, FromRawDescriptor, IntoRawDescriptor};
16 pub use crate::platform::EventReadResult;
17 use crate::{generate_scoped_event, platform::EventFd, RawDescriptor, Result};
18 
19 /// See [EventFd](crate::platform::EventFd) for struct- and method-level
20 /// documentation.
21 #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
22 #[serde(transparent)]
23 pub struct Event(pub EventFd);
24 impl Event {
new() -> Result<Event>25     pub fn new() -> Result<Event> {
26         EventFd::new().map(Event)
27     }
28 
write(&self, v: u64) -> Result<()>29     pub fn write(&self, v: u64) -> Result<()> {
30         self.0.write(v)
31     }
32 
read(&self) -> Result<u64>33     pub fn read(&self) -> Result<u64> {
34         self.0.read()
35     }
36 
read_timeout(&self, timeout: Duration) -> Result<EventReadResult>37     pub fn read_timeout(&self, timeout: Duration) -> Result<EventReadResult> {
38         self.0.read_timeout(timeout)
39     }
40 
try_clone(&self) -> Result<Event>41     pub fn try_clone(&self) -> Result<Event> {
42         self.0.try_clone().map(Event)
43     }
44 }
45 
46 impl AsRawDescriptor for Event {
as_raw_descriptor(&self) -> RawDescriptor47     fn as_raw_descriptor(&self) -> RawDescriptor {
48         self.0.as_raw_fd()
49     }
50 }
51 
52 impl FromRawDescriptor for Event {
from_raw_descriptor(descriptor: RawDescriptor) -> Self53     unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
54         Event(EventFd::from_raw_fd(descriptor))
55     }
56 }
57 
58 impl IntoRawDescriptor for Event {
into_raw_descriptor(self) -> RawDescriptor59     fn into_raw_descriptor(self) -> RawDescriptor {
60         self.0.into_raw_fd()
61     }
62 }
63 
64 generate_scoped_event!(Event);
65