Skip to main content

Encoder

Struct Encoder 

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

Encoder for ALEC messages.

The encoder maintains internal state (sequence numbers) and provides methods to encode sensor data into compact binary messages.

§Thread Safety

Encoder is not thread-safe. Each thread should have its own instance. For multi-threaded scenarios, consider using separate encoders per thread or wrapping in a Mutex.

Implementations§

Source§

impl Encoder

Source

pub fn new() -> Self

Create a new encoder with default settings.

§Example
use alec::Encoder;

let encoder = Encoder::new();
assert_eq!(encoder.sequence(), 0);
assert!(!encoder.checksum_enabled());
Source

pub fn with_checksum() -> Self

Create an encoder with checksum verification enabled.

When checksum is enabled, encoded messages include a CRC32 checksum for integrity verification during decoding.

§Example
use alec::Encoder;

let encoder = Encoder::with_checksum();
assert!(encoder.checksum_enabled());
Source

pub fn checksum_enabled(&self) -> bool

Check if checksum is enabled for this encoder.

Source

pub fn sequence(&self) -> u16

Get the current sequence number.

Sequence numbers are used to detect message loss and ordering issues.

Source

pub fn reset_sequence(&mut self)

Reset the sequence number to zero.

Call this when establishing a new connection or after a sync reset.

Source

pub fn encode_to_bytes( &mut self, data: &RawData, classification: &Classification, context: &Context, ) -> Vec<u8>

Encode data and return raw bytes.

This is a convenience method that combines encoding and serialization. If checksum is enabled, the returned bytes include the checksum.

§Arguments
  • data - The raw sensor data to encode
  • classification - Priority classification from the classifier
  • context - Shared context for predictions and patterns
§Returns

A Vec<u8> containing the encoded message bytes.

Source

pub fn encode_with_metrics( &mut self, data: &RawData, classification: &Classification, context: &Context, metrics: &mut CompressionMetrics, ) -> EncodedMessage

Encode data while collecting compression metrics.

Use this method to track compression performance over time.

§Arguments
  • data - The raw sensor data to encode
  • classification - Priority classification
  • context - Shared context
  • metrics - Metrics collector to update
§Returns

The encoded message.

Source

pub fn encode( &mut self, data: &RawData, classification: &Classification, context: &Context, ) -> EncodedMessage

Encode data with classification into a compact message.

This method selects the optimal encoding strategy based on the context’s predictions and the data’s characteristics:

  • If value equals the last value, uses repeated encoding (1 byte)
  • If value is close to prediction, uses delta encoding (1-4 bytes)
  • Otherwise, uses raw encoding (8 bytes for value)
§Arguments
  • data - The raw sensor data to encode
  • classification - Priority classification from the classifier
  • context - Shared context for predictions and patterns
§Returns

An EncodedMessage containing the compressed data.

§Examples
use alec::{Encoder, Classifier, Context, RawData};

let mut encoder = Encoder::new();
let classifier = Classifier::default();
let context = Context::new();

let data = RawData::new(22.5, 0);
let classification = classifier.classify(&data, &context);
let message = encoder.encode(&data, &classification, &context);

// Message is ready to transmit
println!("Encoded {} bytes", message.len());
Source

pub fn encode_multi( &mut self, values: &[(u8, f64)], source_id: u32, timestamp: u64, priority: Priority, context: &Context, ) -> EncodedMessage

Encode multiple values in one message

Source

pub fn encode_multi_adaptive( &mut self, channels: &[ChannelInput], timestamp: u64, context: &Context, classifier: &Classifier, ) -> (EncodedMessage, Vec<Classification>)

Encode multiple channels into a single frame with adaptive per-channel compression and priority-based inclusion.

  • P1–P3 channels: always included, adaptively encoded
  • P4 channels: included only if frame stays under MULTI_FRAME_CAP
  • P5 channels: excluded from the frame (context still updated by caller)

Returns the encoded message and a list of classifications (one per input channel, in the same order) so the caller can observe P5 channels.

Trait Implementations§

Source§

impl Clone for Encoder

Source§

fn clone(&self) -> Encoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encoder

Source§

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

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

impl Default for Encoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.