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
impl Encoder
Sourcepub fn new() -> Self
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());Sourcepub fn with_checksum() -> Self
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());Sourcepub fn checksum_enabled(&self) -> bool
pub fn checksum_enabled(&self) -> bool
Check if checksum is enabled for this encoder.
Sourcepub fn sequence(&self) -> u16
pub fn sequence(&self) -> u16
Get the current sequence number.
Sequence numbers are used to detect message loss and ordering issues.
Sourcepub fn reset_sequence(&mut self)
pub fn reset_sequence(&mut self)
Reset the sequence number to zero.
Call this when establishing a new connection or after a sync reset.
Sourcepub fn encode_to_bytes(
&mut self,
data: &RawData,
classification: &Classification,
context: &Context,
) -> Vec<u8> ⓘ
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 encodeclassification- Priority classification from the classifiercontext- Shared context for predictions and patterns
§Returns
A Vec<u8> containing the encoded message bytes.
Sourcepub fn encode_with_metrics(
&mut self,
data: &RawData,
classification: &Classification,
context: &Context,
metrics: &mut CompressionMetrics,
) -> EncodedMessage
pub fn encode_with_metrics( &mut self, data: &RawData, classification: &Classification, context: &Context, metrics: &mut CompressionMetrics, ) -> EncodedMessage
Sourcepub fn encode(
&mut self,
data: &RawData,
classification: &Classification,
context: &Context,
) -> EncodedMessage
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 encodeclassification- Priority classification from the classifiercontext- 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());Sourcepub fn encode_multi(
&mut self,
values: &[(u8, f64)],
source_id: u32,
timestamp: u64,
priority: Priority,
context: &Context,
) -> EncodedMessage
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
Sourcepub fn encode_multi_adaptive(
&mut self,
channels: &[ChannelInput],
timestamp: u64,
context: &Context,
classifier: &Classifier,
) -> (EncodedMessage, Vec<Classification>)
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.