Skip to main content

EventEncoder

Trait EventEncoder 

Source
pub trait EventEncoder<O: BitReversible> {
    // Required methods
    fn encode(&mut self) -> Result<(), Error>;
    fn get_encoder(&self) -> &BitEncoder<O>;
    fn get_event(&self) -> u16;
}
Expand description

The event encoder structure that permits constructing events over frames.

To construct an event properly, you have to create a new struct that implements this trait.

§Example

use bitvec::order::Lsb0;

use shdp::prelude::common::{event::EventEncoder, bits::BitEncoder, error::Error};

pub struct MyEvent {
    encoder: BitEncoder<Lsb0>,
    data: u32,
}

impl MyEvent {
    pub fn new(data: u32) -> Self {
        MyEvent {
            encoder: BitEncoder::<Lsb0>::new(),
            data,
        }
    }
}

impl EventEncoder<Lsb0> for MyEvent {
    fn encode(&mut self) -> Result<(), Error> {
        self.encoder.add_data(self.data, 32)?;
        Ok(())
   }

    fn get_encoder(&self) -> &BitEncoder<Lsb0> {
        &self.encoder
    }

    fn get_event(&self) -> u16 {
        0x0001
    }
}

Required Methods§

Source

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

Encode every data into the encoder.

§Errors

It can return an ErrorKind::SizeConstraintViolation if the data overflows a u32.
It can return an ErrorKind::SizeConstraintViolation if the data is less than 8 bits.
It can return an ErrorKind::SizeConstraintViolation if the frame is not well-formed.

It can return any other error based on user implementation.

Source

fn get_encoder(&self) -> &BitEncoder<O>

Get the encoder.

Source

fn get_event(&self) -> u16

Get the event code. It returns a u16 that represents the event code.

!! The event code is a unique identifier and should not be repeated.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§