Struct ShmReader

Source
pub struct ShmReader { /* private fields */ }
Expand description

An implementation of the Reader which access a persistent channel through memory mapping. A ShmReader must be created using the shm_reader function.

§Examples

use kekbit_core::shm::*;
let writer_id = 1850;
let channel_id = 42;
let test_tmp_dir = tempdir::TempDir::new("kektest").unwrap();
let reader = shm_reader(&test_tmp_dir.path(), channel_id).unwrap();
println!("{:?}", reader.header());

Implementations§

Source§

impl ShmReader

Source

pub fn header(&self) -> &Header

Returns a reference to the Header associated with this channel

Source

pub fn position(&self) -> u32

Returns the current read position. It could be the total amount of bytes read so far(including bytes from record headers and the one used for record padding) if no succesfull move_to operation was executed on this reader.

Trait Implementations§

Source§

impl Debug for ShmReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Reader for ShmReader

Source§

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

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 |pos,buf| println!("{}->{}",pos, std::str::from_utf8(buf).unwrap()), 10).unwrap();  
Source§

fn move_to(&mut self, position: u32) -> Result<u32, InvalidPosition>

Tries to move this reader to a given position if it is valid.

Returns the position itself if the operation was successful otherwise some error.

§Arguments
  • position - Position where will try to point this reader. It must be a valid position on the channel
§Errors

///

§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 |pos,buf| println!("{}->{}",pos, std::str::from_utf8(buf).unwrap()), 10).unwrap();  

reader.move_to(0);//start reading from beginning again

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.