Skip to main content

Crate interrupt_read

Crate interrupt_read 

Source
Expand description

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 BufReader since 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 the InterruptReader is 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.

Structs§

InterruptReader
An interruptable, buffered Reader.
InterruptReceived
Indicates that an Interruptor has called Interruptor::interrupt, causing a read operation to be interrupted.
InterruptSendError
An error occurred while calling Interruptor::interrupt.
Interruptor
An interruptor for an InterruptReader.

Functions§

is_interrupt
Wether the error in question originated from an Interruptor calling Interruptor::interrupt.
pair
Returns a pair of an InterruptReader and an Interruptor.