Struct ShmWriter

Source
pub struct ShmWriter<D: DataFormat> { /* private fields */ }
Expand description

An implementation of the Writer which access a persistent channel through memory mapping, and uses a specific DataFormat. A ShmWriter must be created using the shm_writer function. Any ShmWriter exclusively holds the channel is bound to, and it is not thread safe. If multiple threads must write into a channel they should be externally synchronized.

§Examples

use kekbit_core::tick::TickUnit::Nanos;
use kekbit_core::shm::*;
use kekbit_core::header::Header;
use kekbit_core::api::Writer;
use kekbit_codecs::codecs::raw::RawBinDataFormat;

const FOREVER: u64 = 99_999_999_999;
let writer_id = 1850;
let channel_id = 42;
let capacity = 3000;
let max_msg_len = 100;
let header = Header::new(writer_id, channel_id, capacity, max_msg_len, FOREVER, Nanos);
let test_tmp_dir = tempdir::TempDir::new("kektest").unwrap();
let mut writer = shm_writer(&test_tmp_dir.path(), &header, RawBinDataFormat).unwrap();
writer.heartbeat().unwrap();

Implementations§

Source§

impl<D: DataFormat> ShmWriter<D>

Source

pub fn available(&self) -> u32

Returns the amount of space in this channel still available for write.

Source

pub fn write_offset(&self) -> u32

Returns the amount of data written into this channel.

Source

pub fn header(&self) -> &Header

Returns a reference to the Header associated with this channel.

Source

pub fn data_format(&self) -> &D

Trait Implementations§

Source§

impl<D: DataFormat> Drop for ShmWriter<D>

Source§

fn drop(&mut self)

Marks this channel as closed, flushes the changes to the disk, and removes the memory mapping.

Source§

impl<D: DataFormat> Writer<D> for ShmWriter<D>

Source§

fn write(&mut self, data: &impl Encodable<D>) -> Result<u32, WriteError>

Writes a message into the channel. This operation will encode the data directly into channel. While this is a non blocking operation, only one write should be executed at any given time.

Returns the total amount of bytes wrote into the channel which includes, the size of the message, the size of the message header and the amount of padding add to that message.

§Arguments
  • data - The data which to encode and write into the channel.
§Errors

Two kinds of failures may occur. One if the encoding operation failed, the other if the channel rejected the message for reasons such data is too large or no space is available in the channel.

§Examples
use kekbit_core::tick::TickUnit::Nanos;
use kekbit_core::shm::*;
use kekbit_core::header::Header;
use kekbit_core::api::Writer;
use kekbit_codecs::codecs::raw::RawBinDataFormat;

const FOREVER: u64 = 99_999_999_999;
let writer_id = 1850;
let channel_id = 42;
let capacity = 30_000;
let max_msg_len = 100;
let header = Header::new(writer_id, channel_id, capacity, max_msg_len, FOREVER, Nanos);
let test_tmp_dir = tempdir::TempDir::new("kektest").unwrap();
let mut writer = shm_writer(&test_tmp_dir.path(), &header, RawBinDataFormat).unwrap();
let msg = "There are 10 kinds of people: those who know binary and those who don't";
let msg_data = msg.as_bytes();
writer.write(&msg_data).unwrap();
Source§

fn heartbeat(&mut self) -> Result<u32, WriteError>

Push a heartbeat message into the channel. Hearbeats are zero sized messages which do not need encoding. Reader should never activate callbacks for heartbeat messsages.

Returns RecordHeaderLen, 8 in the current version if the operation succeeds.

§Errors

If the operation fails a ChannelFull error will be returned, which signals that the channel will not accept any new messages.

Source§

fn flush(&mut self) -> Result<(), Error>

Flushes the channel’s outstanding memory map modifications to disk. Calling this method explicitly it is not encouraged as flushing does occur automatically and comes with a performance penalty. It should be used only if for various reasons a writer wants to persist the channel data to the disk at a higher rate than is done automatically.

Returns Ok(()) if the operation succeeds.

§Errors

If flushing fails an I/O error is returned.

§Examples
use kekbit_core::tick::TickUnit::Nanos;
use kekbit_core::shm::*;
use kekbit_core::header::Header;
use kekbit_core::api::Writer;
use kekbit_codecs::codecs::raw::RawBinDataFormat;

const FOREVER: u64 = 99_999_999_999;
let writer_id = 1850;
let channel_id = 42;
let capacity = 30_000;
let max_msg_len = 100;
let header = Header::new(writer_id, channel_id, capacity, max_msg_len, FOREVER, Nanos);
let test_tmp_dir = tempdir::TempDir::new("kektest").unwrap();
let mut writer = shm_writer(&test_tmp_dir.path(), &header, RawBinDataFormat).unwrap();
let msg = "There are 10 kinds of people: those who know binary and those who don't";
let msg_data = msg.as_bytes();
writer.write(&msg_data).unwrap();
writer.flush().unwrap();

Auto Trait Implementations§

§

impl<D> Freeze for ShmWriter<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for ShmWriter<D>
where D: RefUnwindSafe,

§

impl<D> !Send for ShmWriter<D>

§

impl<D> !Sync for ShmWriter<D>

§

impl<D> Unpin for ShmWriter<D>
where D: Unpin,

§

impl<D> UnwindSafe for ShmWriter<D>
where D: UnwindSafe,

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.