Module 0x1::type_name
Functionality for converting Move types into values. Use with care!
- Struct
TypeName
- Constants
- Function
get
- Function
get_with_original_ids
- Function
is_primitive
- Function
borrow_string
- Function
get_address
- Function
get_module
- Function
into_string
use 0x1::address;
use 0x1::ascii;
Struct TypeName
struct TypeName has copy, drop, store
Fields
- name: ascii::String
- String representation of the type. All types are represented using their source syntax: "u8", "u64", "bool", "address", "vector", and so on for primitive types. Struct types are represented as fully qualified type names; e.g. 00000000000000000000000000000001::string::String or 0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>> Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
Constants
ASCII Character code for the c (lowercase c) symbol.
const ASCII_C: u8 = 99;
ASCII Character code for the : (colon) symbol.
const ASCII_COLON: u8 = 58;
ASCII Character code for the e (lowercase e) symbol.
const ASCII_E: u8 = 101;
ASCII Character code for the o (lowercase o) symbol.
const ASCII_O: u8 = 111;
ASCII Character code for the r (lowercase r) symbol.
const ASCII_R: u8 = 114;
ASCII Character code for the t (lowercase t) symbol.
const ASCII_T: u8 = 116;
ASCII Character code for the v (lowercase v) symbol.
const ASCII_V: u8 = 118;
The type is not from a package/module. It is a primitive type.
const ENonModuleType: u64 = 0;
Function get
Return a value representation of the type T. Package IDs that appear in fully qualified type names in the output from this function are defining IDs (the ID of the package in storage that first introduced the type).
public fun get<T>(): type_name::TypeName
Function get_with_original_ids
Return a value representation of the type T. Package IDs that appear in fully qualified type names in the output from this function are original IDs (the ID of the first version of the package, even if the type in question was introduced in a later upgrade).
public fun get_with_original_ids<T>(): type_name::TypeName
Implementation
public native fun get_with_original_ids<T>(): TypeName;
Function is_primitive
Returns true iff the TypeName represents a primitive type, i.e. one of u8, u16, u32, u64, u128, u256, bool, address, vector.
public fun is_primitive(self: &type_name::TypeName): bool
Implementation
public fun is_primitive(self: &TypeName): bool {
let bytes = self.name.as_bytes();
bytes == &b"bool" ||
bytes == &b"u8" ||
bytes == &b"u16" ||
bytes == &b"u32" ||
bytes == &b"u64" ||
bytes == &b"u128" ||
bytes == &b"u256" ||
bytes == &b"address" ||
(bytes.length() >= 6 &&
bytes[0] == ASCII_V &&
bytes[1] == ASCII_E &&
bytes[2] == ASCII_C &&
bytes[3] == ASCII_T &&
bytes[4] == ASCII_O &&
bytes[5] == ASCII_R)
}
Function borrow_string
Get the String representation of self
public fun borrow_string(self: &type_name::TypeName): &ascii::String
Implementation
public fun borrow_string(self: &TypeName): &String {
&self.name
}
Function get_address
Get Address string (Base16 encoded), first part of the TypeName. Aborts if given a primitive type.
public fun get_address(self: &type_name::TypeName): ascii::String
Implementation
public fun get_address(self: &TypeName): String {
assert!(!self.is_primitive(), ENonModuleType);
// Base16 (string) representation of an address has 2 symbols per byte.
let len = address::length() * 2;
let str_bytes = self.name.as_bytes();
let mut addr_bytes = vector[];
let mut i = 0;
// Read `len` bytes from the type name and push them to addr_bytes.
while (i < len) {
addr_bytes.push_back(str_bytes[i]);
i = i + 1;
};
ascii::string(addr_bytes)
}
Function get_module
Get name of the module. Aborts if given a primitive type.
public fun get_module(self: &type_name::TypeName): ascii::String
Implementation
public fun get_module(self: &TypeName): String {
assert!(!self.is_primitive(), ENonModuleType);
// Starts after address and a double colon: `<addr as HEX>::`
let mut i = address::length() * 2 + 2;
let str_bytes = self.name.as_bytes();
let mut module_name = vector[];
let colon = ASCII_COLON;
loop {
let char = &str_bytes[i];
if (char != &colon) {
module_name.push_back(*char);
i = i + 1;
} else {
break
}
};
ascii::string(module_name)
}
Function into_string
Convert self into its inner String
public fun into_string(self: type_name::TypeName): ascii::String
Implementation
public fun into_string(self: TypeName): String {
self.name
}