GCR

Struct GCR 

Source
pub struct GCR { /* private fields */ }

Implementations§

Source§

impl GCR

Source

pub fn new() -> Self

Constructs a new GCR (Group Code Recording) instance with precomputed lookup tables for efficient encoding and decoding operations.

The GCR struct uses two lookup tables:

  • decode_mappings: A table that maps 5-bit encoded values (keys) to their decoded 4-bit values. This is used for decoding operations. Values that are considered invalid are initialized to 0xFF.
  • encode_mappings: A table that maps 4-bit decoded values into their respective 5-bit encoded counterparts, which is used for encoding operations.

The mapping pairs are predefined and represent the 4-bit to 5-bit encoding scheme:

(Encoded, Decoded)
(01010, 0), (01011, 1), (10010, 2), (10011, 3),
(01110, 4), (01111, 5), (10110, 6), (10111, 7),
(01001, 8), (11001, 9), (11010, 10), (11011, 11),
(01101, 12), (11101, 13), (11110, 14), (10101, 15)

Each (encoded, decoded) mapping is used to populate the appropriate indices in the lookup tables. For example:

  • decode_mappings[encoded] = decoded
  • encode_mappings[decoded] = encoded
§Returns

Returns an instance of the GCR struct with initialized decode_mappings and encode_mappings.

§Example
let gcr = GCR::new();
assert_eq!(gcr.decode_mappings[0b01010], 0); // Decodes "01010" to 0
assert_eq!(gcr.encode_mappings[0], 0b01010); // Encodes 0 to "01010"
Source

pub fn decode(&self, value: &[u8]) -> Option<Vec<u8>>

Decodes the provided input byte slice (value) into a Vec<u8>.

The decode method processes the input in chunks of 5 bytes, converting each chunk into a 64-bit integer by padding it with three leading zero bytes. It then calls the decode_quintuple method to decode the chunk into a vector of bytes.

§Parameters
  • value: A reference to a slice of bytes (&[u8]) that represents the encoded input data. This slice must have a length that is a multiple of 5 for full decoding.
§Return
  • Returns Some(Vec<u8>) if decoding is successful for all chunks.
  • Returns None if any chunk fails to decode.
§Example
let mut decoder = MyDecoder::new();
let input = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A];
if let Some(decoded) = decoder.decode(&input) {
    println!("Decoded output: {:?}", decoded);
} else {
    eprintln!("Failed to decode input.");
}
§Notes
  • This method uses chunks_exact(5) to divide the input into fixed-size chunks of 5.
  • For each chunk, a u64 is constructed by appending three leading zero bytes to the 5-byte chunk to match the byte size of a u64.
  • It relies on the decode_quintuple method to handle the actual decoding logic for each chunk of reconstructed data. If decode_quintuple returns None for any chunk, the entire decoding fails and the method returns None.
§Panics

This method does not panic under normal operation. However, improper implementation of decode_quintuple or incorrect input may result in unexpected behavior.

Source

pub fn encode(&self, value: &[u8]) -> Vec<u8>

Encodes a slice of bytes using a custom encoding scheme and returns the encoded data as a Vec<u8>.

This method processes the input data in chunks of 4 bytes at a time. For each chunk:

  • Each byte is split into two 4-bit nibbles (high and low).
  • These nibbles are then mapped to corresponding 5-bit encoded values using a predefined encode_mappings table.
  • The 8 encoded nibbles (now 5-bit codes) are packed into a 40-bit value in a big-endian format.
  • Finally, the 40-bit value is split into 5 bytes and appended to the output vector.
§Parameters
  • value: A slice of bytes (&[u8]) to be encoded.
§Returns
  • Vec<u8>: A vector containing the encoded bytes.
§Panics

This function assumes that self.encode_mappings is properly defined (with valid mappings for all 4-bit values [0-15]) and does not perform boundary checks on its size. Providing an invalid or incorrectly sized mapping may result in undefined behavior.

§Example
struct Encoder {
    encode_mappings: [u8; 16],
}

let encoder = Encoder {
    encode_mappings: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};
let input = vec![0x12, 0x34, 0x56, 0x78];
let encoded = encoder.encode(&input);
println!("{:?}", encoded);
§Notes
  • The function processes the input in chunks of exactly 4 bytes. If the length of the input slice is not a multiple of 4, the remaining bytes will be ignored. It is the caller’s responsibility to handle padding or provide properly-sized input.

Auto Trait Implementations§

§

impl Freeze for GCR

§

impl RefUnwindSafe for GCR

§

impl Send for GCR

§

impl Sync for GCR

§

impl Unpin for GCR

§

impl UnwindSafe for GCR

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.