Skip to main content

iceoryx2_cal/reactor/
mod.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#[cfg(target_os = "linux")]
14pub mod epoll;
15pub mod posix_select;
16pub mod recommended;
17
18use core::{fmt::Debug, time::Duration};
19
20use iceoryx2_bb_posix::{
21    file_descriptor::FileDescriptor, file_descriptor_set::SynchronousMultiplexing,
22};
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum ReactorCreateError {
26    InsufficientResources,
27    InternalError,
28}
29
30impl core::fmt::Display for ReactorCreateError {
31    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32        write!(f, "ReactorCreateError::{self:?}")
33    }
34}
35
36impl core::error::Error for ReactorCreateError {}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum ReactorAttachError {
40    AlreadyAttached,
41    CapacityExceeded,
42    InsufficientResources,
43    InternalError,
44}
45
46impl core::fmt::Display for ReactorAttachError {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        write!(f, "ReactorAttachError::{self:?}")
49    }
50}
51
52impl core::error::Error for ReactorAttachError {}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum ReactorWaitError {
56    Interrupt,
57    InsufficientPermissions,
58    InternalError,
59}
60
61impl core::fmt::Display for ReactorWaitError {
62    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
63        write!(f, "ReactorWaitError::{self:?}")
64    }
65}
66
67impl core::error::Error for ReactorWaitError {}
68
69pub trait ReactorGuard<'reactor, 'attachment> {
70    fn file_descriptor(&self) -> &FileDescriptor;
71}
72
73pub trait Reactor: Sized + Debug + Send {
74    type Guard<'reactor, 'attachment>: ReactorGuard<'reactor, 'attachment>
75    where
76        Self: 'reactor;
77    type Builder: ReactorBuilder<Self>;
78
79    fn capacity(&self) -> usize;
80    fn len(&self) -> usize;
81    fn is_empty(&self) -> bool;
82
83    fn attach<'reactor, 'attachment, F: SynchronousMultiplexing + Debug + ?Sized>(
84        &'reactor self,
85        value: &'attachment F,
86    ) -> Result<Self::Guard<'reactor, 'attachment>, ReactorAttachError>;
87
88    fn try_wait<F: FnMut(&FileDescriptor)>(&self, fn_call: F) -> Result<usize, ReactorWaitError>;
89    fn timed_wait<F: FnMut(&FileDescriptor)>(
90        &self,
91        fn_call: F,
92        timeout: Duration,
93    ) -> Result<usize, ReactorWaitError>;
94    fn blocking_wait<F: FnMut(&FileDescriptor)>(
95        &self,
96        fn_call: F,
97    ) -> Result<usize, ReactorWaitError>;
98}
99
100pub trait ReactorBuilder<T: Reactor> {
101    fn new() -> Self;
102    fn create(self) -> Result<T, ReactorCreateError>;
103}