u16
Expand description
Implementations
pub fn min() -> Self
pub fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [u16] - The smallest
u16
value.
Examples
fn foo() {
let val = u16::min();
assert(val == 0u16);
}
pub fn max() -> Self
pub fn max() -> Self
The largest value that can be represented by this integer type,
216 - 1.
Returns
- [u16] - The largest
u16
value.
Examples
fn foo() {
let val = u16::max();
assert(val == 65535u16);
}
pub fn bits() -> u64
pub fn bits() -> u64
The size of this integer type in bits.
Returns
- [u32] - The number of bits for a
u16
.
Examples
fn foo() {
let bits = u16::bits();
assert(bits == 16);
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the u16
type.
Returns
- [u16] -> The zero value for the
u16
type.
Examples
fn foo() {
let zero_u16 = u16::zero();
assert(zero_u16 == 0u16);
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a u16
is set to zero.
Returns
- [bool] -> True if the
u16
is zero, otherwise false.
Examples
fn foo() {
let zero_u16 = u16::zero();
assert(zero_u16.is_zero());
}
pub fn as_u32(self) -> u32
pub fn as_u32(self) -> u32
Extends a u16
to a u32
.
Returns
- [u32] - The converted
u16
value.
Examples
fn foo() {
let val = 10u16;
let result = val.as_u32();
assert(result == 10u32);
}
pub fn as_u64(self) -> u64
pub fn as_u64(self) -> u64
Extends a u16
to a u64
.
Returns
- [u64] - The converted
u16
value.
Examples
fn foo() {
let val = 10u16;
let result = val.as_u64();
assert(result == 10);
}
pub fn as_u256(self) -> u256
pub fn as_u256(self) -> u256
Extends a u16
to a u256
.
Returns
- [u256] - The converted
u16
value.
Examples
fn foo() {
let val = 2u16;
let result = val.as_u256();
assert(result == 0x0000000000000000000000000000000000000000000000000000000000000002u256);
}
pub fn try_as_u8(self) -> Option<u8>
pub fn try_as_u8(self) -> Option<u8>
Attempts to convert the u16 value into a u8 value.
Additional Information
The max value a u8 can represent is 255.
Returns
[Option] - Some(u8)
if the u16 is less than or equal to the max u8 value. Else None
.
Examples
fn foo() {
let val = 255_u16.try_as_u8();
assert(val == Some(255_u8));
// Conversion fails as value is above the max a u8 can represent.
let val2 = 256_u16.try_as_u8();
assert(val == None);
}
pub fn to_le_bytes(self) -> [u8; 2]
pub fn to_le_bytes(self) -> [u8; 2]
Converts the u16
to a sequence of little-endian bytes.
Returns
- [[u8; 2]] - An array of 2
u8
bytes that compose theu16
.
Examples
fn foo() {
let x: u16 = 513;
let result = x.to_le_bytes();
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
}
pub fn from_le_bytes(bytes: [u8; 2]) -> Self
pub fn from_le_bytes(bytes: [u8; 2]) -> Self
Converts a sequence of little-endian bytes to a u16
.
Arguments
bytes
: [[u8; 2]] - A sequence of 2u8
bytes that represent au16
.
Returns
- [u16] - The resulting
u16
value.
Examples
fn foo() {
let bytes = [1_u8, 2_u8];
let result = u16::from_le_bytes(bytes);
assert(result == 513_u16);
}
pub fn to_be_bytes(self) -> [u8; 2]
pub fn to_be_bytes(self) -> [u8; 2]
Converts the u16
to a sequence of big-endian bytes.
Returns
- [[u8; 2]] - An array of 2
u8
bytes that compose theu16
.
Examples
fn foo() {
let x: u16 = 513;
let result = x.to_be_bytes();
assert(result[0] == 2_u8);
assert(result[1] == 1_u8);
}
pub fn from_be_bytes(bytes: [u8; 2]) -> Self
pub fn from_be_bytes(bytes: [u8; 2]) -> Self
Converts a sequence of big-endian bytes to a u16
.
Arguments
bytes
: [[u8; 2]] - A sequence of 2u8
bytes that represent au16
.
Returns
- [u16] - The resulting
u16
value.
Examples
fn foo() {
let bytes = [2_u8, 1_u8];
let result = u16::from_be_bytes(bytes);
assert(result == 513_u16);
}
pub fn to_le_bytes(self) -> Bytes
pub fn to_le_bytes(self) -> Bytes
Converts the u16
to a sequence of little-endian bytes.
Returns
- [Bytes] - The 2 bytes that compose the
u16
.
Examples
fn foo() {
let x: u16 = 513;
let result = x.to_le_bytes();
assert(result.get(0).unwrap() == 1_u8);
assert(result.get(1).unwrap() == 2_u8);
}
pub fn from_le_bytes(bytes: Bytes) -> Self
pub fn from_le_bytes(bytes: Bytes) -> Self
Converts a sequence of little-endian bytes to a u16
.
Arguments
bytes
: [Bytes] - The 2 bytes that compose theu16
.
Returns
- [u16] - The resulting
u16
value.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(1_u8);
bytes.push(2_u8);
let result = u16::from_le_bytes(bytes);
assert(result == 513_u16);
}
pub fn to_be_bytes(self) -> Bytes
pub fn to_be_bytes(self) -> Bytes
Converts the u16
to a sequence of big-endian bytes.
Returns
- [Bytes] - The 2 bytes that compose the
u16
.
Examples
use std::bytes_conversions::u16;
fn foo() {
let x: u16 = 513;
let result = x.to_be_bytes();
assert(result.get(0).unwrap() == 2_u8);
assert(result.get(1).unwrap() == 1_u8);
}
pub fn from_be_bytes(bytes: Bytes) -> Self
pub fn from_be_bytes(bytes: Bytes) -> Self
Converts a sequence of big-endian bytes to a u16
.
Arguments
bytes
: [Bytes] - The 2 bytes that compose theu16
.
Returns
- [u16] - The resulting
u16
value.
Examples
use std::bytes::Bytes;
fn foo() {
let mut bytes = Bytes::new();
bytes.push(2_u8);
bytes.push(1_u8);
let result = u16::from_be_bytes(bytes);
assert(result == 513_u16);
}
Trait Implementations
impl PartialEq for u16
impl PartialEq for u16
pub fn eq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl PartialEq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}
impl Eq for u16
impl BitwiseAnd for u16
impl BitwiseAnd for u16
pub fn binary_and(self, other: Self) -> Self
impl BitwiseXor for u16
impl BitwiseXor for u16
pub fn binary_xor(self, other: Self) -> Self
impl OrdEq for u16
impl OrdEq for u16
pub fn ge(self, other: Self) -> bool
pub fn ge(self, other: Self) -> bool
Evaluates if one value of the same type is greater or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is greater than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn gt(self, other: Self) -> bool {
self.val > other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 >= struct2;
assert(result);
}
pub fn le(self, other: Self) -> bool
pub fn le(self, other: Self) -> bool
Evaluates if one value of the same type is less or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is less than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn lt(self, other: Self) -> bool {
self.val < other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 <= struct2;
assert(result);
}
impl AbiEncode for u16
impl AbiEncode for u16
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for u16
impl AbiDecode for u16
pub fn abi_decode(refmut buffer: BufferReader) -> u16
impl From<u8> for u16
impl From<u8> for u16
pub fn from(u: u8) -> Self
pub fn from(u: u8) -> Self
Casts a u8
to a u16
.
Returns
- [u16] - The
u16
representation of theu8
value.
Examples
fn foo() {
let u16_value = u16::from(0u8);
}