Skip to main content

move_stdlib_sdk/
lib.rs

1//! Move types for the core `std` Sui package located at "0x1" onchain.
2
3af_sui_pkg_sdk::sui_pkg_sdk!(std @ "0x1" {
4    /// The `ASCII` module defines basic string and char newtypes in Move that verify
5    /// that characters are valid ASCII, and that strings consist of only valid ASCII characters.
6    module ascii {
7        /// The `String` struct holds a vector of bytes that all represent
8        /// valid ASCII characters. Note that these ASCII characters may not all
9        /// be printable. To determine if a `String` contains only "printable"
10        /// characters you should use the `all_characters_printable` predicate
11        /// defined in this module.
12        #[serde(transparent)]
13        struct String has copy, drop, store {
14            bytes: vector<u8>,
15        }
16
17        /// An ASCII character.
18        struct Char has copy, drop, store {
19            byte: u8,
20        }
21    }
22
23    module bit_vector {
24        struct BitVector has copy, drop, store {
25            length: u64,
26            bit_field: vector<bool>,
27        }
28    }
29
30    /// Defines a fixed-point numeric type with a 32-bit integer part and
31    /// a 32-bit fractional part.
32    module fixed_point32 {
33        /// Define a fixed-point numeric type with 32 fractional bits.
34        /// This is just a u64 integer but it is wrapped in a struct to
35        /// make a unique type. This is a binary representation, so decimal
36        /// values may not be exactly representable, but it provides more
37        /// than 9 decimal digits of precision both before and after the
38        /// decimal point (18 digits total). For comparison, double precision
39        /// floating-point has less than 16 decimal digits of precision, so
40        /// be careful about using floating-point to convert these values to
41        /// decimal.
42        struct FixedPoint32 has copy, drop, store { value: u64 }
43    }
44
45    /// This module defines the Option type and its methods to represent and handle an optional value.
46    module option {
47        /// Abstraction of a value that may or may not be present. Implemented with a vector of size
48        /// zero or one because Move bytecode does not have ADTs.
49        struct Option<Element> has copy, drop, store {
50            vec: vector<Element>
51        }
52    }
53
54    /// The `string` module defines the `String` type which represents UTF8 encoded strings.
55    module string {
56        /// Rust: prefer using Rust's native [`String`](std::string::String) which implements
57        /// `MoveType`.
58        ///
59        /// A `String` holds a sequence of bytes which is guaranteed to be in utf8 format.
60        struct String has copy, drop, store {
61            bytes: vector<u8>,
62        }
63    }
64
65    /// Functionality for converting Move types into values. Use with care!
66    module type_name {
67        struct TypeName has copy, drop, store {
68            /// String representation of the type. All types are represented
69            /// using their source syntax:
70            /// "u8", "u64", "bool", "address", "vector", and so on for primitive types.
71            /// Struct types are represented as fully qualified type names; e.g.
72            /// `00000000000000000000000000000001::string::String` or
73            /// `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>>`
74            /// Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
75            name: String
76        }
77    }
78});
79
80impl TryFrom<String> for ascii::String {
81    type Error = NotAscii;
82
83    fn try_from(value: String) -> Result<Self, Self::Error> {
84        if !value.is_ascii() {
85            return Err(NotAscii);
86        }
87        Ok(Self {
88            bytes: value.bytes().collect::<Vec<_>>().into(),
89        })
90    }
91}
92
93#[derive(thiserror::Error, Debug)]
94#[error("Not an ascii string")]
95pub struct NotAscii;
96
97impl TryFrom<ascii::String> for String {
98    type Error = std::string::FromUtf8Error;
99
100    fn try_from(value: ascii::String) -> Result<Self, Self::Error> {
101        Self::from_utf8(value.bytes.into())
102    }
103}