pub struct EncodableData {
    pub value: u64,
    pub num_bits: u8,
}
Expand description

Struct containing data to encode in a ClientRoutingLabel.

Consist of 2 properties: value, and num_bits. value is a u64 and should be set to the actual data to encode. num_bits is a u8 and should be set to how many bits should be encoded. This ensures a particular value will always be encoded to the same bit position in a label, regardless of the actual size of value.

§Examples:

use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
use amazon_cloudfront_client_routing_lib::encode_decode::Base32;

let mut data: EncodableData;
let encoding_system = Base32 {};

// value is 1 bit and needs to encode as 10 bits: 0b0000000001
data = EncodableData {
    value: 1,
    num_bits: 10,
};

assert_eq!("ab", encoding_system.encode(&mut [data]));

// value is 4 bits and needs to encode as 5 bits: 0b10000
data = EncodableData {
    value: 16,
    num_bits: 5
};

assert_eq!("q", encoding_system.encode(&mut [data]));

// value is 6 bits and needs to encode as 5 bits: 0b00000
// only the least significant bits are retained
data = EncodableData {
    value: 32,
    num_bits: 5
};

assert_eq!("a", encoding_system.encode(&mut [data]));

Fields§

§value: u64§num_bits: u8

Implementations§

Source§

impl EncodableData

Source

pub fn get_next_bits_to_encode(&mut self, num_bits_needed: u8) -> u8

Returns num_bits_needed from the front of EncodableData.

Masks and shifts value so the bits in the proper location are returned. If EncodableData has a larger num_bits than bits in the actual value, 0 will be returned. Decreases num_bits by num_bits_needed to keep track of how many bits are left to encode.

num_bits_needed needs to be an integer 1-8 because the max bit size for a character for any encoding system up to base 256 is 8 bits. This function will also throw an error if num_bits_needed is bigger than num_bits.

§Examples:
use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
 
let mut encodable_data = EncodableData {
    value: 10, // value can be represented by 4 bits: 0b1010
    num_bits: 6 // specifying 6 bits means it should be encoded as: 0b001010
};
 
assert_eq!(2, encodable_data.get_next_bits_to_encode(4)); // 0b0010
assert_eq!(2, encodable_data.get_next_bits_to_encode(2)); // 0b10
Source

pub fn has_bits_for_char(self, num_bits_in_char: u8) -> bool

Determines if there are enough bits in num_bits to make a char.

Takes one parameter: num_bits_in_char. num_bits_in_char should be determined by the encoding system e.g. 5 bits for a char in base32 encoding.

§Examples:
use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
 
let encodable_data = EncodableData {
    value: 10,
    num_bits: 6
};
 
assert_eq!(true, encodable_data.has_bits_for_char(5));
Source

pub fn add_bits(&mut self, num_bits_to_add: u8, value_to_add: u8)

Adds value_to_add to value and decrements num_bits by num_bits_to_add.

Intended to be used when decoding a value. value will be left shifted by num_bits_to_add and then value_to_add gets shifted in. This ensures bits can be added in their proper places. num_bits gets decremented to keep track of how many bits are still needed to fill EncodableData.

§Examples:
use amazon_cloudfront_client_routing_lib::client_routing_label::EncodableData;
 
let mut encodable_data = EncodableData {
    value: 0,
    num_bits: 10
};
 
encodable_data.add_bits(6, 21);
assert_eq!(21, encodable_data.value);
 
encodable_data.add_bits(3, 6);
assert_eq!(174, encodable_data.value);

Trait Implementations§

Source§

impl Clone for EncodableData

Source§

fn clone(&self) -> EncodableData

Returns a copy 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 EncodableData

Source§

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

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

impl Copy for EncodableData

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V