Struct varuint::Varuint[][src]

pub struct Varuint(pub u128);

Variable length unsigned integer.

Examples

use std::mem;
 
use varuint::{Varuint, Serializable, Deserializable};
use std::io::Read;
 
 
fn test_varuint(v: u128, size: usize) {
 let v = Varuint(v);
 assert_eq!(size, v.size_hint());
 let mut arr: [u8; 17] = unsafe { mem::uninitialized() };
 {
     let mut buf = &mut arr as &mut [u8];
     assert_eq!(size, v.serialize(&mut buf).unwrap());
 }
 let mut buf: &[u8] = &arr;
 let mut read: &mut Read = &mut buf;
 assert_eq!(v, Varuint::deserialize(read).unwrap());
}
 
test_varuint(0, 1);
test_varuint(240, 1);
 
test_varuint(241, 2);
test_varuint(2031, 2);
 
test_varuint(2032, 3);
test_varuint(67567, 3);
 
test_varuint(67568, 4);
test_varuint(16777215, 4);
 
test_varuint(16777216, 5);
test_varuint(4294967295, 5);
 
test_varuint(4294967296, 6);
test_varuint(1099511627775, 6);
 
test_varuint(1099511627776, 7);
test_varuint(281474976710655, 7);
 
test_varuint(281474976710656, 8);
test_varuint(72057594037927935, 8);
 
test_varuint(72057594037927936, 9);
test_varuint(u64::max_value().into(), 9);

test_varuint(u64::max_value() as u128 + 1, 17);
test_varuint(u128::max_value(), 17);

Methods from Deref<Target = u128>

Trait Implementations

impl Copy for Varuint
[src]

impl Clone for Varuint
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for Varuint
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Varuint
[src]

impl Hash for Varuint
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl PartialOrd for Varuint
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Ord for Varuint
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl Debug for Varuint
[src]

Formats the value using the given formatter. Read more

impl Display for Varuint
[src]

Formats the value using the given formatter. Read more

impl Deref for Varuint
[src]

The resulting type after dereferencing.

Dereferences the value.

impl DerefMut for Varuint
[src]

Mutably dereferences the value.

impl From<u8> for Varuint
[src]

Performs the conversion.

impl From<u16> for Varuint
[src]

Performs the conversion.

impl From<u32> for Varuint
[src]

Performs the conversion.

impl From<u64> for Varuint
[src]

Performs the conversion.

impl From<u128> for Varuint
[src]

Performs the conversion.

impl Default for Varuint
[src]

Returns the "default value" for a type. Read more

impl Deserializable for Varuint
[src]

Deserialize value from a Read

impl Serializable for Varuint
[src]

Get a hint of encoded value byte-length

Serialize a value, returns bytes written

Auto Trait Implementations

impl Send for Varuint

impl Sync for Varuint