calloop icon indicating copy to clipboard operation
calloop copied to clipboard

Generic wrappers

Open vikigenius opened this issue 1 year ago • 2 comments

Should I always wrap my FDs in a generic wrapper even if I implement EventSource and just have a single source?

pub struct IPCSource {
    socket_source: calloop::generic::Generic<calloop::generic::FdWrapper<UnixListener>>,
}

impl calloop::EventSource for IPCSource {
...
}

vs

pub struct IPCSource {
    socket: UnixListener,
}

impl calloop::EventSource for IPCSource {
...
}

And if I do wrap it in a Generic:

How should I process events

    fn process_events<F>(
        &mut self,
        readiness: calloop::Readiness,
        token: calloop::Token,
        callback: F,
    ) -> Result<calloop::PostAction, Self::Error>
    where
        F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
    {
        self.socket_source
            .process_events(readiness, token, |evt, _| {
                if readiness.readable {
                    // TODO
                }
            });
        Ok(calloop::PostAction::Continue)
    }

vs

    fn process_events<F>(
        &mut self,
        readiness: calloop::Readiness,
        token: calloop::Token,
        callback: F,
    ) -> Result<calloop::PostAction, Self::Error>
    where
        F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
    {
        
        if readiness.readable {
                    // TODO
        }
        Ok(calloop::PostAction::Continue)
    }

vikigenius avatar Jul 04 '24 15:07 vikigenius

Generic is the best way to wrap a file descriptor, as it takes care of much of the internal busywork of registering file descriptors. So the first pattern is the best one to use here.

notgull avatar Jul 05 '24 13:07 notgull

@notgull Thanks for the response. If I do the first pattern. How should I process events.

If I use this

    fn process_events<F>(
        &mut self,
        readiness: calloop::Readiness,
        token: calloop::Token,
        callback: F,
    ) -> Result<calloop::PostAction, Self::Error>
    where
        F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
    {
        self.socket_source
            .process_events(readiness, token, |evt, _| {
                if readiness.readable {
                    // TODO
                    self.handle_ipc_event(); // Causes compile error
                }
                Ok(calloop::PostAction::Continue)
            });
        Ok(calloop::PostAction::Continue)
    }

I get an error because I can't borrow self as mutable ?

vikigenius avatar Jul 19 '24 17:07 vikigenius