interrupt-read

An interruptable Reader
This crate provides the InterruptReader, which can have its
read operations interrupted by an Interruptor. They are
acquired from the interrupt_reader::pair function, which
returns an mpsc channel backed pair.
When Interruptor::interrupt is called, the InterruptReader
will return an erro of kind ErrorKind::Other with a payload of
InterruptReceived (you can check for that using the
is_interrupt function). Otherwise, it will act like any normal
Read struct.
When an interrupt is received, the underlying data is not lost, it still exists, and if you call a reading function again, it will be retrieved, unless another interrupt is sent before that.
Some things to note about this crate:
- It functions by spawning a separate thread, which will actually
read from the original
Reader, so keep that in mind. - There is some (light) overhead over the read operations.
- You should not wrap this struct in a
BufReadersince the struct already has its own internal buffer. - This reader doesn’t assume that
Ok(0)is the end of input, and the spawned thread will only terminate if theInterruptReaderis dropped.
Note
The reason why this function returns ErrorKind::Other, rather
than ErrorKind::Interrupted is that the latter error is
ignored by functions like BufRead::read_line and
BufRead::read_until, which is probably not what you want to
happen.