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
impl EncodableData
Sourcepub fn get_next_bits_to_encode(&mut self, num_bits_needed: u8) -> u8
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
Sourcepub fn has_bits_for_char(self, num_bits_in_char: u8) -> bool
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));
Sourcepub fn add_bits(&mut self, num_bits_to_add: u8, value_to_add: u8)
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
impl Clone for EncodableData
Source§fn clone(&self) -> EncodableData
fn clone(&self) -> EncodableData
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more