#channel #non-blocking #mio #standard #poll #events #polled

mio_channel

Provide a wrapper of the standard channel that can be polled with Mio

4 releases

0.1.3 Nov 11, 2022
0.1.2 Nov 11, 2022
0.1.1 Nov 11, 2022
0.1.0 Nov 11, 2022

#31 in #poll

Download history 4/week @ 2024-11-16 38/week @ 2024-11-23 46/week @ 2024-11-30 11/week @ 2024-12-07 1/week @ 2024-12-14 1/week @ 2025-01-18 3/week @ 2025-01-25 3/week @ 2025-02-01 37/week @ 2025-02-08 18/week @ 2025-02-15 10/week @ 2025-02-22 15/week @ 2025-03-01

80 downloads per month

MIT license

6KB
65 lines

mio_channel

Provide a wrapper of the standard channel that can be polled with Mio.

Example

const CHANNEL: mio::Token = mio::Token(0);

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut poll = mio::Poll::new()?;

    let mut events = mio::Events::with_capacity(2);

    let (tx, mut rx) = mio_channel::channel();

    poll.registry().register(&mut rx, CHANNEL, mio::Interest::READABLE)?;

    let handler = std::thread::spawn(move || {
        std::thread::sleep(std::time::Duration::from_millis(1000));

        let _ = tx.send("Hello world!");
    });

    poll.poll(&mut events, None)?;

    assert_eq!(rx.try_recv()?, "Hello world!");

    let _ = handler.join();

    Ok(())
}

lib.rs:

mio_channel

Provide a wrapper of the standard channel that can be polled with Mio.

Example

#[cfg(test)]
mod tests {
    const CHANNEL: mio::Token = mio::Token(0);

    #[test]
    fn test_channel() -> Result<(), Box<dyn std::error::Error>> {
        let mut poll = mio::Poll::new()?;

        let mut events = mio::Events::with_capacity(2);

        let (tx, mut rx) = mio_channel::channel();

        poll.registry().register(&mut rx, CHANNEL, mio::Interest::READABLE)?;

        let handler = std::thread::spawn(move || {
            std::thread::sleep(std::time::Duration::from_millis(1000));

            let _ = tx.send("Hello world!");
        });

        poll.poll(&mut events, None)?;

        assert_eq!(rx.try_recv()?, "Hello world!");

        let _ = handler.join();

        Ok(())
    }
}

Dependencies

~0.5–11MB
~54K SLoC