[][src]Trait kekbit_core::api::Reader

pub trait Reader {
    fn read(
        &mut self,
        handler: &mut impl FnMut(&[u8]),
        message_count: u16
    ) -> Result<u32, ReadError>; }

The Reader trait allows reading bytes from a kekbit channel. Implementers of this trait are called 'kekbit readers'. Usually a reader is bound to a given channel, and it is expected that multiple readers will safely access the same channel simultaneous.

Required methods

fn read(
    &mut self,
    handler: &mut impl FnMut(&[u8]),
    message_count: u16
) -> Result<u32, ReadError>

Accesses a number of records from the kekbit channel, and for each record, calls the given callback.

Returns the amount of bytes read from the channel

Arguments

  • handler - The callback function to be called when a record is pulled from the channel
  • message_count - A hint about how many records shall be read from the channel before the method completes. It is expected that this method will take from the channel at most this many records

Errors

If this function fails, than an error variant will be returned. These errors are not expected to be recoverable. Once any error except Timeout occurred, there will never be data to read pass the current read marker. However reading from beginning of the channel to the current read marker should still be a valid operation. The Timeout exception, may or may not be recoverable, depends on the channel Writer behaviour.

Loading content...

Implementors

impl Reader for ShmReader[src]

fn read(
    &mut self,
    handler: &mut impl FnMut(&[u8]),
    message_count: u16
) -> Result<u32, ReadError>
[src]

Reads up to message_count messages from the channel and for each message
calls the given handler. The handler it is not called for heartbeat messages. This operation is non-blocking, if you want to wait for a message to be available, external wait/spin mechanisms must be used.

Returns the amount of bytes read together and/or an error. Even if an error occurred there may have been messages which were correctly read, and for which the handler was called.

Arguments

  • handler - The function which will be called every time a valid messages is read from the channel. The message is just binary data, it's up to the handler to interpret it properly.
  • message_count - The maximum number of messages to be read on this call.

Errors

Various errors may occur such: a writer timeout is detected, end of channel is reached, channel is closed or channel data is corrupted. However even in such situations, some valid records may have been processed.

Examples

use kekbit_core::shm::*;
use crate::kekbit_core::api::Reader;
let writer_id = 1850;
let channel_id = 42;
let test_tmp_dir = tempdir::TempDir::new("kektest").unwrap();
let mut reader = shm_reader(&test_tmp_dir.path(), channel_id).unwrap();
reader.read(&mut |buf| println!("{}",std::str::from_utf8(buf).unwrap()), 10).unwrap();  
Loading content...