Skip to content

Commit

Permalink
Merge pull request #24 from rust-amplify/resourcetype
Browse files Browse the repository at this point in the history
add information about resource type to the Handler::handle_registered
  • Loading branch information
dr-orlovsky authored Jan 29, 2024
2 parents 61c2646 + 4bba06a commit ab016f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ mod reactor;
mod resource;
mod timeouts;

pub use resource::{Io, Resource, ResourceId, ResourceIdGenerator, WriteAtomic, WriteError};
pub use resource::{
Io, Resource, ResourceId, ResourceIdGenerator, ResourceType, WriteAtomic, WriteError,
};
pub use timeouts::{Timer, Timestamp};

pub use self::reactor::{Action, Controller, Error, Handler, Reactor, Runtime};
10 changes: 5 additions & 5 deletions src/reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crossbeam_channel as chan;

use crate::poller::{IoFail, IoType, Poll, Waker, WakerRecv, WakerSend};
use crate::resource::WriteError;
use crate::{Resource, ResourceId, Timer, Timestamp, WriteAtomic};
use crate::{Resource, ResourceId, ResourceType, Timer, Timestamp, WriteAtomic};

/// Maximum amount of time to wait for I/O.
const WAIT_TIMEOUT: Duration = Duration::from_secs(60 * 60);
Expand Down Expand Up @@ -164,7 +164,7 @@ pub trait Handler: Send + Iterator<Item = Action<Self::Listener, Self::Transport
/// The resource id will be used later in [`Self::handle_listener_event`],
/// [`Self::handle_transport_event`], [`Self::handover_listener`] and [`handover_transport`]
/// calls to the handler.
fn handle_registered(&mut self, fd: RawFd, id: ResourceId);
fn handle_registered(&mut self, fd: RawFd, id: ResourceId, ty: ResourceType);

/// Method called by the reactor when a [`Self::Command`] is received for the [`Handler`].
///
Expand Down Expand Up @@ -626,7 +626,7 @@ impl<H: Handler, P: Poll> Runtime<H, P> {

let id = self.poller.register(&listener, IoType::read_only());
self.listeners.insert(id, listener);
self.service.handle_registered(fd, id);
self.service.handle_registered(fd, id, ResourceType::Listener);
}
Action::RegisterTransport(transport) => {
let fd = transport.as_raw_fd();
Expand All @@ -636,7 +636,7 @@ impl<H: Handler, P: Poll> Runtime<H, P> {

let id = self.poller.register(&transport, IoType::read_only());
self.transports.insert(id, transport);
self.service.handle_registered(fd, id);
self.service.handle_registered(fd, id, ResourceType::Transport);
}
Action::UnregisterListener(id) => {
let Some(listener) = self.unregister_listener(id) else {
Expand Down Expand Up @@ -823,7 +823,7 @@ mod test {
) {
unreachable!()
}
fn handle_registered(&mut self, _fd: RawFd, _id: ResourceId) {}
fn handle_registered(&mut self, _fd: RawFd, _id: ResourceId, _ty: ResourceType) {}
fn handle_command(&mut self, cmd: Self::Command) {
match cmd {
Cmd::Init => {
Expand Down
9 changes: 9 additions & 0 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub enum Io {
Write,
}

/// Type of the resource.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum ResourceType {
/// Listener resource.
Listener,
/// Transport resource.
Transport,
}

/// Generator for the new [`ResourceId`]s which should be used by pollers implementing [`Poll`]
/// trait.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
Expand Down

0 comments on commit ab016f9

Please sign in to comment.