diff --git a/aptos-move/framework/aptos-stdlib/doc/any.md b/aptos-move/framework/aptos-stdlib/doc/any.md index e8198a8d9bdbd..1a4704525137b 100644 --- a/aptos-move/framework/aptos-stdlib/doc/any.md +++ b/aptos-move/framework/aptos-stdlib/doc/any.md @@ -120,7 +120,7 @@ also required from T. Unpack a value from the Any representation. This aborts if the value has not the expected type T. -
public fun unpack<T>(x: any::Any): T
+
public fun unpack<T>(self: any::Any): T
 
@@ -129,9 +129,9 @@ Unpack a value from the Any repres Implementation -
public fun unpack<T>(x: Any): T {
-    assert!(type_info::type_name<T>() == x.type_name, error::invalid_argument(ETYPE_MISMATCH));
-    from_bytes<T>(x.data)
+
public fun unpack<T>(self: Any): T {
+    assert!(type_info::type_name<T>() == self.type_name, error::invalid_argument(ETYPE_MISMATCH));
+    from_bytes<T>(self.data)
 }
 
@@ -146,7 +146,7 @@ Unpack a value from the Any repres Returns the type name of this Any -
public fun type_name(x: &any::Any): &string::String
+
public fun type_name(self: &any::Any): &string::String
 
@@ -155,8 +155,8 @@ Returns the type name of this Any Implementation -
public fun type_name(x: &Any): &String {
-    &x.type_name
+
public fun type_name(self: &Any): &String {
+    &self.type_name
 }
 
@@ -195,14 +195,14 @@ Returns the type name of this Any ### Function `unpack` -
public fun unpack<T>(x: any::Any): T
+
public fun unpack<T>(self: any::Any): T
 
include UnpackAbortsIf<T>;
-ensures result == from_bcs::deserialize<T>(x.data);
+ensures result == from_bcs::deserialize<T>(self.data);
 
@@ -212,9 +212,9 @@ Returns the type name of this Any
schema UnpackAbortsIf<T> {
-    x: Any;
-    aborts_if type_info::type_name<T>() != x.type_name;
-    aborts_if !from_bcs::deserializable<T>(x.data);
+    self: Any;
+    aborts_if type_info::type_name<T>() != self.type_name;
+    aborts_if !from_bcs::deserializable<T>(self.data);
 }
 
@@ -225,9 +225,9 @@ Returns the type name of this Any
schema UnpackRequirement<T> {
-    x: Any;
-    requires type_info::type_name<T>() == x.type_name;
-    requires from_bcs::deserializable<T>(x.data);
+    self: Any;
+    requires type_info::type_name<T>() == self.type_name;
+    requires from_bcs::deserializable<T>(self.data);
 }
 
@@ -238,14 +238,14 @@ Returns the type name of this Any ### Function `type_name` -
public fun type_name(x: &any::Any): &string::String
+
public fun type_name(self: &any::Any): &string::String
 
aborts_if false;
-ensures result == x.type_name;
+ensures result == self.type_name;
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/big_vector.md b/aptos-move/framework/aptos-stdlib/doc/big_vector.md index cd3c8996c6564..7d785a5b730a1 100644 --- a/aptos-move/framework/aptos-stdlib/doc/big_vector.md +++ b/aptos-move/framework/aptos-stdlib/doc/big_vector.md @@ -197,11 +197,11 @@ Create a vector of length 1 containing the passed in element. ## Function `destroy_empty` -Destroy the vector v. -Aborts if v is not empty. +Destroy the vector self. +Aborts if self is not empty. -
public fun destroy_empty<T>(v: big_vector::BigVector<T>)
+
public fun destroy_empty<T>(self: big_vector::BigVector<T>)
 
@@ -210,9 +210,9 @@ Aborts if v is not empty. Implementation -
public fun destroy_empty<T>(v: BigVector<T>) {
-    assert!(is_empty(&v), error::invalid_argument(EVECTOR_NOT_EMPTY));
-    let BigVector { buckets, end_index: _, bucket_size: _ } = v;
+
public fun destroy_empty<T>(self: BigVector<T>) {
+    assert!(is_empty(&self), error::invalid_argument(EVECTOR_NOT_EMPTY));
+    let BigVector { buckets, end_index: _, bucket_size: _ } = self;
     table_with_length::destroy_empty(buckets);
 }
 
@@ -225,10 +225,10 @@ Aborts if v is not empty. ## Function `destroy` -Destroy the vector v if T has drop +Destroy the vector self if T has drop -
public fun destroy<T: drop>(v: big_vector::BigVector<T>)
+
public fun destroy<T: drop>(self: big_vector::BigVector<T>)
 
@@ -237,8 +237,8 @@ Destroy the vector v if T has drop Implementation -
public fun destroy<T: drop>(v: BigVector<T>) {
-    let BigVector { buckets, end_index, bucket_size: _ } = v;
+
public fun destroy<T: drop>(self: BigVector<T>) {
+    let BigVector { buckets, end_index, bucket_size: _ } = self;
     let i = 0;
     while (end_index > 0) {
         let num_elements = vector::length(&table_with_length::remove(&mut buckets, i));
@@ -257,11 +257,11 @@ Destroy the vector v if T has drop
 
 ## Function `borrow`
 
-Acquire an immutable reference to the ith element of the vector v.
+Acquire an immutable reference to the ith element of the vector self.
 Aborts if i is out of bounds.
 
 
-
public fun borrow<T>(v: &big_vector::BigVector<T>, i: u64): &T
+
public fun borrow<T>(self: &big_vector::BigVector<T>, i: u64): &T
 
@@ -270,9 +270,9 @@ Aborts if i is out of bounds. Implementation -
public fun borrow<T>(v: &BigVector<T>, i: u64): &T {
-    assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    vector::borrow(table_with_length::borrow(&v.buckets, i / v.bucket_size), i % v.bucket_size)
+
public fun borrow<T>(self: &BigVector<T>, i: u64): &T {
+    assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    vector::borrow(table_with_length::borrow(&self.buckets, i / self.bucket_size), i % self.bucket_size)
 }
 
@@ -284,11 +284,11 @@ Aborts if i is out of bounds. ## Function `borrow_mut` -Return a mutable reference to the ith element in the vector v. +Return a mutable reference to the ith element in the vector self. Aborts if i is out of bounds. -
public fun borrow_mut<T>(v: &mut big_vector::BigVector<T>, i: u64): &mut T
+
public fun borrow_mut<T>(self: &mut big_vector::BigVector<T>, i: u64): &mut T
 
@@ -297,9 +297,9 @@ Aborts if i is out of bounds. Implementation -
public fun borrow_mut<T>(v: &mut BigVector<T>, i: u64): &mut T {
-    assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    vector::borrow_mut(table_with_length::borrow_mut(&mut v.buckets, i / v.bucket_size), i % v.bucket_size)
+
public fun borrow_mut<T>(self: &mut BigVector<T>, i: u64): &mut T {
+    assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    vector::borrow_mut(table_with_length::borrow_mut(&mut self.buckets, i / self.bucket_size), i % self.bucket_size)
 }
 
@@ -311,12 +311,12 @@ Aborts if i is out of bounds. ## Function `append` -Empty and destroy the other vector, and push each of the elements in the other vector onto the lhs vector in the +Empty and destroy the other vector, and push each of the elements in the other vector onto the self vector in the same order as they occurred in other. Disclaimer: This function is costly. Use it at your own discretion. -
public fun append<T: store>(lhs: &mut big_vector::BigVector<T>, other: big_vector::BigVector<T>)
+
public fun append<T: store>(self: &mut big_vector::BigVector<T>, other: big_vector::BigVector<T>)
 
@@ -325,16 +325,16 @@ Disclaimer: This function is costly. Use it at your own discretion. Implementation -
public fun append<T: store>(lhs: &mut BigVector<T>, other: BigVector<T>) {
+
public fun append<T: store>(self: &mut BigVector<T>, other: BigVector<T>) {
     let other_len = length(&other);
     let half_other_len = other_len / 2;
     let i = 0;
     while (i < half_other_len) {
-        push_back(lhs, swap_remove(&mut other, i));
+        push_back(self, swap_remove(&mut other, i));
         i = i + 1;
     };
     while (i < other_len) {
-        push_back(lhs, pop_back(&mut other));
+        push_back(self, pop_back(&mut other));
         i = i + 1;
     };
     destroy_empty(other);
@@ -349,11 +349,11 @@ Disclaimer: This function is costly. Use it at your own discretion.
 
 ## Function `push_back`
 
-Add element val to the end of the vector v. It grows the buckets when the current buckets are full.
+Add element val to the end of the vector self. It grows the buckets when the current buckets are full.
 This operation will cost more gas when it adds new bucket.
 
 
-
public fun push_back<T: store>(v: &mut big_vector::BigVector<T>, val: T)
+
public fun push_back<T: store>(self: &mut big_vector::BigVector<T>, val: T)
 
@@ -362,15 +362,15 @@ This operation will cost more gas when it adds new bucket. Implementation -
public fun push_back<T: store>(v: &mut BigVector<T>, val: T) {
-    let num_buckets = table_with_length::length(&v.buckets);
-    if (v.end_index == num_buckets * v.bucket_size) {
-        table_with_length::add(&mut v.buckets, num_buckets, vector::empty());
-        vector::push_back(table_with_length::borrow_mut(&mut v.buckets, num_buckets), val);
+
public fun push_back<T: store>(self: &mut BigVector<T>, val: T) {
+    let num_buckets = table_with_length::length(&self.buckets);
+    if (self.end_index == num_buckets * self.bucket_size) {
+        table_with_length::add(&mut self.buckets, num_buckets, vector::empty());
+        vector::push_back(table_with_length::borrow_mut(&mut self.buckets, num_buckets), val);
     } else {
-        vector::push_back(table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1), val);
+        vector::push_back(table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1), val);
     };
-    v.end_index = v.end_index + 1;
+    self.end_index = self.end_index + 1;
 }
 
@@ -382,12 +382,12 @@ This operation will cost more gas when it adds new bucket. ## Function `pop_back` -Pop an element from the end of vector v. It doesn't shrink the buckets even if they're empty. +Pop an element from the end of vector self. It doesn't shrink the buckets even if they're empty. Call shrink_to_fit explicity to deallocate empty buckets. -Aborts if v is empty. +Aborts if self is empty. -
public fun pop_back<T>(v: &mut big_vector::BigVector<T>): T
+
public fun pop_back<T>(self: &mut big_vector::BigVector<T>): T
 
@@ -396,17 +396,17 @@ Aborts if v is empty. Implementation -
public fun pop_back<T>(v: &mut BigVector<T>): T {
-    assert!(!is_empty(v), error::invalid_state(EVECTOR_EMPTY));
-    let num_buckets = table_with_length::length(&v.buckets);
-    let last_bucket = table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1);
+
public fun pop_back<T>(self: &mut BigVector<T>): T {
+    assert!(!is_empty(self), error::invalid_state(EVECTOR_EMPTY));
+    let num_buckets = table_with_length::length(&self.buckets);
+    let last_bucket = table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1);
     let val = vector::pop_back(last_bucket);
     // Shrink the table if the last vector is empty.
     if (vector::is_empty(last_bucket)) {
         move last_bucket;
-        vector::destroy_empty(table_with_length::remove(&mut v.buckets, num_buckets - 1));
+        vector::destroy_empty(table_with_length::remove(&mut self.buckets, num_buckets - 1));
     };
-    v.end_index = v.end_index - 1;
+    self.end_index = self.end_index - 1;
     val
 }
 
@@ -419,12 +419,12 @@ Aborts if v is empty. ## Function `remove` -Remove the element at index i in the vector v and return the owned value that was previously stored at i in v. +Remove the element at index i in the vector v and return the owned value that was previously stored at i in self. All elements occurring at indices greater than i will be shifted down by 1. Will abort if i is out of bounds. Disclaimer: This function is costly. Use it at your own discretion. -
public fun remove<T>(v: &mut big_vector::BigVector<T>, i: u64): T
+
public fun remove<T>(self: &mut big_vector::BigVector<T>, i: u64): T
 
@@ -433,28 +433,28 @@ Disclaimer: This function is costly. Use it at your own discretion. Implementation -
public fun remove<T>(v: &mut BigVector<T>, i: u64): T {
-    let len = length(v);
+
public fun remove<T>(self: &mut BigVector<T>, i: u64): T {
+    let len = length(self);
     assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let num_buckets = table_with_length::length(&v.buckets);
-    let cur_bucket_index = i / v.bucket_size + 1;
-    let cur_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index - 1);
-    let res = vector::remove(cur_bucket, i % v.bucket_size);
-    v.end_index = v.end_index - 1;
+    let num_buckets = table_with_length::length(&self.buckets);
+    let cur_bucket_index = i / self.bucket_size + 1;
+    let cur_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index - 1);
+    let res = vector::remove(cur_bucket, i % self.bucket_size);
+    self.end_index = self.end_index - 1;
     move cur_bucket;
     while ({
         spec {
             invariant cur_bucket_index <= num_buckets;
-            invariant table_with_length::spec_len(v.buckets) == num_buckets;
+            invariant table_with_length::spec_len(self.buckets) == num_buckets;
         };
         (cur_bucket_index < num_buckets)
     }) {
         // remove one element from the start of current vector
-        let cur_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index);
+        let cur_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index);
         let t = vector::remove(cur_bucket, 0);
         move cur_bucket;
         // and put it at the end of the last one
-        let prev_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index - 1);
+        let prev_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index - 1);
         vector::push_back(prev_bucket, t);
         cur_bucket_index = cur_bucket_index + 1;
     };
@@ -463,10 +463,10 @@ Disclaimer: This function is costly. Use it at your own discretion.
     };
 
     // Shrink the table if the last vector is empty.
-    let last_bucket = table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1);
+    let last_bucket = table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1);
     if (vector::is_empty(last_bucket)) {
         move last_bucket;
-        vector::destroy_empty(table_with_length::remove(&mut v.buckets, num_buckets - 1));
+        vector::destroy_empty(table_with_length::remove(&mut self.buckets, num_buckets - 1));
     };
 
     res
@@ -481,12 +481,12 @@ Disclaimer: This function is costly. Use it at your own discretion.
 
 ## Function `swap_remove`
 
-Swap the ith element of the vector v with the last element and then pop the vector.
+Swap the ith element of the vector self with the last element and then pop the vector.
 This is O(1), but does not preserve ordering of elements in the vector.
 Aborts if i is out of bounds.
 
 
-
public fun swap_remove<T>(v: &mut big_vector::BigVector<T>, i: u64): T
+
public fun swap_remove<T>(self: &mut big_vector::BigVector<T>, i: u64): T
 
@@ -495,20 +495,20 @@ Aborts if i is out of bounds. Implementation -
public fun swap_remove<T>(v: &mut BigVector<T>, i: u64): T {
-    assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let last_val = pop_back(v);
+
public fun swap_remove<T>(self: &mut BigVector<T>, i: u64): T {
+    assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    let last_val = pop_back(self);
     // if the requested value is the last one, return it
-    if (v.end_index == i) {
+    if (self.end_index == i) {
         return last_val
     };
     // because the lack of mem::swap, here we swap remove the requested value from the bucket
     // and append the last_val to the bucket then swap the last bucket val back
-    let bucket = table_with_length::borrow_mut(&mut v.buckets, i / v.bucket_size);
+    let bucket = table_with_length::borrow_mut(&mut self.buckets, i / self.bucket_size);
     let bucket_len = vector::length(bucket);
-    let val = vector::swap_remove(bucket, i % v.bucket_size);
+    let val = vector::swap_remove(bucket, i % self.bucket_size);
     vector::push_back(bucket, last_val);
-    vector::swap(bucket, i % v.bucket_size, bucket_len - 1);
+    vector::swap(bucket, i % self.bucket_size, bucket_len - 1);
     val
 }
 
@@ -521,11 +521,11 @@ Aborts if i is out of bounds. ## Function `swap` -Swap the elements at the i'th and j'th indices in the vector v. Will abort if either of i or j are out of bounds -for v. +Swap the elements at the i'th and j'th indices in the vector self. Will abort if either of i or j are out of bounds +for self. -
public fun swap<T>(v: &mut big_vector::BigVector<T>, i: u64, j: u64)
+
public fun swap<T>(self: &mut big_vector::BigVector<T>, i: u64, j: u64)
 
@@ -534,19 +534,19 @@ for v. Implementation -
public fun swap<T>(v: &mut BigVector<T>, i: u64, j: u64) {
-    assert!(i < length(v) && j < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let i_bucket_index = i / v.bucket_size;
-    let j_bucket_index = j / v.bucket_size;
-    let i_vector_index = i % v.bucket_size;
-    let j_vector_index = j % v.bucket_size;
+
public fun swap<T>(self: &mut BigVector<T>, i: u64, j: u64) {
+    assert!(i < length(self) && j < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    let i_bucket_index = i / self.bucket_size;
+    let j_bucket_index = j / self.bucket_size;
+    let i_vector_index = i % self.bucket_size;
+    let j_vector_index = j % self.bucket_size;
     if (i_bucket_index == j_bucket_index) {
-        vector::swap(table_with_length::borrow_mut(&mut v.buckets, i_bucket_index), i_vector_index, j_vector_index);
+        vector::swap(table_with_length::borrow_mut(&mut self.buckets, i_bucket_index), i_vector_index, j_vector_index);
         return
     };
     // If i and j are in different buckets, take the buckets out first for easy mutation.
-    let bucket_i = table_with_length::remove(&mut v.buckets, i_bucket_index);
-    let bucket_j = table_with_length::remove(&mut v.buckets, j_bucket_index);
+    let bucket_i = table_with_length::remove(&mut self.buckets, i_bucket_index);
+    let bucket_j = table_with_length::remove(&mut self.buckets, j_bucket_index);
     // Get the elements from buckets by calling `swap_remove`.
     let element_i = vector::swap_remove(&mut bucket_i, i_vector_index);
     let element_j = vector::swap_remove(&mut bucket_j, j_vector_index);
@@ -559,8 +559,8 @@ for v.
     vector::swap(&mut bucket_i, i_vector_index, last_index_in_bucket_i);
     vector::swap(&mut bucket_j, j_vector_index, last_index_in_bucket_j);
     // Add back the buckets.
-    table_with_length::add(&mut v.buckets, i_bucket_index, bucket_i);
-    table_with_length::add(&mut v.buckets, j_bucket_index, bucket_j);
+    table_with_length::add(&mut self.buckets, i_bucket_index, bucket_i);
+    table_with_length::add(&mut self.buckets, j_bucket_index, bucket_j);
 }
 
@@ -572,11 +572,11 @@ for v. ## Function `reverse` -Reverse the order of the elements in the vector v in-place. +Reverse the order of the elements in the vector self in-place. Disclaimer: This function is costly. Use it at your own discretion. -
public fun reverse<T>(v: &mut big_vector::BigVector<T>)
+
public fun reverse<T>(self: &mut big_vector::BigVector<T>)
 
@@ -585,17 +585,17 @@ Disclaimer: This function is costly. Use it at your own discretion. Implementation -
public fun reverse<T>(v: &mut BigVector<T>) {
+
public fun reverse<T>(self: &mut BigVector<T>) {
     let new_buckets = vector[];
     let push_bucket = vector[];
-    let num_buckets = table_with_length::length(&v.buckets);
+    let num_buckets = table_with_length::length(&self.buckets);
     let num_buckets_left = num_buckets;
 
     while (num_buckets_left > 0) {
-        let pop_bucket = table_with_length::remove(&mut v.buckets, num_buckets_left - 1);
+        let pop_bucket = table_with_length::remove(&mut self.buckets, num_buckets_left - 1);
         vector::for_each_reverse(pop_bucket, |val| {
             vector::push_back(&mut push_bucket, val);
-            if (vector::length(&push_bucket) == v.bucket_size) {
+            if (vector::length(&push_bucket) == self.bucket_size) {
                 vector::push_back(&mut new_buckets, push_bucket);
                 push_bucket = vector[];
             };
@@ -611,9 +611,9 @@ Disclaimer: This function is costly. Use it at your own discretion.
 
     vector::reverse(&mut new_buckets);
     let i = 0;
-    assert!(table_with_length::length(&v.buckets) == 0, 0);
+    assert!(table_with_length::length(&self.buckets) == 0, 0);
     while (i < num_buckets) {
-        table_with_length::add(&mut v.buckets, i, vector::pop_back(&mut new_buckets));
+        table_with_length::add(&mut self.buckets, i, vector::pop_back(&mut new_buckets));
         i = i + 1;
     };
     vector::destroy_empty(new_buckets);
@@ -628,12 +628,12 @@ Disclaimer: This function is costly. Use it at your own discretion.
 
 ## Function `index_of`
 
-Return the index of the first occurrence of an element in v that is equal to e. Returns (true, index) if such an
+Return the index of the first occurrence of an element in self that is equal to e. Returns (true, index) if such an
 element was found, and (false, 0) otherwise.
 Disclaimer: This function is costly. Use it at your own discretion.
 
 
-
public fun index_of<T>(v: &big_vector::BigVector<T>, val: &T): (bool, u64)
+
public fun index_of<T>(self: &big_vector::BigVector<T>, val: &T): (bool, u64)
 
@@ -642,14 +642,14 @@ Disclaimer: This function is costly. Use it at your own discretion. Implementation -
public fun index_of<T>(v: &BigVector<T>, val: &T): (bool, u64) {
-    let num_buckets = table_with_length::length(&v.buckets);
+
public fun index_of<T>(self: &BigVector<T>, val: &T): (bool, u64) {
+    let num_buckets = table_with_length::length(&self.buckets);
     let bucket_index = 0;
     while (bucket_index < num_buckets) {
-        let cur = table_with_length::borrow(&v.buckets, bucket_index);
+        let cur = table_with_length::borrow(&self.buckets, bucket_index);
         let (found, i) = vector::index_of(cur, val);
         if (found) {
-            return (true, bucket_index * v.bucket_size + i)
+            return (true, bucket_index * self.bucket_size + i)
         };
         bucket_index = bucket_index + 1;
     };
@@ -665,11 +665,11 @@ Disclaimer: This function is costly. Use it at your own discretion.
 
 ## Function `contains`
 
-Return if an element equal to e exists in the vector v.
+Return if an element equal to e exists in the vector self.
 Disclaimer: This function is costly. Use it at your own discretion.
 
 
-
public fun contains<T>(v: &big_vector::BigVector<T>, val: &T): bool
+
public fun contains<T>(self: &big_vector::BigVector<T>, val: &T): bool
 
@@ -678,9 +678,9 @@ Disclaimer: This function is costly. Use it at your own discretion. Implementation -
public fun contains<T>(v: &BigVector<T>, val: &T): bool {
-    if (is_empty(v)) return false;
-    let (exist, _) = index_of(v, val);
+
public fun contains<T>(self: &BigVector<T>, val: &T): bool {
+    if (is_empty(self)) return false;
+    let (exist, _) = index_of(self, val);
     exist
 }
 
@@ -698,7 +698,7 @@ atomic view of the whole vector. Disclaimer: This function may be costly as the big vector may be huge in size. Use it at your own discretion. -
public fun to_vector<T: copy>(v: &big_vector::BigVector<T>): vector<T>
+
public fun to_vector<T: copy>(self: &big_vector::BigVector<T>): vector<T>
 
@@ -707,12 +707,12 @@ Disclaimer: This function may be costly as the big vector may be huge in size. U Implementation -
public fun to_vector<T: copy>(v: &BigVector<T>): vector<T> {
+
public fun to_vector<T: copy>(self: &BigVector<T>): vector<T> {
     let res = vector[];
-    let num_buckets = table_with_length::length(&v.buckets);
+    let num_buckets = table_with_length::length(&self.buckets);
     let i = 0;
     while (i < num_buckets) {
-        vector::append(&mut res, *table_with_length::borrow(&v.buckets, i));
+        vector::append(&mut res, *table_with_length::borrow(&self.buckets, i));
         i = i + 1;
     };
     res
@@ -730,7 +730,7 @@ Disclaimer: This function may be costly as the big vector may be huge in size. U
 Return the length of the vector.
 
 
-
public fun length<T>(v: &big_vector::BigVector<T>): u64
+
public fun length<T>(self: &big_vector::BigVector<T>): u64
 
@@ -739,8 +739,8 @@ Return the length of the vector. Implementation -
public fun length<T>(v: &BigVector<T>): u64 {
-    v.end_index
+
public fun length<T>(self: &BigVector<T>): u64 {
+    self.end_index
 }
 
@@ -755,7 +755,7 @@ Return the length of the vector. Return true if the vector v has no elements and false otherwise. -
public fun is_empty<T>(v: &big_vector::BigVector<T>): bool
+
public fun is_empty<T>(self: &big_vector::BigVector<T>): bool
 
@@ -764,8 +764,8 @@ Return true if the vector v has no elements and Implementation -
public fun is_empty<T>(v: &BigVector<T>): bool {
-    length(v) == 0
+
public fun is_empty<T>(self: &BigVector<T>): bool {
+    length(self) == 0
 }
 
@@ -876,13 +876,13 @@ Return true if the vector v has no elements and ### Function `destroy_empty` -
public fun destroy_empty<T>(v: big_vector::BigVector<T>)
+
public fun destroy_empty<T>(self: big_vector::BigVector<T>)
 
-
aborts_if !is_empty(v);
+
aborts_if !is_empty(self);
 
@@ -892,14 +892,14 @@ Return true if the vector v has no elements and ### Function `borrow` -
public fun borrow<T>(v: &big_vector::BigVector<T>, i: u64): &T
+
public fun borrow<T>(self: &big_vector::BigVector<T>, i: u64): &T
 
-
aborts_if i >= length(v);
-ensures result == spec_at(v, i);
+
aborts_if i >= length(self);
+ensures result == spec_at(self, i);
 
@@ -909,14 +909,14 @@ Return true if the vector v has no elements and ### Function `borrow_mut` -
public fun borrow_mut<T>(v: &mut big_vector::BigVector<T>, i: u64): &mut T
+
public fun borrow_mut<T>(self: &mut big_vector::BigVector<T>, i: u64): &mut T
 
-
aborts_if i >= length(v);
-ensures result == spec_at(v, i);
+
aborts_if i >= length(self);
+ensures result == spec_at(self, i);
 
@@ -926,7 +926,7 @@ Return true if the vector v has no elements and ### Function `append` -
public fun append<T: store>(lhs: &mut big_vector::BigVector<T>, other: big_vector::BigVector<T>)
+
public fun append<T: store>(self: &mut big_vector::BigVector<T>, other: big_vector::BigVector<T>)
 
@@ -942,19 +942,19 @@ Return true if the vector v has no elements and ### Function `push_back` -
public fun push_back<T: store>(v: &mut big_vector::BigVector<T>, val: T)
+
public fun push_back<T: store>(self: &mut big_vector::BigVector<T>, val: T)
 
-
let num_buckets = spec_table_len(v.buckets);
+
let num_buckets = spec_table_len(self.buckets);
 include PushbackAbortsIf<T>;
-ensures length(v) == length(old(v)) + 1;
-ensures v.end_index == old(v.end_index) + 1;
-ensures spec_at(v, v.end_index-1) == val;
-ensures forall i in 0..v.end_index-1: spec_at(v, i) == spec_at(old(v), i);
-ensures v.bucket_size == old(v).bucket_size;
+ensures length(self) == length(old(self)) + 1;
+ensures self.end_index == old(self.end_index) + 1;
+ensures spec_at(self, self.end_index-1) == val;
+ensures forall i in 0..self.end_index-1: spec_at(self, i) == spec_at(old(self), i);
+ensures self.bucket_size == old(self).bucket_size;
 
@@ -964,10 +964,10 @@ Return true if the vector v has no elements and
schema PushbackAbortsIf<T> {
-    v: BigVector<T>;
-    let num_buckets = spec_table_len(v.buckets);
-    aborts_if num_buckets * v.bucket_size > MAX_U64;
-    aborts_if v.end_index + 1 > MAX_U64;
+    self: BigVector<T>;
+    let num_buckets = spec_table_len(self.buckets);
+    aborts_if num_buckets * self.bucket_size > MAX_U64;
+    aborts_if self.end_index + 1 > MAX_U64;
 }
 
@@ -978,16 +978,16 @@ Return true if the vector v has no elements and ### Function `pop_back` -
public fun pop_back<T>(v: &mut big_vector::BigVector<T>): T
+
public fun pop_back<T>(self: &mut big_vector::BigVector<T>): T
 
-
aborts_if is_empty(v);
-ensures length(v) == length(old(v)) - 1;
-ensures result == old(spec_at(v, v.end_index-1));
-ensures forall i in 0..v.end_index: spec_at(v, i) == spec_at(old(v), i);
+
aborts_if is_empty(self);
+ensures length(self) == length(old(self)) - 1;
+ensures result == old(spec_at(self, self.end_index-1));
+ensures forall i in 0..self.end_index: spec_at(self, i) == spec_at(old(self), i);
 
@@ -997,7 +997,7 @@ Return true if the vector v has no elements and ### Function `remove` -
public fun remove<T>(v: &mut big_vector::BigVector<T>, i: u64): T
+
public fun remove<T>(self: &mut big_vector::BigVector<T>, i: u64): T
 
@@ -1013,16 +1013,16 @@ Return true if the vector v has no elements and ### Function `swap_remove` -
public fun swap_remove<T>(v: &mut big_vector::BigVector<T>, i: u64): T
+
public fun swap_remove<T>(self: &mut big_vector::BigVector<T>, i: u64): T
 
pragma verify_duration_estimate = 120;
-aborts_if i >= length(v);
-ensures length(v) == length(old(v)) - 1;
-ensures result == spec_at(old(v), i);
+aborts_if i >= length(self);
+ensures length(self) == length(old(self)) - 1;
+ensures result == spec_at(old(self), i);
 
@@ -1032,20 +1032,20 @@ Return true if the vector v has no elements and ### Function `swap` -
public fun swap<T>(v: &mut big_vector::BigVector<T>, i: u64, j: u64)
+
public fun swap<T>(self: &mut big_vector::BigVector<T>, i: u64, j: u64)
 
pragma verify_duration_estimate = 1000;
-aborts_if i >= length(v) || j >= length(v);
-ensures length(v) == length(old(v));
-ensures spec_at(v, i) == spec_at(old(v), j);
-ensures spec_at(v, j) == spec_at(old(v), i);
-ensures forall idx in 0..length(v)
+aborts_if i >= length(self) || j >= length(self);
+ensures length(self) == length(old(self));
+ensures spec_at(self, i) == spec_at(old(self), j);
+ensures spec_at(self, j) == spec_at(old(self), i);
+ensures forall idx in 0..length(self)
     where idx != i && idx != j:
-    spec_at(v, idx) == spec_at(old(v), idx);
+    spec_at(self, idx) == spec_at(old(self), idx);
 
@@ -1055,7 +1055,7 @@ Return true if the vector v has no elements and ### Function `reverse` -
public fun reverse<T>(v: &mut big_vector::BigVector<T>)
+
public fun reverse<T>(self: &mut big_vector::BigVector<T>)
 
@@ -1071,7 +1071,7 @@ Return true if the vector v has no elements and ### Function `index_of` -
public fun index_of<T>(v: &big_vector::BigVector<T>, val: &T): (bool, u64)
+
public fun index_of<T>(self: &big_vector::BigVector<T>, val: &T): (bool, u64)
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/capability.md b/aptos-move/framework/aptos-stdlib/doc/capability.md index 5daf02b7ad916..2d2aef644e829 100644 --- a/aptos-move/framework/aptos-stdlib/doc/capability.md +++ b/aptos-move/framework/aptos-stdlib/doc/capability.md @@ -400,7 +400,7 @@ Returns the root address associated with the given capability token. Only the ow of the feature can do this. -
public fun root_addr<Feature>(cap: capability::Cap<Feature>, _feature_witness: &Feature): address
+
public fun root_addr<Feature>(self: capability::Cap<Feature>, _feature_witness: &Feature): address
 
@@ -409,8 +409,8 @@ of the feature can do this. Implementation -
public fun root_addr<Feature>(cap: Cap<Feature>, _feature_witness: &Feature): address {
-    cap.root
+
public fun root_addr<Feature>(self: Cap<Feature>, _feature_witness: &Feature): address {
+    self.root
 }
 
@@ -425,7 +425,7 @@ of the feature can do this. Returns the root address associated with the given linear capability token. -
public fun linear_root_addr<Feature>(cap: capability::LinearCap<Feature>, _feature_witness: &Feature): address
+
public fun linear_root_addr<Feature>(self: capability::LinearCap<Feature>, _feature_witness: &Feature): address
 
@@ -434,8 +434,8 @@ Returns the root address associated with the given linear capability token. Implementation -
public fun linear_root_addr<Feature>(cap: LinearCap<Feature>, _feature_witness: &Feature): address {
-    cap.root
+
public fun linear_root_addr<Feature>(self: LinearCap<Feature>, _feature_witness: &Feature): address {
+    self.root
 }
 
@@ -451,7 +451,7 @@ Registers a delegation relation. If the relation already exists, this function d nothing. -
public fun delegate<Feature>(cap: capability::Cap<Feature>, _feature_witness: &Feature, to: &signer)
+
public fun delegate<Feature>(self: capability::Cap<Feature>, _feature_witness: &Feature, to: &signer)
 
@@ -460,12 +460,12 @@ nothing. Implementation -
public fun delegate<Feature>(cap: Cap<Feature>, _feature_witness: &Feature, to: &signer)
+
public fun delegate<Feature>(self: Cap<Feature>, _feature_witness: &Feature, to: &signer)
 acquires CapState {
     let addr = signer::address_of(to);
     if (exists<CapDelegateState<Feature>>(addr)) return;
-    move_to(to, CapDelegateState<Feature> { root: cap.root });
-    add_element(&mut borrow_global_mut<CapState<Feature>>(cap.root).delegates, addr);
+    move_to(to, CapDelegateState<Feature> { root: self.root });
+    add_element(&mut borrow_global_mut<CapState<Feature>>(self.root).delegates, addr);
 }
 
@@ -480,7 +480,7 @@ nothing. Revokes a delegation relation. If no relation exists, this function does nothing. -
public fun revoke<Feature>(cap: capability::Cap<Feature>, _feature_witness: &Feature, from: address)
+
public fun revoke<Feature>(self: capability::Cap<Feature>, _feature_witness: &Feature, from: address)
 
@@ -489,12 +489,12 @@ Revokes a delegation relation. If no relation exists, this function does nothing Implementation -
public fun revoke<Feature>(cap: Cap<Feature>, _feature_witness: &Feature, from: address)
+
public fun revoke<Feature>(self: Cap<Feature>, _feature_witness: &Feature, from: address)
 acquires CapState, CapDelegateState
 {
     if (!exists<CapDelegateState<Feature>>(from)) return;
     let CapDelegateState { root: _root } = move_from<CapDelegateState<Feature>>(from);
-    remove_element(&mut borrow_global_mut<CapState<Feature>>(cap.root).delegates, &from);
+    remove_element(&mut borrow_global_mut<CapState<Feature>>(self.root).delegates, &from);
 }
 
@@ -676,7 +676,7 @@ Helper specification function to check whether a delegated capability exists at ### Function `delegate` -
public fun delegate<Feature>(cap: capability::Cap<Feature>, _feature_witness: &Feature, to: &signer)
+
public fun delegate<Feature>(self: capability::Cap<Feature>, _feature_witness: &Feature, to: &signer)
 
@@ -684,8 +684,8 @@ Helper specification function to check whether a delegated capability exists at
let addr = signer::address_of(to);
 ensures spec_has_delegate_cap<Feature>(addr);
-ensures !old(spec_has_delegate_cap<Feature>(addr)) ==> global<CapDelegateState<Feature>>(addr).root == cap.root;
-ensures !old(spec_has_delegate_cap<Feature>(addr)) ==> vector::spec_contains(spec_delegates<Feature>(cap.root), addr);
+ensures !old(spec_has_delegate_cap<Feature>(addr)) ==> global<CapDelegateState<Feature>>(addr).root == self.root;
+ensures !old(spec_has_delegate_cap<Feature>(addr)) ==> vector::spec_contains(spec_delegates<Feature>(self.root), addr);
 
@@ -695,7 +695,7 @@ Helper specification function to check whether a delegated capability exists at ### Function `revoke` -
public fun revoke<Feature>(cap: capability::Cap<Feature>, _feature_witness: &Feature, from: address)
+
public fun revoke<Feature>(self: capability::Cap<Feature>, _feature_witness: &Feature, from: address)
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/comparator.md b/aptos-move/framework/aptos-stdlib/doc/comparator.md index 1949f5812c5aa..527e13d59a077 100644 --- a/aptos-move/framework/aptos-stdlib/doc/comparator.md +++ b/aptos-move/framework/aptos-stdlib/doc/comparator.md @@ -92,7 +92,7 @@ Provides a framework for comparing two elements -
public fun is_equal(result: &comparator::Result): bool
+
public fun is_equal(self: &comparator::Result): bool
 
@@ -101,8 +101,8 @@ Provides a framework for comparing two elements Implementation -
public fun is_equal(result: &Result): bool {
-    result.inner == EQUAL
+
public fun is_equal(self: &Result): bool {
+    self.inner == EQUAL
 }
 
@@ -116,7 +116,7 @@ Provides a framework for comparing two elements -
public fun is_smaller_than(result: &comparator::Result): bool
+
public fun is_smaller_than(self: &comparator::Result): bool
 
@@ -125,8 +125,8 @@ Provides a framework for comparing two elements Implementation -
public fun is_smaller_than(result: &Result): bool {
-    result.inner == SMALLER
+
public fun is_smaller_than(self: &Result): bool {
+    self.inner == SMALLER
 }
 
@@ -140,7 +140,7 @@ Provides a framework for comparing two elements -
public fun is_greater_than(result: &comparator::Result): bool
+
public fun is_greater_than(self: &comparator::Result): bool
 
@@ -149,8 +149,8 @@ Provides a framework for comparing two elements Implementation -
public fun is_greater_than(result: &Result): bool {
-    result.inner == GREATER
+
public fun is_greater_than(self: &Result): bool {
+    self.inner == GREATER
 }
 
@@ -268,14 +268,14 @@ Provides a framework for comparing two elements ### Function `is_equal` -
public fun is_equal(result: &comparator::Result): bool
+
public fun is_equal(self: &comparator::Result): bool
 
aborts_if false;
-let res = result;
+let res = self;
 ensures result == (res.inner == EQUAL);
 
@@ -286,14 +286,14 @@ Provides a framework for comparing two elements ### Function `is_smaller_than` -
public fun is_smaller_than(result: &comparator::Result): bool
+
public fun is_smaller_than(self: &comparator::Result): bool
 
aborts_if false;
-let res = result;
+let res = self;
 ensures result == (res.inner == SMALLER);
 
@@ -304,14 +304,14 @@ Provides a framework for comparing two elements ### Function `is_greater_than` -
public fun is_greater_than(result: &comparator::Result): bool
+
public fun is_greater_than(self: &comparator::Result): bool
 
aborts_if false;
-let res = result;
+let res = self;
 ensures result == (res.inner == GREATER);
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/copyable_any.md b/aptos-move/framework/aptos-stdlib/doc/copyable_any.md index 7ac120437e652..ab34a75e7e8ed 100644 --- a/aptos-move/framework/aptos-stdlib/doc/copyable_any.md +++ b/aptos-move/framework/aptos-stdlib/doc/copyable_any.md @@ -110,7 +110,7 @@ also required from T. Unpack a value from the Any representation. This aborts if the value has not the expected type T. -
public fun unpack<T>(x: copyable_any::Any): T
+
public fun unpack<T>(self: copyable_any::Any): T
 
@@ -119,9 +119,9 @@ Unpack a value from the Any Implementation -
public fun unpack<T>(x: Any): T {
-    assert!(type_info::type_name<T>() == x.type_name, error::invalid_argument(ETYPE_MISMATCH));
-    from_bytes<T>(x.data)
+
public fun unpack<T>(self: Any): T {
+    assert!(type_info::type_name<T>() == self.type_name, error::invalid_argument(ETYPE_MISMATCH));
+    from_bytes<T>(self.data)
 }
 
@@ -136,7 +136,7 @@ Unpack a value from the Any Returns the type name of this Any -
public fun type_name(x: &copyable_any::Any): &string::String
+
public fun type_name(self: &copyable_any::Any): &string::String
 
@@ -145,8 +145,8 @@ Returns the type name of this Any Implementation -
public fun type_name(x: &Any): &String {
-    &x.type_name
+
public fun type_name(self: &Any): &String {
+    &self.type_name
 }
 
@@ -186,14 +186,14 @@ Returns the type name of this Any ### Function `unpack` -
public fun unpack<T>(x: copyable_any::Any): T
+
public fun unpack<T>(self: copyable_any::Any): T
 
include UnpackAbortsIf<T>;
-ensures result == from_bcs::deserialize<T>(x.data);
+ensures result == from_bcs::deserialize<T>(self.data);
 
@@ -203,9 +203,9 @@ Returns the type name of this Any
schema UnpackAbortsIf<T> {
-    x: Any;
-    aborts_if type_info::type_name<T>() != x.type_name;
-    aborts_if !from_bcs::deserializable<T>(x.data);
+    self: Any;
+    aborts_if type_info::type_name<T>() != self.type_name;
+    aborts_if !from_bcs::deserializable<T>(self.data);
 }
 
@@ -216,14 +216,14 @@ Returns the type name of this Any ### Function `type_name` -
public fun type_name(x: &copyable_any::Any): &string::String
+
public fun type_name(self: &copyable_any::Any): &string::String
 
aborts_if false;
-ensures result == x.type_name;
+ensures result == self.type_name;
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/fixed_point64.md b/aptos-move/framework/aptos-stdlib/doc/fixed_point64.md index 642601dc34a1b..aa572e69c7a73 100644 --- a/aptos-move/framework/aptos-stdlib/doc/fixed_point64.md +++ b/aptos-move/framework/aptos-stdlib/doc/fixed_point64.md @@ -169,10 +169,10 @@ The computed ratio when converting to a sub(x: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64 +
public fun sub(self: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
 
@@ -181,8 +181,8 @@ Returns x - y. x must be not less than y. Implementation -
public fun sub(x: FixedPoint64, y: FixedPoint64): FixedPoint64 {
-    let x_raw = get_raw_value(x);
+
public fun sub(self: FixedPoint64, y: FixedPoint64): FixedPoint64 {
+    let x_raw = get_raw_value(self);
     let y_raw = get_raw_value(y);
     assert!(x_raw >= y_raw, ENEGATIVE_RESULT);
     create_from_raw_value(x_raw - y_raw)
@@ -197,10 +197,10 @@ Returns x - y. x must be not less than y.
 
 ## Function `add`
 
-Returns x + y. The result cannot be greater than MAX_U128.
+Returns self + y. The result cannot be greater than MAX_U128.
 
 
-
public fun add(x: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
+
public fun add(self: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
 
@@ -209,8 +209,8 @@ Returns x + y. The result cannot be greater than MAX_U128. Implementation -
public fun add(x: FixedPoint64, y: FixedPoint64): FixedPoint64 {
-    let x_raw = get_raw_value(x);
+
public fun add(self: FixedPoint64, y: FixedPoint64): FixedPoint64 {
+    let x_raw = get_raw_value(self);
     let y_raw = get_raw_value(y);
     let result = (x_raw as u256) + (y_raw as u256);
     assert!(result <= MAX_U128, ERATIO_OUT_OF_RANGE);
@@ -403,7 +403,7 @@ adding or subtracting FixedPoint64 values, can be done using the raw
 values directly.
 
 
-
public fun get_raw_value(num: fixed_point64::FixedPoint64): u128
+
public fun get_raw_value(self: fixed_point64::FixedPoint64): u128
 
@@ -412,8 +412,8 @@ values directly. Implementation -
public fun get_raw_value(num: FixedPoint64): u128 {
-    num.value
+
public fun get_raw_value(self: FixedPoint64): u128 {
+    self.value
 }
 
@@ -428,7 +428,7 @@ values directly. Returns true if the ratio is zero. -
public fun is_zero(num: fixed_point64::FixedPoint64): bool
+
public fun is_zero(self: fixed_point64::FixedPoint64): bool
 
@@ -437,8 +437,8 @@ Returns true if the ratio is zero. Implementation -
public fun is_zero(num: FixedPoint64): bool {
-    num.value == 0
+
public fun is_zero(self: FixedPoint64): bool {
+    self.value == 0
 }
 
@@ -508,10 +508,10 @@ Returns the larger of the two FixedPoint64 numbers. ## Function `less_or_equal` -Returns true if num1 <= num2 +Returns true if self <= num2 -
public fun less_or_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun less_or_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -520,8 +520,8 @@ Returns true if num1 <= num2 Implementation -
public fun less_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-    num1.value <= num2.value
+
public fun less_or_equal(self: FixedPoint64, num2: FixedPoint64): bool {
+    self.value <= num2.value
 }
 
@@ -533,10 +533,10 @@ Returns true if num1 <= num2 ## Function `less` -Returns true if num1 < num2 +Returns true if self < num2 -
public fun less(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun less(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -545,8 +545,8 @@ Returns true if num1 < num2 Implementation -
public fun less(num1: FixedPoint64, num2: FixedPoint64): bool {
-    num1.value < num2.value
+
public fun less(self: FixedPoint64, num2: FixedPoint64): bool {
+    self.value < num2.value
 }
 
@@ -558,10 +558,10 @@ Returns true if num1 < num2 ## Function `greater_or_equal` -Returns true if num1 >= num2 +Returns true if self >= num2 -
public fun greater_or_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun greater_or_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -570,8 +570,8 @@ Returns true if num1 >= num2 Implementation -
public fun greater_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-    num1.value >= num2.value
+
public fun greater_or_equal(self: FixedPoint64, num2: FixedPoint64): bool {
+    self.value >= num2.value
 }
 
@@ -583,10 +583,10 @@ Returns true if num1 >= num2 ## Function `greater` -Returns true if num1 > num2 +Returns true if self > num2 -
public fun greater(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun greater(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -595,8 +595,8 @@ Returns true if num1 > num2 Implementation -
public fun greater(num1: FixedPoint64, num2: FixedPoint64): bool {
-    num1.value > num2.value
+
public fun greater(self: FixedPoint64, num2: FixedPoint64): bool {
+    self.value > num2.value
 }
 
@@ -608,10 +608,10 @@ Returns true if num1 > num2 ## Function `equal` -Returns true if num1 = num2 +Returns true if self = num2 -
public fun equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -620,8 +620,8 @@ Returns true if num1 = num2 Implementation -
public fun equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-    num1.value == num2.value
+
public fun equal(self: FixedPoint64, num2: FixedPoint64): bool {
+    self.value == num2.value
 }
 
@@ -633,10 +633,10 @@ Returns true if num1 = num2 ## Function `almost_equal` -Returns true if num1 almost equals to num2, which means abs(num1-num2) <= precision +Returns true if self almost equals to num2, which means abs(num1-num2) <= precision -
public fun almost_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64, precision: fixed_point64::FixedPoint64): bool
+
public fun almost_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64, precision: fixed_point64::FixedPoint64): bool
 
@@ -645,11 +645,11 @@ Returns true if num1 almost equals to num2, which means abs(num1-num2) <= precis Implementation -
public fun almost_equal(num1: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool {
-    if (num1.value > num2.value) {
-        (num1.value - num2.value <= precision.value)
+
public fun almost_equal(self: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool {
+    if (self.value > num2.value) {
+        (self.value - num2.value <= precision.value)
     } else {
-        (num2.value - num1.value <= precision.value)
+        (num2.value - self.value <= precision.value)
     }
 }
 
@@ -692,7 +692,7 @@ Create a fixedpoint value from a u128 value. Returns the largest integer less than or equal to a given number. -
public fun floor(num: fixed_point64::FixedPoint64): u128
+
public fun floor(self: fixed_point64::FixedPoint64): u128
 
@@ -701,8 +701,8 @@ Returns the largest integer less than or equal to a given number. Implementation -
public fun floor(num: FixedPoint64): u128 {
-    num.value >> 64
+
public fun floor(self: FixedPoint64): u128 {
+    self.value >> 64
 }
 
@@ -717,7 +717,7 @@ Returns the largest integer less than or equal to a given number. Rounds up the given FixedPoint64 to the next largest integer. -
public fun ceil(num: fixed_point64::FixedPoint64): u128
+
public fun ceil(self: fixed_point64::FixedPoint64): u128
 
@@ -726,9 +726,9 @@ Rounds up the given FixedPoint64 to the next largest integer. Implementation -
public fun ceil(num: FixedPoint64): u128 {
-    let floored_num = floor(num) << 64;
-    if (num.value == floored_num) {
+
public fun ceil(self: FixedPoint64): u128 {
+    let floored_num = floor(self) << 64;
+    if (self.value == floored_num) {
         return floored_num >> 64
     };
     let val = ((floored_num as u256) + (1 << 64));
@@ -747,7 +747,7 @@ Rounds up the given FixedPoint64 to the next largest integer.
 Returns the value of a FixedPoint64 to the nearest integer.
 
 
-
public fun round(num: fixed_point64::FixedPoint64): u128
+
public fun round(self: fixed_point64::FixedPoint64): u128
 
@@ -756,13 +756,13 @@ Returns the value of a FixedPoint64 to the nearest integer. Implementation -
public fun round(num: FixedPoint64): u128 {
-    let floored_num = floor(num) << 64;
+
public fun round(self: FixedPoint64): u128 {
+    let floored_num = floor(self) << 64;
     let boundary = floored_num + ((1 << 64) / 2);
-    if (num.value < boundary) {
+    if (self.value < boundary) {
         floored_num >> 64
     } else {
-        ceil(num)
+        ceil(self)
     }
 }
 
@@ -788,15 +788,15 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `sub` -
public fun sub(x: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
+
public fun sub(self: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
 
pragma opaque;
-aborts_if x.value < y.value with ENEGATIVE_RESULT;
-ensures result.value == x.value - y.value;
+aborts_if self.value < y.value with ENEGATIVE_RESULT;
+ensures result.value == self.value - y.value;
 
@@ -806,15 +806,15 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `add` -
public fun add(x: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
+
public fun add(self: fixed_point64::FixedPoint64, y: fixed_point64::FixedPoint64): fixed_point64::FixedPoint64
 
pragma opaque;
-aborts_if (x.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE;
-ensures result.value == x.value + y.value;
+aborts_if (self.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE;
+ensures result.value == self.value + y.value;
 
@@ -1041,7 +1041,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `less_or_equal` -
public fun less_or_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun less_or_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -1049,7 +1049,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_less_or_equal(num1, num2);
+ensures result == spec_less_or_equal(self, num2);
 
@@ -1058,8 +1058,8 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_less_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-   num1.value <= num2.value
+
fun spec_less_or_equal(self: FixedPoint64, num2: FixedPoint64): bool {
+   self.value <= num2.value
 }
 
@@ -1070,7 +1070,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `less` -
public fun less(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun less(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -1078,7 +1078,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_less(num1, num2);
+ensures result == spec_less(self, num2);
 
@@ -1087,8 +1087,8 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_less(num1: FixedPoint64, num2: FixedPoint64): bool {
-   num1.value < num2.value
+
fun spec_less(self: FixedPoint64, num2: FixedPoint64): bool {
+   self.value < num2.value
 }
 
@@ -1099,7 +1099,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `greater_or_equal` -
public fun greater_or_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun greater_or_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -1107,7 +1107,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_greater_or_equal(num1, num2);
+ensures result == spec_greater_or_equal(self, num2);
 
@@ -1116,8 +1116,8 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_greater_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-   num1.value >= num2.value
+
fun spec_greater_or_equal(self: FixedPoint64, num2: FixedPoint64): bool {
+   self.value >= num2.value
 }
 
@@ -1128,7 +1128,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `greater` -
public fun greater(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun greater(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -1136,7 +1136,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_greater(num1, num2);
+ensures result == spec_greater(self, num2);
 
@@ -1145,8 +1145,8 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_greater(num1: FixedPoint64, num2: FixedPoint64): bool {
-   num1.value > num2.value
+
fun spec_greater(self: FixedPoint64, num2: FixedPoint64): bool {
+   self.value > num2.value
 }
 
@@ -1157,7 +1157,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `equal` -
public fun equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
+
public fun equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64): bool
 
@@ -1165,7 +1165,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_equal(num1, num2);
+ensures result == spec_equal(self, num2);
 
@@ -1174,8 +1174,8 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_equal(num1: FixedPoint64, num2: FixedPoint64): bool {
-   num1.value == num2.value
+
fun spec_equal(self: FixedPoint64, num2: FixedPoint64): bool {
+   self.value == num2.value
 }
 
@@ -1186,7 +1186,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `almost_equal` -
public fun almost_equal(num1: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64, precision: fixed_point64::FixedPoint64): bool
+
public fun almost_equal(self: fixed_point64::FixedPoint64, num2: fixed_point64::FixedPoint64, precision: fixed_point64::FixedPoint64): bool
 
@@ -1194,7 +1194,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_almost_equal(num1, num2, precision);
+ensures result == spec_almost_equal(self, num2, precision);
 
@@ -1203,11 +1203,11 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_almost_equal(num1: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool {
-   if (num1.value > num2.value) {
-       (num1.value - num2.value <= precision.value)
+
fun spec_almost_equal(self: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool {
+   if (self.value > num2.value) {
+       (self.value - num2.value <= precision.value)
    } else {
-       (num2.value - num1.value <= precision.value)
+       (num2.value - self.value <= precision.value)
    }
 }
 
@@ -1261,7 +1261,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `floor` -
public fun floor(num: fixed_point64::FixedPoint64): u128
+
public fun floor(self: fixed_point64::FixedPoint64): u128
 
@@ -1269,7 +1269,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_floor(num);
+ensures result == spec_floor(self);
 
@@ -1278,12 +1278,12 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_floor(val: FixedPoint64): u128 {
-   let fractional = val.value % (1 << 64);
+
fun spec_floor(self: FixedPoint64): u128 {
+   let fractional = self.value % (1 << 64);
    if (fractional == 0) {
-       val.value >> 64
+       self.value >> 64
    } else {
-       (val.value - fractional) >> 64
+       (self.value - fractional) >> 64
    }
 }
 
@@ -1295,7 +1295,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `ceil` -
public fun ceil(num: fixed_point64::FixedPoint64): u128
+
public fun ceil(self: fixed_point64::FixedPoint64): u128
 
@@ -1304,7 +1304,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma verify_duration_estimate = 1000;
 pragma opaque;
 aborts_if false;
-ensures result == spec_ceil(num);
+ensures result == spec_ceil(self);
 
@@ -1313,13 +1313,13 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_ceil(val: FixedPoint64): u128 {
-   let fractional = val.value % (1 << 64);
+
fun spec_ceil(self: FixedPoint64): u128 {
+   let fractional = self.value % (1 << 64);
    let one = 1 << 64;
    if (fractional == 0) {
-       val.value >> 64
+       self.value >> 64
    } else {
-       (val.value - fractional + one) >> 64
+       (self.value - fractional + one) >> 64
    }
 }
 
@@ -1331,7 +1331,7 @@ Returns the value of a FixedPoint64 to the nearest integer. ### Function `round` -
public fun round(num: fixed_point64::FixedPoint64): u128
+
public fun round(self: fixed_point64::FixedPoint64): u128
 
@@ -1339,7 +1339,7 @@ Returns the value of a FixedPoint64 to the nearest integer.
pragma opaque;
 aborts_if false;
-ensures result == spec_round(num);
+ensures result == spec_round(self);
 
@@ -1348,14 +1348,14 @@ Returns the value of a FixedPoint64 to the nearest integer. -
fun spec_round(val: FixedPoint64): u128 {
-   let fractional = val.value % (1 << 64);
+
fun spec_round(self: FixedPoint64): u128 {
+   let fractional = self.value % (1 << 64);
    let boundary = (1 << 64) / 2;
    let one = 1 << 64;
    if (fractional < boundary) {
-       (val.value - fractional) >> 64
+       (self.value - fractional) >> 64
    } else {
-       (val.value - fractional + one) >> 64
+       (self.value - fractional + one) >> 64
    }
 }
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/from_bcs.md b/aptos-move/framework/aptos-stdlib/doc/from_bcs.md index f0a71b713d1bf..cdcd6b3e1223e 100644 --- a/aptos-move/framework/aptos-stdlib/doc/from_bcs.md +++ b/aptos-move/framework/aptos-stdlib/doc/from_bcs.md @@ -5,7 +5,7 @@ This module provides a number of functions to convert _primitive_ types from their representation in std::bcs to values. This is the opposite of bcs::to_bytes. Note that it is not safe to define a generic public from_bytes -function because this can violate implicit struct invariants, therefore only primitive types are offerred. If +function because this can violate implicit struct invariants, therefore only primitive types are offered. If a general conversion back-and-force is needed, consider the aptos_std::Any type which preserves invariants. Example: diff --git a/aptos-move/framework/aptos-stdlib/doc/math128.md b/aptos-move/framework/aptos-stdlib/doc/math128.md index 0160568fc30b7..1f84365d1a599 100644 --- a/aptos-move/framework/aptos-stdlib/doc/math128.md +++ b/aptos-move/framework/aptos-stdlib/doc/math128.md @@ -11,6 +11,7 @@ Standard math utilities missing in the Move Language. - [Function `min`](#0x1_math128_min) - [Function `average`](#0x1_math128_average) - [Function `gcd`](#0x1_math128_gcd) +- [Function `lcm`](#0x1_math128_lcm) - [Function `mul_div`](#0x1_math128_mul_div) - [Function `clamp`](#0x1_math128_clamp) - [Function `pow`](#0x1_math128_pow) @@ -159,6 +160,35 @@ Return greatest common divisor of a & b, via the Eucli + + + + +## Function `lcm` + +Return least common multiple of a & b + + +
public fun lcm(a: u128, b: u128): u128
+
+ + + +
+Implementation + + +
public inline fun lcm(a: u128, b: u128): u128 {
+    if (a == 0 || b == 0) {
+        0
+    } else {
+        a / gcd(a, b) * b
+    }
+}
+
+ + +
diff --git a/aptos-move/framework/aptos-stdlib/doc/math64.md b/aptos-move/framework/aptos-stdlib/doc/math64.md index b40a67a25860f..bbccd3c2ab032 100644 --- a/aptos-move/framework/aptos-stdlib/doc/math64.md +++ b/aptos-move/framework/aptos-stdlib/doc/math64.md @@ -11,6 +11,7 @@ Standard math utilities missing in the Move Language. - [Function `min`](#0x1_math64_min) - [Function `average`](#0x1_math64_average) - [Function `gcd`](#0x1_math64_gcd) +- [Function `lcm`](#0x1_math64_lcm) - [Function `mul_div`](#0x1_math64_mul_div) - [Function `clamp`](#0x1_math64_clamp) - [Function `pow`](#0x1_math64_pow) @@ -157,6 +158,35 @@ Return greatest common divisor of a & b, via the Eucli + + + + +## Function `lcm` + +Returns least common multiple of a & b. + + +
public fun lcm(a: u64, b: u64): u64
+
+ + + +
+Implementation + + +
public inline fun lcm(a: u64, b: u64): u64 {
+    if (a == 0 || b == 0) {
+        0
+    } else {
+        a / gcd(a, b) * b
+    }
+}
+
+ + +
diff --git a/aptos-move/framework/aptos-stdlib/doc/pool_u64.md b/aptos-move/framework/aptos-stdlib/doc/pool_u64.md index a9e3d17c22eb1..2eccd5b2ea3a6 100644 --- a/aptos-move/framework/aptos-stdlib/doc/pool_u64.md +++ b/aptos-move/framework/aptos-stdlib/doc/pool_u64.md @@ -299,7 +299,7 @@ Create a new pool with custom scaling_factor. Destroy an empty pool. This will fail if the pool has any balance of coins. -
public fun destroy_empty(pool: pool_u64::Pool)
+
public fun destroy_empty(self: pool_u64::Pool)
 
@@ -308,8 +308,8 @@ Destroy an empty pool. This will fail if the pool has any balance of coins. Implementation -
public fun destroy_empty(pool: Pool) {
-    assert!(pool.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY));
+
public fun destroy_empty(self: Pool) {
+    assert!(self.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY));
     let Pool {
         shareholders_limit: _,
         total_coins: _,
@@ -317,7 +317,7 @@ Destroy an empty pool. This will fail if the pool has any balance of coins.
         shares: _,
         shareholders: _,
         scaling_factor: _,
-    } = pool;
+    } = self;
 }
 
@@ -329,10 +329,10 @@ Destroy an empty pool. This will fail if the pool has any balance of coins. ## Function `total_coins` -Return pool's total balance of coins. +Return self's total balance of coins. -
public fun total_coins(pool: &pool_u64::Pool): u64
+
public fun total_coins(self: &pool_u64::Pool): u64
 
@@ -341,8 +341,8 @@ Return pool's total balance of coins. Implementation -
public fun total_coins(pool: &Pool): u64 {
-    pool.total_coins
+
public fun total_coins(self: &Pool): u64 {
+    self.total_coins
 }
 
@@ -354,10 +354,10 @@ Return pool's total balance of coins. ## Function `total_shares` -Return the total number of shares across all shareholders in pool. +Return the total number of shares across all shareholders in self. -
public fun total_shares(pool: &pool_u64::Pool): u64
+
public fun total_shares(self: &pool_u64::Pool): u64
 
@@ -366,8 +366,8 @@ Return the total number of shares across all shareholders in pool. Implementation -
public fun total_shares(pool: &Pool): u64 {
-    pool.total_shares
+
public fun total_shares(self: &Pool): u64 {
+    self.total_shares
 }
 
@@ -379,10 +379,10 @@ Return the total number of shares across all shareholders in pool. ## Function `contains` -Return true if shareholder is in pool. +Return true if shareholder is in self. -
public fun contains(pool: &pool_u64::Pool, shareholder: address): bool
+
public fun contains(self: &pool_u64::Pool, shareholder: address): bool
 
@@ -391,8 +391,8 @@ Return true if shareholder is in pool. Implementation -
public fun contains(pool: &Pool, shareholder: address): bool {
-    simple_map::contains_key(&pool.shares, &shareholder)
+
public fun contains(self: &Pool, shareholder: address): bool {
+    simple_map::contains_key(&self.shares, &shareholder)
 }
 
@@ -404,10 +404,10 @@ Return true if shareholder is in pool. ## Function `shares` -Return the number of shares of stakeholder in pool. +Return the number of shares of stakeholder in self. -
public fun shares(pool: &pool_u64::Pool, shareholder: address): u64
+
public fun shares(self: &pool_u64::Pool, shareholder: address): u64
 
@@ -416,9 +416,9 @@ Return the number of shares of stakeholder in pool. Implementation -
public fun shares(pool: &Pool, shareholder: address): u64 {
-    if (contains(pool, shareholder)) {
-        *simple_map::borrow(&pool.shares, &shareholder)
+
public fun shares(self: &Pool, shareholder: address): u64 {
+    if (contains(self, shareholder)) {
+        *simple_map::borrow(&self.shares, &shareholder)
     } else {
         0
     }
@@ -433,10 +433,10 @@ Return the number of shares of stakeholder in pool.
 
 ## Function `balance`
 
-Return the balance in coins of shareholder in pool.
+Return the balance in coins of shareholder in self.
 
 
-
public fun balance(pool: &pool_u64::Pool, shareholder: address): u64
+
public fun balance(self: &pool_u64::Pool, shareholder: address): u64
 
@@ -445,9 +445,9 @@ Return the balance in coins of shareholder in pool. Implementation -
public fun balance(pool: &Pool, shareholder: address): u64 {
-    let num_shares = shares(pool, shareholder);
-    shares_to_amount(pool, num_shares)
+
public fun balance(self: &Pool, shareholder: address): u64 {
+    let num_shares = shares(self, shareholder);
+    shares_to_amount(self, num_shares)
 }
 
@@ -459,10 +459,10 @@ Return the balance in coins of shareholder in pool. ## Function `shareholders` -Return the list of shareholders in pool. +Return the list of shareholders in self. -
public fun shareholders(pool: &pool_u64::Pool): vector<address>
+
public fun shareholders(self: &pool_u64::Pool): vector<address>
 
@@ -471,8 +471,8 @@ Return the list of shareholders in pool. Implementation -
public fun shareholders(pool: &Pool): vector<address> {
-    pool.shareholders
+
public fun shareholders(self: &Pool): vector<address> {
+    self.shareholders
 }
 
@@ -484,10 +484,10 @@ Return the list of shareholders in pool. ## Function `shareholders_count` -Return the number of shareholders in pool. +Return the number of shareholders in self. -
public fun shareholders_count(pool: &pool_u64::Pool): u64
+
public fun shareholders_count(self: &pool_u64::Pool): u64
 
@@ -496,8 +496,8 @@ Return the number of shareholders in pool. Implementation -
public fun shareholders_count(pool: &Pool): u64 {
-    vector::length(&pool.shareholders)
+
public fun shareholders_count(self: &Pool): u64 {
+    vector::length(&self.shareholders)
 }
 
@@ -509,10 +509,10 @@ Return the number of shareholders in pool. ## Function `update_total_coins` -Update pool's total balance of coins. +Update self's total balance of coins. -
public fun update_total_coins(pool: &mut pool_u64::Pool, new_total_coins: u64)
+
public fun update_total_coins(self: &mut pool_u64::Pool, new_total_coins: u64)
 
@@ -521,8 +521,8 @@ Update pool's total balance of coins. Implementation -
public fun update_total_coins(pool: &mut Pool, new_total_coins: u64) {
-    pool.total_coins = new_total_coins;
+
public fun update_total_coins(self: &mut Pool, new_total_coins: u64) {
+    self.total_coins = new_total_coins;
 }
 
@@ -537,7 +537,7 @@ Update pool's total balance of coins. Allow an existing or new shareholder to add their coins to the pool in exchange for new shares. -
public fun buy_in(pool: &mut pool_u64::Pool, shareholder: address, coins_amount: u64): u64
+
public fun buy_in(self: &mut pool_u64::Pool, shareholder: address, coins_amount: u64): u64
 
@@ -546,16 +546,16 @@ Allow an existing or new shareholder to add their coins to the pool in exchange Implementation -
public fun buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u64 {
+
public fun buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u64 {
     if (coins_amount == 0) return 0;
 
-    let new_shares = amount_to_shares(pool, coins_amount);
-    assert!(MAX_U64 - pool.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
-    assert!(MAX_U64 - pool.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
+    let new_shares = amount_to_shares(self, coins_amount);
+    assert!(MAX_U64 - self.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
+    assert!(MAX_U64 - self.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
 
-    pool.total_coins = pool.total_coins + coins_amount;
-    pool.total_shares = pool.total_shares + new_shares;
-    add_shares(pool, shareholder, new_shares);
+    self.total_coins = self.total_coins + coins_amount;
+    self.total_shares = self.total_shares + new_shares;
+    add_shares(self, shareholder, new_shares);
     new_shares
 }
 
@@ -568,11 +568,11 @@ Allow an existing or new shareholder to add their coins to the pool in exchange ## Function `add_shares` -Add the number of shares directly for shareholder in pool. +Add the number of shares directly for shareholder in self. This would dilute other shareholders if the pool's balance of coins didn't change. -
fun add_shares(pool: &mut pool_u64::Pool, shareholder: address, new_shares: u64): u64
+
fun add_shares(self: &mut pool_u64::Pool, shareholder: address, new_shares: u64): u64
 
@@ -581,9 +581,9 @@ This would dilute other shareholders if the pool's balance of coins didn't chang Implementation -
fun add_shares(pool: &mut Pool, shareholder: address, new_shares: u64): u64 {
-    if (contains(pool, shareholder)) {
-        let existing_shares = simple_map::borrow_mut(&mut pool.shares, &shareholder);
+
fun add_shares(self: &mut Pool, shareholder: address, new_shares: u64): u64 {
+    if (contains(self, shareholder)) {
+        let existing_shares = simple_map::borrow_mut(&mut self.shares, &shareholder);
         let current_shares = *existing_shares;
         assert!(MAX_U64 - current_shares >= new_shares, error::invalid_argument(ESHAREHOLDER_SHARES_OVERFLOW));
 
@@ -591,12 +591,12 @@ This would dilute other shareholders if the pool's balance of coins didn't chang
         *existing_shares
     } else if (new_shares > 0) {
         assert!(
-            vector::length(&pool.shareholders) < pool.shareholders_limit,
+            vector::length(&self.shareholders) < self.shareholders_limit,
             error::invalid_state(ETOO_MANY_SHAREHOLDERS),
         );
 
-        vector::push_back(&mut pool.shareholders, shareholder);
-        simple_map::add(&mut pool.shares, shareholder, new_shares);
+        vector::push_back(&mut self.shareholders, shareholder);
+        simple_map::add(&mut self.shares, shareholder, new_shares);
         new_shares
     } else {
         new_shares
@@ -612,10 +612,10 @@ This would dilute other shareholders if the pool's balance of coins didn't chang
 
 ## Function `redeem_shares`
 
-Allow shareholder to redeem their shares in pool for coins.
+Allow shareholder to redeem their shares in self for coins.
 
 
-
public fun redeem_shares(pool: &mut pool_u64::Pool, shareholder: address, shares_to_redeem: u64): u64
+
public fun redeem_shares(self: &mut pool_u64::Pool, shareholder: address, shares_to_redeem: u64): u64
 
@@ -624,16 +624,16 @@ Allow shareholder to redeem their shares in pool for c Implementation -
public fun redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 {
-    assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES));
+
public fun redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 {
+    assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES));
 
     if (shares_to_redeem == 0) return 0;
 
-    let redeemed_coins = shares_to_amount(pool, shares_to_redeem);
-    pool.total_coins = pool.total_coins - redeemed_coins;
-    pool.total_shares = pool.total_shares - shares_to_redeem;
-    deduct_shares(pool, shareholder, shares_to_redeem);
+    let redeemed_coins = shares_to_amount(self, shares_to_redeem);
+    self.total_coins = self.total_coins - redeemed_coins;
+    self.total_shares = self.total_shares - shares_to_redeem;
+    deduct_shares(self, shareholder, shares_to_redeem);
 
     redeemed_coins
 }
@@ -650,7 +650,7 @@ Allow shareholder to redeem their shares in pool for c
 Transfer shares from shareholder_1 to shareholder_2.
 
 
-
public fun transfer_shares(pool: &mut pool_u64::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64)
+
public fun transfer_shares(self: &mut pool_u64::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64)
 
@@ -660,17 +660,17 @@ Transfer shares from shareholder_1 to shareholder_2.
public fun transfer_shares(
-    pool: &mut Pool,
+    self: &mut Pool,
     shareholder_1: address,
     shareholder_2: address,
     shares_to_transfer: u64,
 ) {
-    assert!(contains(pool, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES));
+    assert!(contains(self, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES));
     if (shares_to_transfer == 0) return;
 
-    deduct_shares(pool, shareholder_1, shares_to_transfer);
-    add_shares(pool, shareholder_2, shares_to_transfer);
+    deduct_shares(self, shareholder_1, shares_to_transfer);
+    add_shares(self, shareholder_2, shares_to_transfer);
 }
 
@@ -682,10 +682,10 @@ Transfer shares from shareholder_1 to shareholder_2. ## Function `deduct_shares` -Directly deduct shareholder's number of shares in pool and return the number of remaining shares. +Directly deduct shareholder's number of shares in self and return the number of remaining shares. -
fun deduct_shares(pool: &mut pool_u64::Pool, shareholder: address, num_shares: u64): u64
+
fun deduct_shares(self: &mut pool_u64::Pool, shareholder: address, num_shares: u64): u64
 
@@ -694,19 +694,19 @@ Directly deduct shareholder's number of shares in pool Implementation -
fun deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u64): u64 {
-    assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES));
+
fun deduct_shares(self: &mut Pool, shareholder: address, num_shares: u64): u64 {
+    assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES));
 
-    let existing_shares = simple_map::borrow_mut(&mut pool.shares, &shareholder);
+    let existing_shares = simple_map::borrow_mut(&mut self.shares, &shareholder);
     *existing_shares = *existing_shares - num_shares;
 
     // Remove the shareholder completely if they have no shares left.
     let remaining_shares = *existing_shares;
     if (remaining_shares == 0) {
-        let (_, shareholder_index) = vector::index_of(&pool.shareholders, &shareholder);
-        vector::remove(&mut pool.shareholders, shareholder_index);
-        simple_map::remove(&mut pool.shares, &shareholder);
+        let (_, shareholder_index) = vector::index_of(&self.shareholders, &shareholder);
+        vector::remove(&mut self.shareholders, shareholder_index);
+        simple_map::remove(&mut self.shares, &shareholder);
     };
 
     remaining_shares
@@ -721,11 +721,11 @@ Directly deduct shareholder's number of shares in pool
 
 ## Function `amount_to_shares`
 
-Return the number of new shares coins_amount can buy in pool.
+Return the number of new shares coins_amount can buy in self.
 amount needs to big enough to avoid rounding number.
 
 
-
public fun amount_to_shares(pool: &pool_u64::Pool, coins_amount: u64): u64
+
public fun amount_to_shares(self: &pool_u64::Pool, coins_amount: u64): u64
 
@@ -734,8 +734,8 @@ Return the number of new shares coins_amount can buy in pool< Implementation -
public fun amount_to_shares(pool: &Pool, coins_amount: u64): u64 {
-    amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins)
+
public fun amount_to_shares(self: &Pool, coins_amount: u64): u64 {
+    amount_to_shares_with_total_coins(self, coins_amount, self.total_coins)
 }
 
@@ -747,11 +747,11 @@ Return the number of new shares coins_amount can buy in pool< ## Function `amount_to_shares_with_total_coins` -Return the number of new shares coins_amount can buy in pool with a custom total coins number. +Return the number of new shares coins_amount can buy in self with a custom total coins number. amount needs to big enough to avoid rounding number. -
public fun amount_to_shares_with_total_coins(pool: &pool_u64::Pool, coins_amount: u64, total_coins: u64): u64
+
public fun amount_to_shares_with_total_coins(self: &pool_u64::Pool, coins_amount: u64, total_coins: u64): u64
 
@@ -760,17 +760,17 @@ Return the number of new shares coins_amount can buy in pool< Implementation -
public fun amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u64 {
+
public fun amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u64 {
     // No shares yet so amount is worth the same number of shares.
-    if (pool.total_coins == 0 || pool.total_shares == 0) {
+    if (self.total_coins == 0 || self.total_shares == 0) {
         // Multiply by scaling factor to minimize rounding errors during internal calculations for buy ins/redeems.
         // This can overflow but scaling factor is expected to be chosen carefully so this would not overflow.
-        coins_amount * pool.scaling_factor
+        coins_amount * self.scaling_factor
     } else {
         // Shares price = total_coins / total existing shares.
         // New number of shares = new_amount / shares_price = new_amount * existing_shares / total_amount.
         // We rearrange the calc and do multiplication first to avoid rounding errors.
-        multiply_then_divide(pool, coins_amount, pool.total_shares, total_coins)
+        multiply_then_divide(self, coins_amount, self.total_shares, total_coins)
     }
 }
 
@@ -783,11 +783,11 @@ Return the number of new shares coins_amount can buy in pool< ## Function `shares_to_amount` -Return the number of coins shares are worth in pool. +Return the number of coins shares are worth in self. shares needs to big enough to avoid rounding number. -
public fun shares_to_amount(pool: &pool_u64::Pool, shares: u64): u64
+
public fun shares_to_amount(self: &pool_u64::Pool, shares: u64): u64
 
@@ -796,8 +796,8 @@ Return the number of coins shares are worth in pool. Implementation -
public fun shares_to_amount(pool: &Pool, shares: u64): u64 {
-    shares_to_amount_with_total_coins(pool, shares, pool.total_coins)
+
public fun shares_to_amount(self: &Pool, shares: u64): u64 {
+    shares_to_amount_with_total_coins(self, shares, self.total_coins)
 }
 
@@ -809,11 +809,11 @@ Return the number of coins shares are worth in pool. ## Function `shares_to_amount_with_total_coins` -Return the number of coins shares are worth in pool with a custom total coins number. +Return the number of coins shares are worth in self with a custom total coins number. shares needs to big enough to avoid rounding number. -
public fun shares_to_amount_with_total_coins(pool: &pool_u64::Pool, shares: u64, total_coins: u64): u64
+
public fun shares_to_amount_with_total_coins(self: &pool_u64::Pool, shares: u64, total_coins: u64): u64
 
@@ -822,15 +822,15 @@ Return the number of coins shares are worth in pool wi Implementation -
public fun shares_to_amount_with_total_coins(pool: &Pool, shares: u64, total_coins: u64): u64 {
+
public fun shares_to_amount_with_total_coins(self: &Pool, shares: u64, total_coins: u64): u64 {
     // No shares or coins yet so shares are worthless.
-    if (pool.total_coins == 0 || pool.total_shares == 0) {
+    if (self.total_coins == 0 || self.total_shares == 0) {
         0
     } else {
         // Shares price = total_coins / total existing shares.
         // Shares worth = shares * shares price = shares * total_coins / total existing shares.
         // We rearrange the calc and do multiplication first to avoid rounding errors.
-        multiply_then_divide(pool, shares, total_coins, pool.total_shares)
+        multiply_then_divide(self, shares, total_coins, self.total_shares)
     }
 }
 
@@ -845,7 +845,7 @@ Return the number of coins shares are worth in pool wi -
public fun multiply_then_divide(_pool: &pool_u64::Pool, x: u64, y: u64, z: u64): u64
+
public fun multiply_then_divide(self: &pool_u64::Pool, x: u64, y: u64, z: u64): u64
 
@@ -854,7 +854,7 @@ Return the number of coins shares are worth in pool wi Implementation -
public fun multiply_then_divide(_pool: &Pool, x: u64, y: u64, z: u64): u64 {
+
public fun multiply_then_divide(self: &Pool, x: u64, y: u64, z: u64): u64 {
     let result = (to_u128(x) * to_u128(y)) / to_u128(z);
     (result as u64)
 }
@@ -974,14 +974,14 @@ Return the number of coins shares are worth in pool wi
 ### Function `contains`
 
 
-
public fun contains(pool: &pool_u64::Pool, shareholder: address): bool
+
public fun contains(self: &pool_u64::Pool, shareholder: address): bool
 
aborts_if false;
-ensures result == spec_contains(pool, shareholder);
+ensures result == spec_contains(self, shareholder);
 
@@ -1007,14 +1007,14 @@ Return the number of coins shares are worth in pool wi ### Function `shares` -
public fun shares(pool: &pool_u64::Pool, shareholder: address): u64
+
public fun shares(self: &pool_u64::Pool, shareholder: address): u64
 
aborts_if false;
-ensures result == spec_shares(pool, shareholder);
+ensures result == spec_shares(self, shareholder);
 
@@ -1024,16 +1024,16 @@ Return the number of coins shares are worth in pool wi ### Function `balance` -
public fun balance(pool: &pool_u64::Pool, shareholder: address): u64
+
public fun balance(self: &pool_u64::Pool, shareholder: address): u64
 
-
let shares = spec_shares(pool, shareholder);
-let total_coins = pool.total_coins;
-aborts_if pool.total_coins > 0 && pool.total_shares > 0 && (shares * total_coins) / pool.total_shares > MAX_U64;
-ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins);
+
let shares = spec_shares(self, shareholder);
+let total_coins = self.total_coins;
+aborts_if self.total_coins > 0 && self.total_shares > 0 && (shares * total_coins) / self.total_shares > MAX_U64;
+ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins);
 
@@ -1043,19 +1043,19 @@ Return the number of coins shares are worth in pool wi ### Function `buy_in` -
public fun buy_in(pool: &mut pool_u64::Pool, shareholder: address, coins_amount: u64): u64
+
public fun buy_in(self: &mut pool_u64::Pool, shareholder: address, coins_amount: u64): u64
 
-
let new_shares = spec_amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins);
-aborts_if pool.total_coins + coins_amount > MAX_U64;
-aborts_if pool.total_shares + new_shares > MAX_U64;
+
let new_shares = spec_amount_to_shares_with_total_coins(self, coins_amount, self.total_coins);
+aborts_if self.total_coins + coins_amount > MAX_U64;
+aborts_if self.total_shares + new_shares > MAX_U64;
 include coins_amount > 0 ==> AddSharesAbortsIf { new_shares: new_shares };
 include coins_amount > 0 ==> AddSharesEnsures { new_shares: new_shares };
-ensures pool.total_coins == old(pool.total_coins) + coins_amount;
-ensures pool.total_shares == old(pool.total_shares) + new_shares;
+ensures self.total_coins == old(self.total_coins) + coins_amount;
+ensures self.total_shares == old(self.total_shares) + new_shares;
 ensures result == new_shares;
 
@@ -1066,7 +1066,7 @@ Return the number of coins shares are worth in pool wi ### Function `add_shares` -
fun add_shares(pool: &mut pool_u64::Pool, shareholder: address, new_shares: u64): u64
+
fun add_shares(self: &mut pool_u64::Pool, shareholder: address, new_shares: u64): u64
 
@@ -1074,8 +1074,8 @@ Return the number of coins shares are worth in pool wi
include AddSharesAbortsIf;
 include AddSharesEnsures;
-let key_exists = simple_map::spec_contains_key(pool.shares, shareholder);
-ensures result == if (key_exists) { simple_map::spec_get(pool.shares, shareholder) }
+let key_exists = simple_map::spec_contains_key(self.shares, shareholder);
+ensures result == if (key_exists) { simple_map::spec_get(self.shares, shareholder) }
 else { new_shares };
 
@@ -1086,13 +1086,13 @@ Return the number of coins shares are worth in pool wi
schema AddSharesAbortsIf {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     new_shares: u64;
-    let key_exists = simple_map::spec_contains_key(pool.shares, shareholder);
-    let current_shares = simple_map::spec_get(pool.shares, shareholder);
+    let key_exists = simple_map::spec_contains_key(self.shares, shareholder);
+    let current_shares = simple_map::spec_get(self.shares, shareholder);
     aborts_if key_exists && current_shares + new_shares > MAX_U64;
-    aborts_if !key_exists && new_shares > 0 && len(pool.shareholders) >= pool.shareholders_limit;
+    aborts_if !key_exists && new_shares > 0 && len(self.shareholders) >= self.shareholders_limit;
 }
 
@@ -1103,17 +1103,17 @@ Return the number of coins shares are worth in pool wi
schema AddSharesEnsures {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     new_shares: u64;
-    let key_exists = simple_map::spec_contains_key(pool.shares, shareholder);
-    let current_shares = simple_map::spec_get(pool.shares, shareholder);
+    let key_exists = simple_map::spec_contains_key(self.shares, shareholder);
+    let current_shares = simple_map::spec_get(self.shares, shareholder);
     ensures key_exists ==>
-        pool.shares == simple_map::spec_set(old(pool.shares), shareholder, current_shares + new_shares);
+        self.shares == simple_map::spec_set(old(self.shares), shareholder, current_shares + new_shares);
     ensures (!key_exists && new_shares > 0) ==>
-        pool.shares == simple_map::spec_set(old(pool.shares), shareholder, new_shares);
+        self.shares == simple_map::spec_set(old(self.shares), shareholder, new_shares);
     ensures (!key_exists && new_shares > 0) ==>
-        vector::eq_push_back(pool.shareholders, old(pool.shareholders), shareholder);
+        vector::eq_push_back(self.shareholders, old(self.shareholders), shareholder);
 }
 
@@ -1140,20 +1140,22 @@ Return the number of coins shares are worth in pool wi ### Function `redeem_shares` -
public fun redeem_shares(pool: &mut pool_u64::Pool, shareholder: address, shares_to_redeem: u64): u64
+
public fun redeem_shares(self: &mut pool_u64::Pool, shareholder: address, shares_to_redeem: u64): u64
 
-
let redeemed_coins = spec_shares_to_amount_with_total_coins(pool, shares_to_redeem, pool.total_coins);
-aborts_if !spec_contains(pool, shareholder);
-aborts_if spec_shares(pool, shareholder) < shares_to_redeem;
-aborts_if pool.total_coins < redeemed_coins;
-aborts_if pool.total_shares < shares_to_redeem;
-ensures pool.total_coins == old(pool.total_coins) - redeemed_coins;
-ensures pool.total_shares == old(pool.total_shares) - shares_to_redeem;
-include shares_to_redeem > 0 ==> DeductSharesEnsures { num_shares: shares_to_redeem };
+
let redeemed_coins = spec_shares_to_amount_with_total_coins(self, shares_to_redeem, self.total_coins);
+aborts_if !spec_contains(self, shareholder);
+aborts_if spec_shares(self, shareholder) < shares_to_redeem;
+aborts_if self.total_coins < redeemed_coins;
+aborts_if self.total_shares < shares_to_redeem;
+ensures self.total_coins == old(self.total_coins) - redeemed_coins;
+ensures self.total_shares == old(self.total_shares) - shares_to_redeem;
+include shares_to_redeem > 0 ==> DeductSharesEnsures {
+    num_shares: shares_to_redeem
+};
 ensures result == redeemed_coins;
 
@@ -1164,15 +1166,15 @@ Return the number of coins shares are worth in pool wi ### Function `transfer_shares` -
public fun transfer_shares(pool: &mut pool_u64::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64)
+
public fun transfer_shares(self: &mut pool_u64::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64)
 
pragma aborts_if_is_partial;
-aborts_if !spec_contains(pool, shareholder_1);
-aborts_if spec_shares(pool, shareholder_1) < shares_to_transfer;
+aborts_if !spec_contains(self, shareholder_1);
+aborts_if spec_shares(self, shareholder_1) < shares_to_transfer;
 
@@ -1182,17 +1184,17 @@ Return the number of coins shares are worth in pool wi ### Function `deduct_shares` -
fun deduct_shares(pool: &mut pool_u64::Pool, shareholder: address, num_shares: u64): u64
+
fun deduct_shares(self: &mut pool_u64::Pool, shareholder: address, num_shares: u64): u64
 
-
aborts_if !spec_contains(pool, shareholder);
-aborts_if spec_shares(pool, shareholder) < num_shares;
+
aborts_if !spec_contains(self, shareholder);
+aborts_if spec_shares(self, shareholder) < num_shares;
 include DeductSharesEnsures;
-let remaining_shares = simple_map::spec_get(pool.shares, shareholder) - num_shares;
-ensures remaining_shares > 0 ==> result == simple_map::spec_get(pool.shares, shareholder);
+let remaining_shares = simple_map::spec_get(self.shares, shareholder) - num_shares;
+ensures remaining_shares > 0 ==> result == simple_map::spec_get(self.shares, shareholder);
 ensures remaining_shares == 0 ==> result == 0;
 
@@ -1203,13 +1205,13 @@ Return the number of coins shares are worth in pool wi
schema DeductSharesEnsures {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     num_shares: u64;
-    let remaining_shares = simple_map::spec_get(pool.shares, shareholder) - num_shares;
-    ensures remaining_shares > 0 ==> simple_map::spec_get(pool.shares, shareholder) == remaining_shares;
-    ensures remaining_shares == 0 ==> !simple_map::spec_contains_key(pool.shares, shareholder);
-    ensures remaining_shares == 0 ==> !vector::spec_contains(pool.shareholders, shareholder);
+    let remaining_shares = simple_map::spec_get(self.shares, shareholder) - num_shares;
+    ensures remaining_shares > 0 ==> simple_map::spec_get(self.shares, shareholder) == remaining_shares;
+    ensures remaining_shares == 0 ==> !simple_map::spec_contains_key(self.shares, shareholder);
+    ensures remaining_shares == 0 ==> !vector::spec_contains(self.shareholders, shareholder);
 }
 
@@ -1220,18 +1222,18 @@ Return the number of coins shares are worth in pool wi ### Function `amount_to_shares_with_total_coins` -
public fun amount_to_shares_with_total_coins(pool: &pool_u64::Pool, coins_amount: u64, total_coins: u64): u64
+
public fun amount_to_shares_with_total_coins(self: &pool_u64::Pool, coins_amount: u64, total_coins: u64): u64
 
-
aborts_if pool.total_coins > 0 && pool.total_shares > 0
-    && (coins_amount * pool.total_shares) / total_coins > MAX_U64;
-aborts_if (pool.total_coins == 0 || pool.total_shares == 0)
-    && coins_amount * pool.scaling_factor > MAX_U64;
-aborts_if pool.total_coins > 0 && pool.total_shares > 0 && total_coins == 0;
-ensures result == spec_amount_to_shares_with_total_coins(pool, coins_amount, total_coins);
+
aborts_if self.total_coins > 0 && self.total_shares > 0
+    && (coins_amount * self.total_shares) / total_coins > MAX_U64;
+aborts_if (self.total_coins == 0 || self.total_shares == 0)
+    && coins_amount * self.scaling_factor > MAX_U64;
+aborts_if self.total_coins > 0 && self.total_shares > 0 && total_coins == 0;
+ensures result == spec_amount_to_shares_with_total_coins(self, coins_amount, total_coins);
 
@@ -1241,15 +1243,15 @@ Return the number of coins shares are worth in pool wi ### Function `shares_to_amount_with_total_coins` -
public fun shares_to_amount_with_total_coins(pool: &pool_u64::Pool, shares: u64, total_coins: u64): u64
+
public fun shares_to_amount_with_total_coins(self: &pool_u64::Pool, shares: u64, total_coins: u64): u64
 
-
aborts_if pool.total_coins > 0 && pool.total_shares > 0
-    && (shares * total_coins) / pool.total_shares > MAX_U64;
-ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins);
+
aborts_if self.total_coins > 0 && self.total_shares > 0
+    && (shares * total_coins) / self.total_shares > MAX_U64;
+ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins);
 
@@ -1275,7 +1277,7 @@ Return the number of coins shares are worth in pool wi ### Function `multiply_then_divide` -
public fun multiply_then_divide(_pool: &pool_u64::Pool, x: u64, y: u64, z: u64): u64
+
public fun multiply_then_divide(self: &pool_u64::Pool, x: u64, y: u64, z: u64): u64
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/pool_u64_unbound.md b/aptos-move/framework/aptos-stdlib/doc/pool_u64_unbound.md index 281fb751e7dfd..ae7f13852e8fc 100644 --- a/aptos-move/framework/aptos-stdlib/doc/pool_u64_unbound.md +++ b/aptos-move/framework/aptos-stdlib/doc/pool_u64_unbound.md @@ -296,7 +296,7 @@ Create a new pool with custom scaling_factor. Destroy an empty pool. This will fail if the pool has any balance of coins. -
public fun destroy_empty(pool: pool_u64_unbound::Pool)
+
public fun destroy_empty(self: pool_u64_unbound::Pool)
 
@@ -305,14 +305,14 @@ Destroy an empty pool. This will fail if the pool has any balance of coins. Implementation -
public fun destroy_empty(pool: Pool) {
-    assert!(pool.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY));
+
public fun destroy_empty(self: Pool) {
+    assert!(self.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY));
     let Pool {
         total_coins: _,
         total_shares: _,
         shares,
         scaling_factor: _,
-    } = pool;
+    } = self;
     table::destroy_empty<address, u128>(shares);
 }
 
@@ -325,10 +325,10 @@ Destroy an empty pool. This will fail if the pool has any balance of coins. ## Function `total_coins` -Return pool's total balance of coins. +Return self's total balance of coins. -
public fun total_coins(pool: &pool_u64_unbound::Pool): u64
+
public fun total_coins(self: &pool_u64_unbound::Pool): u64
 
@@ -337,8 +337,8 @@ Return pool's total balance of coins. Implementation -
public fun total_coins(pool: &Pool): u64 {
-    pool.total_coins
+
public fun total_coins(self: &Pool): u64 {
+    self.total_coins
 }
 
@@ -350,10 +350,10 @@ Return pool's total balance of coins. ## Function `total_shares` -Return the total number of shares across all shareholders in pool. +Return the total number of shares across all shareholders in self. -
public fun total_shares(pool: &pool_u64_unbound::Pool): u128
+
public fun total_shares(self: &pool_u64_unbound::Pool): u128
 
@@ -362,8 +362,8 @@ Return the total number of shares across all shareholders in pool. Implementation -
public fun total_shares(pool: &Pool): u128 {
-    pool.total_shares
+
public fun total_shares(self: &Pool): u128 {
+    self.total_shares
 }
 
@@ -375,10 +375,10 @@ Return the total number of shares across all shareholders in pool. ## Function `contains` -Return true if shareholder is in pool. +Return true if shareholder is in self. -
public fun contains(pool: &pool_u64_unbound::Pool, shareholder: address): bool
+
public fun contains(self: &pool_u64_unbound::Pool, shareholder: address): bool
 
@@ -387,8 +387,8 @@ Return true if shareholder is in pool. Implementation -
public fun contains(pool: &Pool, shareholder: address): bool {
-    table::contains(&pool.shares, shareholder)
+
public fun contains(self: &Pool, shareholder: address): bool {
+    table::contains(&self.shares, shareholder)
 }
 
@@ -400,10 +400,10 @@ Return true if shareholder is in pool. ## Function `shares` -Return the number of shares of stakeholder in pool. +Return the number of shares of stakeholder in self. -
public fun shares(pool: &pool_u64_unbound::Pool, shareholder: address): u128
+
public fun shares(self: &pool_u64_unbound::Pool, shareholder: address): u128
 
@@ -412,9 +412,9 @@ Return the number of shares of stakeholder in pool. Implementation -
public fun shares(pool: &Pool, shareholder: address): u128 {
-    if (contains(pool, shareholder)) {
-        *table::borrow(&pool.shares, shareholder)
+
public fun shares(self: &Pool, shareholder: address): u128 {
+    if (contains(self, shareholder)) {
+        *table::borrow(&self.shares, shareholder)
     } else {
         0
     }
@@ -429,10 +429,10 @@ Return the number of shares of stakeholder in pool.
 
 ## Function `balance`
 
-Return the balance in coins of shareholder in pool.
+Return the balance in coins of shareholder in self.
 
 
-
public fun balance(pool: &pool_u64_unbound::Pool, shareholder: address): u64
+
public fun balance(self: &pool_u64_unbound::Pool, shareholder: address): u64
 
@@ -441,9 +441,9 @@ Return the balance in coins of shareholder in pool. Implementation -
public fun balance(pool: &Pool, shareholder: address): u64 {
-    let num_shares = shares(pool, shareholder);
-    shares_to_amount(pool, num_shares)
+
public fun balance(self: &Pool, shareholder: address): u64 {
+    let num_shares = shares(self, shareholder);
+    shares_to_amount(self, num_shares)
 }
 
@@ -455,10 +455,10 @@ Return the balance in coins of shareholder in pool. ## Function `shareholders_count` -Return the number of shareholders in pool. +Return the number of shareholders in self. -
public fun shareholders_count(pool: &pool_u64_unbound::Pool): u64
+
public fun shareholders_count(self: &pool_u64_unbound::Pool): u64
 
@@ -467,8 +467,8 @@ Return the number of shareholders in pool. Implementation -
public fun shareholders_count(pool: &Pool): u64 {
-    table::length(&pool.shares)
+
public fun shareholders_count(self: &Pool): u64 {
+    table::length(&self.shares)
 }
 
@@ -480,10 +480,10 @@ Return the number of shareholders in pool. ## Function `update_total_coins` -Update pool's total balance of coins. +Update self's total balance of coins. -
public fun update_total_coins(pool: &mut pool_u64_unbound::Pool, new_total_coins: u64)
+
public fun update_total_coins(self: &mut pool_u64_unbound::Pool, new_total_coins: u64)
 
@@ -492,8 +492,8 @@ Update pool's total balance of coins. Implementation -
public fun update_total_coins(pool: &mut Pool, new_total_coins: u64) {
-    pool.total_coins = new_total_coins;
+
public fun update_total_coins(self: &mut Pool, new_total_coins: u64) {
+    self.total_coins = new_total_coins;
 }
 
@@ -508,7 +508,7 @@ Update pool's total balance of coins. Allow an existing or new shareholder to add their coins to the pool in exchange for new shares. -
public fun buy_in(pool: &mut pool_u64_unbound::Pool, shareholder: address, coins_amount: u64): u128
+
public fun buy_in(self: &mut pool_u64_unbound::Pool, shareholder: address, coins_amount: u64): u128
 
@@ -517,16 +517,16 @@ Allow an existing or new shareholder to add their coins to the pool in exchange Implementation -
public fun buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u128 {
+
public fun buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u128 {
     if (coins_amount == 0) return 0;
 
-    let new_shares = amount_to_shares(pool, coins_amount);
-    assert!(MAX_U64 - pool.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
-    assert!(MAX_U128 - pool.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_SHARES_OVERFLOW));
+    let new_shares = amount_to_shares(self, coins_amount);
+    assert!(MAX_U64 - self.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW));
+    assert!(MAX_U128 - self.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_SHARES_OVERFLOW));
 
-    pool.total_coins = pool.total_coins + coins_amount;
-    pool.total_shares = pool.total_shares + new_shares;
-    add_shares(pool, shareholder, new_shares);
+    self.total_coins = self.total_coins + coins_amount;
+    self.total_shares = self.total_shares + new_shares;
+    add_shares(self, shareholder, new_shares);
     new_shares
 }
 
@@ -539,11 +539,11 @@ Allow an existing or new shareholder to add their coins to the pool in exchange ## Function `add_shares` -Add the number of shares directly for shareholder in pool. +Add the number of shares directly for shareholder in self. This would dilute other shareholders if the pool's balance of coins didn't change. -
fun add_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, new_shares: u128): u128
+
fun add_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, new_shares: u128): u128
 
@@ -552,16 +552,16 @@ This would dilute other shareholders if the pool's balance of coins didn't chang Implementation -
fun add_shares(pool: &mut Pool, shareholder: address, new_shares: u128): u128 {
-    if (contains(pool, shareholder)) {
-        let existing_shares = table::borrow_mut(&mut pool.shares, shareholder);
+
fun add_shares(self: &mut Pool, shareholder: address, new_shares: u128): u128 {
+    if (contains(self, shareholder)) {
+        let existing_shares = table::borrow_mut(&mut self.shares, shareholder);
         let current_shares = *existing_shares;
         assert!(MAX_U128 - current_shares >= new_shares, error::invalid_argument(ESHAREHOLDER_SHARES_OVERFLOW));
 
         *existing_shares = current_shares + new_shares;
         *existing_shares
     } else if (new_shares > 0) {
-        table::add(&mut pool.shares, shareholder, new_shares);
+        table::add(&mut self.shares, shareholder, new_shares);
         new_shares
     } else {
         new_shares
@@ -577,10 +577,10 @@ This would dilute other shareholders if the pool's balance of coins didn't chang
 
 ## Function `redeem_shares`
 
-Allow shareholder to redeem their shares in pool for coins.
+Allow shareholder to redeem their shares in self for coins.
 
 
-
public fun redeem_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, shares_to_redeem: u128): u64
+
public fun redeem_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, shares_to_redeem: u128): u64
 
@@ -589,16 +589,16 @@ Allow shareholder to redeem their shares in pool for c Implementation -
public fun redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 {
-    assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES));
+
public fun redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 {
+    assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES));
 
     if (shares_to_redeem == 0) return 0;
 
-    let redeemed_coins = shares_to_amount(pool, shares_to_redeem);
-    pool.total_coins = pool.total_coins - redeemed_coins;
-    pool.total_shares = pool.total_shares - shares_to_redeem;
-    deduct_shares(pool, shareholder, shares_to_redeem);
+    let redeemed_coins = shares_to_amount(self, shares_to_redeem);
+    self.total_coins = self.total_coins - redeemed_coins;
+    self.total_shares = self.total_shares - shares_to_redeem;
+    deduct_shares(self, shareholder, shares_to_redeem);
 
     redeemed_coins
 }
@@ -615,7 +615,7 @@ Allow shareholder to redeem their shares in pool for c
 Transfer shares from shareholder_1 to shareholder_2.
 
 
-
public fun transfer_shares(pool: &mut pool_u64_unbound::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128)
+
public fun transfer_shares(self: &mut pool_u64_unbound::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128)
 
@@ -625,17 +625,17 @@ Transfer shares from shareholder_1 to shareholder_2.
public fun transfer_shares(
-    pool: &mut Pool,
+    self: &mut Pool,
     shareholder_1: address,
     shareholder_2: address,
     shares_to_transfer: u128,
 ) {
-    assert!(contains(pool, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES));
+    assert!(contains(self, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES));
     if (shares_to_transfer == 0) return;
 
-    deduct_shares(pool, shareholder_1, shares_to_transfer);
-    add_shares(pool, shareholder_2, shares_to_transfer);
+    deduct_shares(self, shareholder_1, shares_to_transfer);
+    add_shares(self, shareholder_2, shares_to_transfer);
 }
 
@@ -647,10 +647,10 @@ Transfer shares from shareholder_1 to shareholder_2. ## Function `deduct_shares` -Directly deduct shareholder's number of shares in pool and return the number of remaining shares. +Directly deduct shareholder's number of shares in self and return the number of remaining shares. -
fun deduct_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, num_shares: u128): u128
+
fun deduct_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, num_shares: u128): u128
 
@@ -659,17 +659,17 @@ Directly deduct shareholder's number of shares in pool Implementation -
fun deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u128): u128 {
-    assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
-    assert!(shares(pool, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES));
+
fun deduct_shares(self: &mut Pool, shareholder: address, num_shares: u128): u128 {
+    assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND));
+    assert!(shares(self, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES));
 
-    let existing_shares = table::borrow_mut(&mut pool.shares, shareholder);
+    let existing_shares = table::borrow_mut(&mut self.shares, shareholder);
     *existing_shares = *existing_shares - num_shares;
 
     // Remove the shareholder completely if they have no shares left.
     let remaining_shares = *existing_shares;
     if (remaining_shares == 0) {
-        table::remove(&mut pool.shares, shareholder);
+        table::remove(&mut self.shares, shareholder);
     };
 
     remaining_shares
@@ -684,11 +684,11 @@ Directly deduct shareholder's number of shares in pool
 
 ## Function `amount_to_shares`
 
-Return the number of new shares coins_amount can buy in pool.
+Return the number of new shares coins_amount can buy in self.
 amount needs to big enough to avoid rounding number.
 
 
-
public fun amount_to_shares(pool: &pool_u64_unbound::Pool, coins_amount: u64): u128
+
public fun amount_to_shares(self: &pool_u64_unbound::Pool, coins_amount: u64): u128
 
@@ -697,8 +697,8 @@ Return the number of new shares coins_amount can buy in pool< Implementation -
public fun amount_to_shares(pool: &Pool, coins_amount: u64): u128 {
-    amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins)
+
public fun amount_to_shares(self: &Pool, coins_amount: u64): u128 {
+    amount_to_shares_with_total_coins(self, coins_amount, self.total_coins)
 }
 
@@ -710,11 +710,11 @@ Return the number of new shares coins_amount can buy in pool< ## Function `amount_to_shares_with_total_coins` -Return the number of new shares coins_amount can buy in pool with a custom total coins number. +Return the number of new shares coins_amount can buy in self with a custom total coins number. amount needs to big enough to avoid rounding number. -
public fun amount_to_shares_with_total_coins(pool: &pool_u64_unbound::Pool, coins_amount: u64, total_coins: u64): u128
+
public fun amount_to_shares_with_total_coins(self: &pool_u64_unbound::Pool, coins_amount: u64, total_coins: u64): u128
 
@@ -723,17 +723,17 @@ Return the number of new shares coins_amount can buy in pool< Implementation -
public fun amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u128 {
+
public fun amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u128 {
     // No shares yet so amount is worth the same number of shares.
-    if (pool.total_coins == 0 || pool.total_shares == 0) {
+    if (self.total_coins == 0 || self.total_shares == 0) {
         // Multiply by scaling factor to minimize rounding errors during internal calculations for buy ins/redeems.
         // This can overflow but scaling factor is expected to be chosen carefully so this would not overflow.
-        to_u128(coins_amount) * to_u128(pool.scaling_factor)
+        to_u128(coins_amount) * to_u128(self.scaling_factor)
     } else {
         // Shares price = total_coins / total existing shares.
         // New number of shares = new_amount / shares_price = new_amount * existing_shares / total_amount.
         // We rearrange the calc and do multiplication first to avoid rounding errors.
-        multiply_then_divide(pool, to_u128(coins_amount), pool.total_shares, to_u128(total_coins))
+        multiply_then_divide(self, to_u128(coins_amount), self.total_shares, to_u128(total_coins))
     }
 }
 
@@ -746,11 +746,11 @@ Return the number of new shares coins_amount can buy in pool< ## Function `shares_to_amount` -Return the number of coins shares are worth in pool. +Return the number of coins shares are worth in self. shares needs to big enough to avoid rounding number. -
public fun shares_to_amount(pool: &pool_u64_unbound::Pool, shares: u128): u64
+
public fun shares_to_amount(self: &pool_u64_unbound::Pool, shares: u128): u64
 
@@ -759,8 +759,8 @@ Return the number of coins shares are worth in pool. Implementation -
public fun shares_to_amount(pool: &Pool, shares: u128): u64 {
-    shares_to_amount_with_total_coins(pool, shares, pool.total_coins)
+
public fun shares_to_amount(self: &Pool, shares: u128): u64 {
+    shares_to_amount_with_total_coins(self, shares, self.total_coins)
 }
 
@@ -772,11 +772,11 @@ Return the number of coins shares are worth in pool. ## Function `shares_to_amount_with_total_coins` -Return the number of coins shares are worth in pool with a custom total coins number. +Return the number of coins shares are worth in self with a custom total coins number. shares needs to big enough to avoid rounding number. -
public fun shares_to_amount_with_total_coins(pool: &pool_u64_unbound::Pool, shares: u128, total_coins: u64): u64
+
public fun shares_to_amount_with_total_coins(self: &pool_u64_unbound::Pool, shares: u128, total_coins: u64): u64
 
@@ -785,15 +785,15 @@ Return the number of coins shares are worth in pool wi Implementation -
public fun shares_to_amount_with_total_coins(pool: &Pool, shares: u128, total_coins: u64): u64 {
+
public fun shares_to_amount_with_total_coins(self: &Pool, shares: u128, total_coins: u64): u64 {
     // No shares or coins yet so shares are worthless.
-    if (pool.total_coins == 0 || pool.total_shares == 0) {
+    if (self.total_coins == 0 || self.total_shares == 0) {
         0
     } else {
         // Shares price = total_coins / total existing shares.
         // Shares worth = shares * shares price = shares * total_coins / total existing shares.
         // We rearrange the calc and do multiplication first to avoid rounding errors.
-        (multiply_then_divide(pool, shares, to_u128(total_coins), pool.total_shares) as u64)
+        (multiply_then_divide(self, shares, to_u128(total_coins), self.total_shares) as u64)
     }
 }
 
@@ -809,7 +809,7 @@ Return the number of coins shares are worth in pool wi Return the number of coins shares are worth in pool with custom total coins and shares numbers. -
public fun shares_to_amount_with_total_stats(pool: &pool_u64_unbound::Pool, shares: u128, total_coins: u64, total_shares: u128): u64
+
public fun shares_to_amount_with_total_stats(self: &pool_u64_unbound::Pool, shares: u128, total_coins: u64, total_shares: u128): u64
 
@@ -819,15 +819,15 @@ Return the number of coins shares are worth in pool wi
public fun shares_to_amount_with_total_stats(
-    pool: &Pool,
+    self: &Pool,
     shares: u128,
     total_coins: u64,
     total_shares: u128,
 ): u64 {
-    if (pool.total_coins == 0 || total_shares == 0) {
+    if (self.total_coins == 0 || total_shares == 0) {
         0
     } else {
-        (multiply_then_divide(pool, shares, to_u128(total_coins), total_shares) as u64)
+        (multiply_then_divide(self, shares, to_u128(total_coins), total_shares) as u64)
     }
 }
 
@@ -842,7 +842,7 @@ Return the number of coins shares are worth in pool wi -
public fun multiply_then_divide(_pool: &pool_u64_unbound::Pool, x: u128, y: u128, z: u128): u128
+
public fun multiply_then_divide(self: &pool_u64_unbound::Pool, x: u128, y: u128, z: u128): u128
 
@@ -851,7 +851,7 @@ Return the number of coins shares are worth in pool wi Implementation -
public fun multiply_then_divide(_pool: &Pool, x: u128, y: u128, z: u128): u128 {
+
public fun multiply_then_divide(self: &Pool, x: u128, y: u128, z: u128): u128 {
     let result = (to_u256(x) * to_u256(y)) / to_u256(z);
     (result as u128)
 }
@@ -975,14 +975,14 @@ Return the number of coins shares are worth in pool wi
 ### Function `contains`
 
 
-
public fun contains(pool: &pool_u64_unbound::Pool, shareholder: address): bool
+
public fun contains(self: &pool_u64_unbound::Pool, shareholder: address): bool
 
aborts_if false;
-ensures result == spec_contains(pool, shareholder);
+ensures result == spec_contains(self, shareholder);
 
@@ -1008,14 +1008,14 @@ Return the number of coins shares are worth in pool wi ### Function `shares` -
public fun shares(pool: &pool_u64_unbound::Pool, shareholder: address): u128
+
public fun shares(self: &pool_u64_unbound::Pool, shareholder: address): u128
 
aborts_if false;
-ensures result == spec_shares(pool, shareholder);
+ensures result == spec_shares(self, shareholder);
 
@@ -1025,16 +1025,16 @@ Return the number of coins shares are worth in pool wi ### Function `balance` -
public fun balance(pool: &pool_u64_unbound::Pool, shareholder: address): u64
+
public fun balance(self: &pool_u64_unbound::Pool, shareholder: address): u64
 
-
let shares = spec_shares(pool, shareholder);
-let total_coins = pool.total_coins;
-aborts_if pool.total_coins > 0 && pool.total_shares > 0 && (shares * total_coins) / pool.total_shares > MAX_U64;
-ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins);
+
let shares = spec_shares(self, shareholder);
+let total_coins = self.total_coins;
+aborts_if self.total_coins > 0 && self.total_shares > 0 && (shares * total_coins) / self.total_shares > MAX_U64;
+ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins);
 
@@ -1044,19 +1044,19 @@ Return the number of coins shares are worth in pool wi ### Function `buy_in` -
public fun buy_in(pool: &mut pool_u64_unbound::Pool, shareholder: address, coins_amount: u64): u128
+
public fun buy_in(self: &mut pool_u64_unbound::Pool, shareholder: address, coins_amount: u64): u128
 
-
let new_shares = spec_amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins);
-aborts_if pool.total_coins + coins_amount > MAX_U64;
-aborts_if pool.total_shares + new_shares > MAX_U128;
+
let new_shares = spec_amount_to_shares_with_total_coins(self, coins_amount, self.total_coins);
+aborts_if self.total_coins + coins_amount > MAX_U64;
+aborts_if self.total_shares + new_shares > MAX_U128;
 include coins_amount > 0 ==> AddSharesAbortsIf { new_shares: new_shares };
 include coins_amount > 0 ==> AddSharesEnsures { new_shares: new_shares };
-ensures pool.total_coins == old(pool.total_coins) + coins_amount;
-ensures pool.total_shares == old(pool.total_shares) + new_shares;
+ensures self.total_coins == old(self.total_coins) + coins_amount;
+ensures self.total_shares == old(self.total_shares) + new_shares;
 ensures result == new_shares;
 
@@ -1067,7 +1067,7 @@ Return the number of coins shares are worth in pool wi ### Function `add_shares` -
fun add_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, new_shares: u128): u128
+
fun add_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, new_shares: u128): u128
 
@@ -1075,8 +1075,8 @@ Return the number of coins shares are worth in pool wi
include AddSharesAbortsIf;
 include AddSharesEnsures;
-let key_exists = table::spec_contains(pool.shares, shareholder);
-ensures result == if (key_exists) { table::spec_get(pool.shares, shareholder) }
+let key_exists = table::spec_contains(self.shares, shareholder);
+ensures result == if (key_exists) { table::spec_get(self.shares, shareholder) }
 else { new_shares };
 
@@ -1087,11 +1087,11 @@ Return the number of coins shares are worth in pool wi
schema AddSharesAbortsIf {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     new_shares: u64;
-    let key_exists = table::spec_contains(pool.shares, shareholder);
-    let current_shares = table::spec_get(pool.shares, shareholder);
+    let key_exists = table::spec_contains(self.shares, shareholder);
+    let current_shares = table::spec_get(self.shares, shareholder);
     aborts_if key_exists && current_shares + new_shares > MAX_U128;
 }
 
@@ -1103,15 +1103,15 @@ Return the number of coins shares are worth in pool wi
schema AddSharesEnsures {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     new_shares: u64;
-    let key_exists = table::spec_contains(pool.shares, shareholder);
-    let current_shares = table::spec_get(pool.shares, shareholder);
+    let key_exists = table::spec_contains(self.shares, shareholder);
+    let current_shares = table::spec_get(self.shares, shareholder);
     ensures key_exists ==>
-        pool.shares == table::spec_set(old(pool.shares), shareholder, current_shares + new_shares);
+        self.shares == table::spec_set(old(self.shares), shareholder, current_shares + new_shares);
     ensures (!key_exists && new_shares > 0) ==>
-        pool.shares == table::spec_set(old(pool.shares), shareholder, new_shares);
+        self.shares == table::spec_set(old(self.shares), shareholder, new_shares);
 }
 
@@ -1138,20 +1138,22 @@ Return the number of coins shares are worth in pool wi ### Function `redeem_shares` -
public fun redeem_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, shares_to_redeem: u128): u64
+
public fun redeem_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, shares_to_redeem: u128): u64
 
-
let redeemed_coins = spec_shares_to_amount_with_total_coins(pool, shares_to_redeem, pool.total_coins);
-aborts_if !spec_contains(pool, shareholder);
-aborts_if spec_shares(pool, shareholder) < shares_to_redeem;
-aborts_if pool.total_coins < redeemed_coins;
-aborts_if pool.total_shares < shares_to_redeem;
-ensures pool.total_coins == old(pool.total_coins) - redeemed_coins;
-ensures pool.total_shares == old(pool.total_shares) - shares_to_redeem;
-include shares_to_redeem > 0 ==> DeductSharesEnsures { num_shares: shares_to_redeem };
+
let redeemed_coins = spec_shares_to_amount_with_total_coins(self, shares_to_redeem, self.total_coins);
+aborts_if !spec_contains(self, shareholder);
+aborts_if spec_shares(self, shareholder) < shares_to_redeem;
+aborts_if self.total_coins < redeemed_coins;
+aborts_if self.total_shares < shares_to_redeem;
+ensures self.total_coins == old(self.total_coins) - redeemed_coins;
+ensures self.total_shares == old(self.total_shares) - shares_to_redeem;
+include shares_to_redeem > 0 ==> DeductSharesEnsures {
+    num_shares: shares_to_redeem
+};
 ensures result == redeemed_coins;
 
@@ -1162,27 +1164,28 @@ Return the number of coins shares are worth in pool wi ### Function `transfer_shares` -
public fun transfer_shares(pool: &mut pool_u64_unbound::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128)
+
public fun transfer_shares(self: &mut pool_u64_unbound::Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128)
 
-
aborts_if (shareholder_1 != shareholder_2) && shares_to_transfer > 0 && spec_contains(pool, shareholder_2) &&
-    (spec_shares(pool, shareholder_2) + shares_to_transfer > MAX_U128);
-aborts_if !spec_contains(pool, shareholder_1);
-aborts_if spec_shares(pool, shareholder_1) < shares_to_transfer;
-ensures shareholder_1 == shareholder_2 ==> spec_shares(old(pool), shareholder_1) == spec_shares(pool, shareholder_1);
-ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(pool), shareholder_1) == shares_to_transfer)) ==>
-    !spec_contains(pool, shareholder_1);
+
aborts_if (shareholder_1 != shareholder_2) && shares_to_transfer > 0 && spec_contains(self, shareholder_2) &&
+    (spec_shares(self, shareholder_2) + shares_to_transfer > MAX_U128);
+aborts_if !spec_contains(self, shareholder_1);
+aborts_if spec_shares(self, shareholder_1) < shares_to_transfer;
+ensures shareholder_1 == shareholder_2 ==> spec_shares(old(self), shareholder_1) == spec_shares(
+    self, shareholder_1);
+ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(self), shareholder_1) == shares_to_transfer)) ==>
+    !spec_contains(self, shareholder_1);
 ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0) ==>
-    (spec_contains(pool, shareholder_2));
-ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && !spec_contains(old(pool), shareholder_2)) ==>
-    (spec_contains(pool, shareholder_2) && spec_shares(pool, shareholder_2) == shares_to_transfer);
-ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && spec_contains(old(pool), shareholder_2)) ==>
-    (spec_contains(pool, shareholder_2) && spec_shares(pool, shareholder_2) == spec_shares(old(pool), shareholder_2) + shares_to_transfer);
-ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(pool), shareholder_1) > shares_to_transfer)) ==>
-    (spec_contains(pool, shareholder_1) && (spec_shares(pool, shareholder_1) == spec_shares(old(pool), shareholder_1) - shares_to_transfer));
+    (spec_contains(self, shareholder_2));
+ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && !spec_contains(old(self), shareholder_2)) ==>
+    (spec_contains(self, shareholder_2) && spec_shares(self, shareholder_2) == shares_to_transfer);
+ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && spec_contains(old(self), shareholder_2)) ==>
+    (spec_contains(self, shareholder_2) && spec_shares(self, shareholder_2) == spec_shares(old(self), shareholder_2) + shares_to_transfer);
+ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(self), shareholder_1) > shares_to_transfer)) ==>
+    (spec_contains(self, shareholder_1) && (spec_shares(self, shareholder_1) == spec_shares(old(self), shareholder_1) - shares_to_transfer));
 
@@ -1192,17 +1195,17 @@ Return the number of coins shares are worth in pool wi ### Function `deduct_shares` -
fun deduct_shares(pool: &mut pool_u64_unbound::Pool, shareholder: address, num_shares: u128): u128
+
fun deduct_shares(self: &mut pool_u64_unbound::Pool, shareholder: address, num_shares: u128): u128
 
-
aborts_if !spec_contains(pool, shareholder);
-aborts_if spec_shares(pool, shareholder) < num_shares;
+
aborts_if !spec_contains(self, shareholder);
+aborts_if spec_shares(self, shareholder) < num_shares;
 include DeductSharesEnsures;
-let remaining_shares = table::spec_get(pool.shares, shareholder) - num_shares;
-ensures remaining_shares > 0 ==> result == table::spec_get(pool.shares, shareholder);
+let remaining_shares = table::spec_get(self.shares, shareholder) - num_shares;
+ensures remaining_shares > 0 ==> result == table::spec_get(self.shares, shareholder);
 ensures remaining_shares == 0 ==> result == 0;
 
@@ -1213,12 +1216,12 @@ Return the number of coins shares are worth in pool wi
schema DeductSharesEnsures {
-    pool: Pool;
+    self: Pool;
     shareholder: address;
     num_shares: u64;
-    let remaining_shares = table::spec_get(pool.shares, shareholder) - num_shares;
-    ensures remaining_shares > 0 ==> table::spec_get(pool.shares, shareholder) == remaining_shares;
-    ensures remaining_shares == 0 ==> !table::spec_contains(pool.shares, shareholder);
+    let remaining_shares = table::spec_get(self.shares, shareholder) - num_shares;
+    ensures remaining_shares > 0 ==> table::spec_get(self.shares, shareholder) == remaining_shares;
+    ensures remaining_shares == 0 ==> !table::spec_contains(self.shares, shareholder);
 }
 
@@ -1229,18 +1232,18 @@ Return the number of coins shares are worth in pool wi ### Function `amount_to_shares_with_total_coins` -
public fun amount_to_shares_with_total_coins(pool: &pool_u64_unbound::Pool, coins_amount: u64, total_coins: u64): u128
+
public fun amount_to_shares_with_total_coins(self: &pool_u64_unbound::Pool, coins_amount: u64, total_coins: u64): u128
 
-
aborts_if pool.total_coins > 0 && pool.total_shares > 0
-    && (coins_amount * pool.total_shares) / total_coins > MAX_U128;
-aborts_if (pool.total_coins == 0 || pool.total_shares == 0)
-    && coins_amount * pool.scaling_factor > MAX_U128;
-aborts_if pool.total_coins > 0 && pool.total_shares > 0 && total_coins == 0;
-ensures result == spec_amount_to_shares_with_total_coins(pool, coins_amount, total_coins);
+
aborts_if self.total_coins > 0 && self.total_shares > 0
+    && (coins_amount * self.total_shares) / total_coins > MAX_U128;
+aborts_if (self.total_coins == 0 || self.total_shares == 0)
+    && coins_amount * self.scaling_factor > MAX_U128;
+aborts_if self.total_coins > 0 && self.total_shares > 0 && total_coins == 0;
+ensures result == spec_amount_to_shares_with_total_coins(self, coins_amount, total_coins);
 
@@ -1250,15 +1253,15 @@ Return the number of coins shares are worth in pool wi ### Function `shares_to_amount_with_total_coins` -
public fun shares_to_amount_with_total_coins(pool: &pool_u64_unbound::Pool, shares: u128, total_coins: u64): u64
+
public fun shares_to_amount_with_total_coins(self: &pool_u64_unbound::Pool, shares: u128, total_coins: u64): u64
 
-
aborts_if pool.total_coins > 0 && pool.total_shares > 0
-    && (shares * total_coins) / pool.total_shares > MAX_U64;
-ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins);
+
aborts_if self.total_coins > 0 && self.total_shares > 0
+    && (shares * total_coins) / self.total_shares > MAX_U64;
+ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins);
 
@@ -1284,7 +1287,7 @@ Return the number of coins shares are worth in pool wi ### Function `multiply_then_divide` -
public fun multiply_then_divide(_pool: &pool_u64_unbound::Pool, x: u128, y: u128, z: u128): u128
+
public fun multiply_then_divide(self: &pool_u64_unbound::Pool, x: u128, y: u128, z: u128): u128
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/simple_map.md b/aptos-move/framework/aptos-stdlib/doc/simple_map.md index ea5040273580b..3fc1a1a0390cb 100644 --- a/aptos-move/framework/aptos-stdlib/doc/simple_map.md +++ b/aptos-move/framework/aptos-stdlib/doc/simple_map.md @@ -149,7 +149,7 @@ Map key is not found -
public fun length<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>): u64
+
public fun length<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>): u64
 
@@ -158,8 +158,8 @@ Map key is not found Implementation -
public fun length<Key: store, Value: store>(map: &SimpleMap<Key, Value>): u64 {
-    vector::length(&map.data)
+
public fun length<Key: store, Value: store>(self: &SimpleMap<Key, Value>): u64 {
+    vector::length(&self.data)
 }
 
@@ -257,7 +257,7 @@ This function is deprecated, use new instead. -
public fun borrow<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value
+
public fun borrow<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value
 
@@ -267,13 +267,13 @@ This function is deprecated, use new instead.
public fun borrow<Key: store, Value: store>(
-    map: &SimpleMap<Key, Value>,
+    self: &SimpleMap<Key, Value>,
     key: &Key,
 ): &Value {
-    let maybe_idx = find(map, key);
+    let maybe_idx = find(self, key);
     assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
     let idx = option::extract(&mut maybe_idx);
-    &vector::borrow(&map.data, idx).value
+    &vector::borrow(&self.data, idx).value
 }
 
@@ -287,7 +287,7 @@ This function is deprecated, use new instead. -
public fun borrow_mut<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value
+
public fun borrow_mut<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value
 
@@ -297,13 +297,13 @@ This function is deprecated, use new instead.
public fun borrow_mut<Key: store, Value: store>(
-    map: &mut SimpleMap<Key, Value>,
+    self: &mut SimpleMap<Key, Value>,
     key: &Key,
 ): &mut Value {
-    let maybe_idx = find(map, key);
+    let maybe_idx = find(self, key);
     assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
     let idx = option::extract(&mut maybe_idx);
-    &mut vector::borrow_mut(&mut map.data, idx).value
+    &mut vector::borrow_mut(&mut self.data, idx).value
 }
 
@@ -317,7 +317,7 @@ This function is deprecated, use new instead. -
public fun contains_key<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): bool
+
public fun contains_key<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): bool
 
@@ -327,10 +327,10 @@ This function is deprecated, use new instead.
public fun contains_key<Key: store, Value: store>(
-    map: &SimpleMap<Key, Value>,
+    self: &SimpleMap<Key, Value>,
     key: &Key,
 ): bool {
-    let maybe_idx = find(map, key);
+    let maybe_idx = find(self, key);
     option::is_some(&maybe_idx)
 }
 
@@ -345,7 +345,7 @@ This function is deprecated, use new instead. -
public fun destroy_empty<Key: store, Value: store>(map: simple_map::SimpleMap<Key, Value>)
+
public fun destroy_empty<Key: store, Value: store>(self: simple_map::SimpleMap<Key, Value>)
 
@@ -354,8 +354,8 @@ This function is deprecated, use new instead. Implementation -
public fun destroy_empty<Key: store, Value: store>(map: SimpleMap<Key, Value>) {
-    let SimpleMap { data } = map;
+
public fun destroy_empty<Key: store, Value: store>(self: SimpleMap<Key, Value>) {
+    let SimpleMap { data } = self;
     vector::destroy_empty(data);
 }
 
@@ -371,7 +371,7 @@ This function is deprecated, use new instead. Add a key/value pair to the map. The key must not already exist. -
public fun add<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)
+
public fun add<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)
 
@@ -381,14 +381,14 @@ Add a key/value pair to the map. The key must not already exist.
public fun add<Key: store, Value: store>(
-    map: &mut SimpleMap<Key, Value>,
+    self: &mut SimpleMap<Key, Value>,
     key: Key,
     value: Value,
 ) {
-    let maybe_idx = find(map, &key);
+    let maybe_idx = find(self, &key);
     assert!(option::is_none(&maybe_idx), error::invalid_argument(EKEY_ALREADY_EXISTS));
 
-    vector::push_back(&mut map.data, Element { key, value });
+    vector::push_back(&mut self.data, Element { key, value });
 }
 
@@ -403,7 +403,7 @@ Add a key/value pair to the map. The key must not already exist. Add multiple key/value pairs to the map. The keys must not already exist. -
public fun add_all<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, keys: vector<Key>, values: vector<Value>)
+
public fun add_all<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, keys: vector<Key>, values: vector<Value>)
 
@@ -413,12 +413,12 @@ Add multiple key/value pairs to the map. The keys must not already exist.
public fun add_all<Key: store, Value: store>(
-    map: &mut SimpleMap<Key, Value>,
+    self: &mut SimpleMap<Key, Value>,
     keys: vector<Key>,
     values: vector<Value>,
 ) {
     vector::zip(keys, values, |key, value| {
-        add(map, key, value);
+        add(self, key, value);
     });
 }
 
@@ -434,7 +434,7 @@ Add multiple key/value pairs to the map. The keys must not already exist. Insert key/value pair or update an existing key to a new value -
public fun upsert<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value): (option::Option<Key>, option::Option<Value>)
+
public fun upsert<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value): (option::Option<Key>, option::Option<Value>)
 
@@ -444,11 +444,11 @@ Insert key/value pair or update an existing key to a new value
public fun upsert<Key: store, Value: store>(
-    map: &mut SimpleMap<Key, Value>,
+    self: &mut SimpleMap<Key, Value>,
     key: Key,
     value: Value
 ): (std::option::Option<Key>, std::option::Option<Value>) {
-    let data = &mut map.data;
+    let data = &mut self.data;
     let len = vector::length(data);
     let i = 0;
     while (i < len) {
@@ -461,7 +461,7 @@ Insert key/value pair or update an existing key to a new value
         };
         i = i + 1;
     };
-    vector::push_back(&mut map.data, Element { key, value });
+    vector::push_back(&mut self.data, Element { key, value });
     (std::option::none(), std::option::none())
 }
 
@@ -477,7 +477,7 @@ Insert key/value pair or update an existing key to a new value Return all keys in the map. This requires keys to be copyable. -
public fun keys<Key: copy, Value>(map: &simple_map::SimpleMap<Key, Value>): vector<Key>
+
public fun keys<Key: copy, Value>(self: &simple_map::SimpleMap<Key, Value>): vector<Key>
 
@@ -486,8 +486,8 @@ Return all keys in the map. This requires keys to be copyable. Implementation -
public fun keys<Key: copy, Value>(map: &SimpleMap<Key, Value>): vector<Key> {
-    vector::map_ref(&map.data, |e| {
+
public fun keys<Key: copy, Value>(self: &SimpleMap<Key, Value>): vector<Key> {
+    vector::map_ref(&self.data, |e| {
         let e: &Element<Key, Value> = e;
         e.key
     })
@@ -505,7 +505,7 @@ Return all keys in the map. This requires keys to be copyable.
 Return all values in the map. This requires values to be copyable.
 
 
-
public fun values<Key, Value: copy>(map: &simple_map::SimpleMap<Key, Value>): vector<Value>
+
public fun values<Key, Value: copy>(self: &simple_map::SimpleMap<Key, Value>): vector<Value>
 
@@ -514,8 +514,8 @@ Return all values in the map. This requires values to be copyable. Implementation -
public fun values<Key, Value: copy>(map: &SimpleMap<Key, Value>): vector<Value> {
-    vector::map_ref(&map.data, |e| {
+
public fun values<Key, Value: copy>(self: &SimpleMap<Key, Value>): vector<Value> {
+    vector::map_ref(&self.data, |e| {
         let e: &Element<Key, Value> = e;
         e.value
     })
@@ -534,7 +534,7 @@ Transform the map into two vectors with the keys and values respectively
 Primarily used to destroy a map
 
 
-
public fun to_vec_pair<Key: store, Value: store>(map: simple_map::SimpleMap<Key, Value>): (vector<Key>, vector<Value>)
+
public fun to_vec_pair<Key: store, Value: store>(self: simple_map::SimpleMap<Key, Value>): (vector<Key>, vector<Value>)
 
@@ -544,10 +544,10 @@ Primarily used to destroy a map
public fun to_vec_pair<Key: store, Value: store>(
-    map: SimpleMap<Key, Value>): (vector<Key>, vector<Value>) {
+    self: SimpleMap<Key, Value>): (vector<Key>, vector<Value>) {
     let keys: vector<Key> = vector::empty();
     let values: vector<Value> = vector::empty();
-    let SimpleMap { data } = map;
+    let SimpleMap { data } = self;
     vector::for_each(data, |e| {
         let Element { key, value } = e;
         vector::push_back(&mut keys, key);
@@ -569,7 +569,7 @@ For maps that cannot be dropped this is a utility to destroy them
 using lambdas to destroy the individual keys and values.
 
 
-
public fun destroy<Key: store, Value: store>(map: simple_map::SimpleMap<Key, Value>, dk: |Key|, dv: |Value|)
+
public fun destroy<Key: store, Value: store>(self: simple_map::SimpleMap<Key, Value>, dk: |Key|, dv: |Value|)
 
@@ -579,11 +579,11 @@ using lambdas to destroy the individual keys and values.
public inline fun destroy<Key: store, Value: store>(
-    map: SimpleMap<Key, Value>,
+    self: SimpleMap<Key, Value>,
     dk: |Key|,
     dv: |Value|
 ) {
-    let (keys, values) = to_vec_pair(map);
+    let (keys, values) = to_vec_pair(self);
     vector::destroy(keys, |_k| dk(_k));
     vector::destroy(values, |_v| dv(_v));
 }
@@ -600,7 +600,7 @@ using lambdas to destroy the individual keys and values.
 Remove a key/value pair from the map. The key must exist.
 
 
-
public fun remove<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)
+
public fun remove<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)
 
@@ -610,13 +610,13 @@ Remove a key/value pair from the map. The key must exist.
public fun remove<Key: store, Value: store>(
-    map: &mut SimpleMap<Key, Value>,
+    self: &mut SimpleMap<Key, Value>,
     key: &Key,
 ): (Key, Value) {
-    let maybe_idx = find(map, key);
+    let maybe_idx = find(self, key);
     assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
     let placement = option::extract(&mut maybe_idx);
-    let Element { key, value } = vector::swap_remove(&mut map.data, placement);
+    let Element { key, value } = vector::swap_remove(&mut self.data, placement);
     (key, value)
 }
 
@@ -631,7 +631,7 @@ Remove a key/value pair from the map. The key must exist. -
fun find<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): option::Option<u64>
+
fun find<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): option::Option<u64>
 
@@ -641,13 +641,13 @@ Remove a key/value pair from the map. The key must exist.
fun find<Key: store, Value: store>(
-    map: &SimpleMap<Key, Value>,
+    self: &SimpleMap<Key, Value>,
     key: &Key,
 ): option::Option<u64> {
-    let leng = vector::length(&map.data);
+    let leng = vector::length(&self.data);
     let i = 0;
     while (i < leng) {
-        let element = vector::borrow(&map.data, i);
+        let element = vector::borrow(&self.data, i);
         if (&element.key == key) {
             return option::some(i)
         };
@@ -710,7 +710,7 @@ Remove a key/value pair from the map. The key must exist.
 ### Function `length`
 
 
-
public fun length<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>): u64
+
public fun length<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>): u64
 
@@ -785,7 +785,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `borrow` -
public fun borrow<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value
+
public fun borrow<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): &Value
 
@@ -801,7 +801,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `borrow_mut` -
public fun borrow_mut<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value
+
public fun borrow_mut<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: &Key): &mut Value
 
@@ -817,7 +817,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `contains_key` -
public fun contains_key<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): bool
+
public fun contains_key<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): bool
 
@@ -833,7 +833,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `destroy_empty` -
public fun destroy_empty<Key: store, Value: store>(map: simple_map::SimpleMap<Key, Value>)
+
public fun destroy_empty<Key: store, Value: store>(self: simple_map::SimpleMap<Key, Value>)
 
@@ -849,7 +849,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `add` -
public fun add<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)
+
public fun add<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value)
 
@@ -865,7 +865,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `add_all` -
public fun add_all<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, keys: vector<Key>, values: vector<Value>)
+
public fun add_all<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, keys: vector<Key>, values: vector<Value>)
 
@@ -881,7 +881,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `upsert` -
public fun upsert<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value): (option::Option<Key>, option::Option<Value>)
+
public fun upsert<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: Key, value: Value): (option::Option<Key>, option::Option<Value>)
 
@@ -890,12 +890,14 @@ Remove a key/value pair from the map. The key must exist.
pragma intrinsic;
 pragma opaque;
 aborts_if [abstract] false;
-ensures [abstract] !spec_contains_key(old(map), key) ==> option::is_none(result_1);
-ensures [abstract] !spec_contains_key(old(map), key) ==> option::is_none(result_2);
-ensures [abstract] spec_contains_key(map, key);
-ensures [abstract] spec_get(map, key) == value;
-ensures [abstract] spec_contains_key(old(map), key) ==> ((option::is_some(result_1)) && (option::spec_borrow(result_1) == key));
-ensures [abstract] spec_contains_key(old(map), key) ==> ((option::is_some(result_2)) && (option::spec_borrow(result_2) == spec_get(old(map), key)));
+ensures [abstract] !spec_contains_key(old(self), key) ==> option::is_none(result_1);
+ensures [abstract] !spec_contains_key(old(self), key) ==> option::is_none(result_2);
+ensures [abstract] spec_contains_key(self, key);
+ensures [abstract] spec_get(self, key) == value;
+ensures [abstract] spec_contains_key(old(self), key) ==> ((option::is_some(result_1)) && (option::spec_borrow(result_1) == key));
+ensures [abstract] spec_contains_key(old(self), key) ==> ((option::is_some(result_2)) && (option::spec_borrow(result_2) == spec_get(old(
+    self
+), key)));
 
@@ -950,7 +952,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `keys` -
public fun keys<Key: copy, Value>(map: &simple_map::SimpleMap<Key, Value>): vector<Key>
+
public fun keys<Key: copy, Value>(self: &simple_map::SimpleMap<Key, Value>): vector<Key>
 
@@ -966,7 +968,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `values` -
public fun values<Key, Value: copy>(map: &simple_map::SimpleMap<Key, Value>): vector<Value>
+
public fun values<Key, Value: copy>(self: &simple_map::SimpleMap<Key, Value>): vector<Value>
 
@@ -982,7 +984,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `to_vec_pair` -
public fun to_vec_pair<Key: store, Value: store>(map: simple_map::SimpleMap<Key, Value>): (vector<Key>, vector<Value>)
+
public fun to_vec_pair<Key: store, Value: store>(self: simple_map::SimpleMap<Key, Value>): (vector<Key>, vector<Value>)
 
@@ -992,9 +994,9 @@ Remove a key/value pair from the map. The key must exist. pragma opaque; ensures [abstract] forall k: Key: vector::spec_contains(result_1, k) <==> - spec_contains_key(map, k); + spec_contains_key(self, k); ensures [abstract] forall i in 0..len(result_1): - spec_get(map, vector::borrow(result_1, i)) == vector::borrow(result_2, i); + spec_get(self, vector::borrow(result_1, i)) == vector::borrow(result_2, i);
@@ -1004,7 +1006,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `remove` -
public fun remove<Key: store, Value: store>(map: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)
+
public fun remove<Key: store, Value: store>(self: &mut simple_map::SimpleMap<Key, Value>, key: &Key): (Key, Value)
 
@@ -1020,7 +1022,7 @@ Remove a key/value pair from the map. The key must exist. ### Function `find` -
fun find<Key: store, Value: store>(map: &simple_map::SimpleMap<Key, Value>, key: &Key): option::Option<u64>
+
fun find<Key: store, Value: store>(self: &simple_map::SimpleMap<Key, Value>, key: &Key): option::Option<u64>
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/smart_table.md b/aptos-move/framework/aptos-stdlib/doc/smart_table.md index d69931fabb804..ac5388661f2ca 100644 --- a/aptos-move/framework/aptos-stdlib/doc/smart_table.md +++ b/aptos-move/framework/aptos-stdlib/doc/smart_table.md @@ -360,7 +360,7 @@ Destroy empty table. Aborts if it's not empty. -
public fun destroy_empty<K, V>(table: smart_table::SmartTable<K, V>)
+
public fun destroy_empty<K, V>(self: smart_table::SmartTable<K, V>)
 
@@ -369,14 +369,14 @@ Aborts if it's not empty. Implementation -
public fun destroy_empty<K, V>(table: SmartTable<K, V>) {
-    assert!(table.size == 0, error::invalid_argument(ENOT_EMPTY));
+
public fun destroy_empty<K, V>(self: SmartTable<K, V>) {
+    assert!(self.size == 0, error::invalid_argument(ENOT_EMPTY));
     let i = 0;
-    while (i < table.num_buckets) {
-        vector::destroy_empty(table_with_length::remove(&mut table.buckets, i));
+    while (i < self.num_buckets) {
+        vector::destroy_empty(table_with_length::remove(&mut self.buckets, i));
         i = i + 1;
     };
-    let SmartTable { buckets, num_buckets: _, level: _, size: _, split_load_threshold: _, target_bucket_size: _ } = table;
+    let SmartTable { buckets, num_buckets: _, level: _, size: _, split_load_threshold: _, target_bucket_size: _ } = self;
     table_with_length::destroy_empty(buckets);
 }
 
@@ -392,7 +392,7 @@ Aborts if it's not empty. Destroy a table completely when V has drop. -
public fun destroy<K: drop, V: drop>(table: smart_table::SmartTable<K, V>)
+
public fun destroy<K: drop, V: drop>(self: smart_table::SmartTable<K, V>)
 
@@ -401,9 +401,9 @@ Destroy a table completely when V has drop. Implementation -
public fun destroy<K: drop, V: drop>(table: SmartTable<K, V>) {
-    clear(&mut table);
-    destroy_empty(table);
+
public fun destroy<K: drop, V: drop>(self: SmartTable<K, V>) {
+    clear(&mut self);
+    destroy_empty(self);
 }
 
@@ -418,7 +418,7 @@ Destroy a table completely when V has drop. Clear a table completely when T has drop. -
public fun clear<K: drop, V: drop>(table: &mut smart_table::SmartTable<K, V>)
+
public fun clear<K: drop, V: drop>(self: &mut smart_table::SmartTable<K, V>)
 
@@ -427,16 +427,16 @@ Clear a table completely when T has drop. Implementation -
public fun clear<K: drop, V: drop>(table: &mut SmartTable<K, V>) {
-    *table_with_length::borrow_mut(&mut table.buckets, 0) = vector::empty();
+
public fun clear<K: drop, V: drop>(self: &mut SmartTable<K, V>) {
+    *table_with_length::borrow_mut(&mut self.buckets, 0) = vector::empty();
     let i = 1;
-    while (i < table.num_buckets) {
-        table_with_length::remove(&mut table.buckets, i);
+    while (i < self.num_buckets) {
+        table_with_length::remove(&mut self.buckets, i);
         i = i + 1;
     };
-    table.num_buckets = 1;
-    table.level = 0;
-    table.size = 0;
+    self.num_buckets = 1;
+    self.level = 0;
+    self.size = 0;
 }
 
@@ -455,7 +455,7 @@ Abort if key already exists. Note: This method may occasionally cost much more gas when triggering bucket split. -
public fun add<K, V>(table: &mut smart_table::SmartTable<K, V>, key: K, value: V)
+
public fun add<K, V>(self: &mut smart_table::SmartTable<K, V>, key: K, value: V)
 
@@ -464,10 +464,10 @@ Note: This method may occasionally cost much more gas when triggering bucket spl Implementation -
public fun add<K, V>(table: &mut SmartTable<K, V>, key: K, value: V) {
+
public fun add<K, V>(self: &mut SmartTable<K, V>, key: K, value: V) {
     let hash = sip_hash_from_value(&key);
-    let index = bucket_index(table.level, table.num_buckets, hash);
-    let bucket = table_with_length::borrow_mut(&mut table.buckets, index);
+    let index = bucket_index(self.level, self.num_buckets, hash);
+    let bucket = table_with_length::borrow_mut(&mut self.buckets, index);
     // We set a per-bucket limit here with a upper bound (10000) that nobody should normally reach.
     assert!(vector::length(bucket) <= 10000, error::permission_denied(EEXCEED_MAX_BUCKET_SIZE));
     assert!(vector::all(bucket, | entry | {
@@ -475,15 +475,15 @@ Note: This method may occasionally cost much more gas when triggering bucket spl
         &e.key != &key
     }), error::invalid_argument(EALREADY_EXIST));
     let e = Entry { hash, key, value };
-    if (table.target_bucket_size == 0) {
+    if (self.target_bucket_size == 0) {
         let estimated_entry_size = max(size_of_val(&e), 1);
-        table.target_bucket_size = max(1024 /* free_write_quota */ / estimated_entry_size, 1);
+        self.target_bucket_size = max(1024 /* free_write_quota */ / estimated_entry_size, 1);
     };
     vector::push_back(bucket, e);
-    table.size = table.size + 1;
+    self.size = self.size + 1;
 
-    if (load_factor(table) >= (table.split_load_threshold as u64)) {
-        split_one_bucket(table);
+    if (load_factor(self) >= (self.split_load_threshold as u64)) {
+        split_one_bucket(self);
     }
 }
 
@@ -499,7 +499,7 @@ Note: This method may occasionally cost much more gas when triggering bucket spl Add multiple key/value pairs to the smart table. The keys must not already exist. -
public fun add_all<K, V>(table: &mut smart_table::SmartTable<K, V>, keys: vector<K>, values: vector<V>)
+
public fun add_all<K, V>(self: &mut smart_table::SmartTable<K, V>, keys: vector<K>, values: vector<V>)
 
@@ -508,8 +508,8 @@ Add multiple key/value pairs to the smart table. The keys must not already exist Implementation -
public fun add_all<K, V>(table: &mut SmartTable<K, V>, keys: vector<K>, values: vector<V>) {
-    vector::zip(keys, values, |key, value| { add(table, key, value); });
+
public fun add_all<K, V>(self: &mut SmartTable<K, V>, keys: vector<K>, values: vector<V>) {
+    vector::zip(keys, values, |key, value| { add(self, key, value); });
 }
 
@@ -557,7 +557,7 @@ view of the whole table. Disclaimer: This function may be costly as the smart table may be huge in size. Use it at your own discretion. -
public fun to_simple_map<K: copy, drop, store, V: copy, store>(table: &smart_table::SmartTable<K, V>): simple_map::SimpleMap<K, V>
+
public fun to_simple_map<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>): simple_map::SimpleMap<K, V>
 
@@ -567,12 +567,12 @@ Disclaimer: This function may be costly as the smart table may be huge in size.
public fun to_simple_map<K: store + copy + drop, V: store + copy>(
-    table: &SmartTable<K, V>,
+    self: &SmartTable<K, V>,
 ): SimpleMap<K, V> {
     let i = 0;
     let res = simple_map::new<K, V>();
-    while (i < table.num_buckets) {
-        let (keys, values) = unzip_entries(table_with_length::borrow(&table.buckets, i));
+    while (i < self.num_buckets) {
+        let (keys, values) = unzip_entries(table_with_length::borrow(&self.buckets, i));
         simple_map::add_all(&mut res, keys, values);
         i = i + 1;
     };
@@ -594,7 +594,7 @@ For a large enough smart table this function will fail due to execution gas limi
 keys_paginated should be used instead.
 
 
-
public fun keys<K: copy, drop, store, V: copy, store>(table_ref: &smart_table::SmartTable<K, V>): vector<K>
+
public fun keys<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>): vector<K>
 
@@ -604,9 +604,9 @@ For a large enough smart table this function will fail due to execution gas limi
public fun keys<K: store + copy + drop, V: store + copy>(
-    table_ref: &SmartTable<K, V>
+    self: &SmartTable<K, V>
 ): vector<K> {
-    let (keys, _, _) = keys_paginated(table_ref, 0, 0, length(table_ref));
+    let (keys, _, _) = keys_paginated(self, 0, 0, length(self));
     keys
 }
 
@@ -634,7 +634,7 @@ returned bucket index and vector index value options are both none, which means pagination is complete. For an example, see test_keys(). -
public fun keys_paginated<K: copy, drop, store, V: copy, store>(table_ref: &smart_table::SmartTable<K, V>, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64): (vector<K>, option::Option<u64>, option::Option<u64>)
+
public fun keys_paginated<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64): (vector<K>, option::Option<u64>, option::Option<u64>)
 
@@ -644,7 +644,7 @@ pagination is complete. For an example, see test_keys().
public fun keys_paginated<K: store + copy + drop, V: store + copy>(
-    table_ref: &SmartTable<K, V>,
+    self: &SmartTable<K, V>,
     starting_bucket_index: u64,
     starting_vector_index: u64,
     num_keys_to_get: u64,
@@ -653,8 +653,8 @@ pagination is complete. For an example, see test_keys().
     Option<u64>,
     Option<u64>,
 ) {
-    let num_buckets = table_ref.num_buckets;
-    let buckets_ref = &table_ref.buckets;
+    let num_buckets = self.num_buckets;
+    let buckets_ref = &self.buckets;
     assert!(starting_bucket_index < num_buckets, EINVALID_BUCKET_INDEX);
     let bucket_ref = table_with_length::borrow(buckets_ref, starting_bucket_index);
     let bucket_length = vector::length(bucket_ref);
@@ -707,7 +707,7 @@ pagination is complete. For an example, see test_keys().
 Decide which is the next bucket to split and split it into two with the elements inside the bucket.
 
 
-
fun split_one_bucket<K, V>(table: &mut smart_table::SmartTable<K, V>)
+
fun split_one_bucket<K, V>(self: &mut smart_table::SmartTable<K, V>)
 
@@ -716,23 +716,23 @@ Decide which is the next bucket to split and split it into two with the elements Implementation -
fun split_one_bucket<K, V>(table: &mut SmartTable<K, V>) {
-    let new_bucket_index = table.num_buckets;
+
fun split_one_bucket<K, V>(self: &mut SmartTable<K, V>) {
+    let new_bucket_index = self.num_buckets;
     // the next bucket to split is num_bucket without the most significant bit.
-    let to_split = new_bucket_index ^ (1 << table.level);
-    table.num_buckets = new_bucket_index + 1;
+    let to_split = new_bucket_index ^ (1 << self.level);
+    self.num_buckets = new_bucket_index + 1;
     // if the whole level is splitted once, bump the level.
-    if (to_split + 1 == 1 << table.level) {
-        table.level = table.level + 1;
+    if (to_split + 1 == 1 << self.level) {
+        self.level = self.level + 1;
     };
-    let old_bucket = table_with_length::borrow_mut(&mut table.buckets, to_split);
+    let old_bucket = table_with_length::borrow_mut(&mut self.buckets, to_split);
     // partition the bucket, [0..p) stays in old bucket, [p..len) goes to new bucket
     let p = vector::partition(old_bucket, |e| {
         let entry: &Entry<K, V> = e; // Explicit type to satisfy compiler
-        bucket_index(table.level, table.num_buckets, entry.hash) != new_bucket_index
+        bucket_index(self.level, self.num_buckets, entry.hash) != new_bucket_index
     });
     let new_bucket = vector::trim_reverse(old_bucket, p);
-    table_with_length::add(&mut table.buckets, new_bucket_index, new_bucket);
+    table_with_length::add(&mut self.buckets, new_bucket_index, new_bucket);
 }
 
@@ -782,7 +782,7 @@ Acquire an immutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow<K: drop, V>(table: &smart_table::SmartTable<K, V>, key: K): &V
+
public fun borrow<K: drop, V>(self: &smart_table::SmartTable<K, V>, key: K): &V
 
@@ -791,9 +791,9 @@ Aborts if there is no entry for key. Implementation -
public fun borrow<K: drop, V>(table: &SmartTable<K, V>, key: K): &V {
-    let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key));
-    let bucket = table_with_length::borrow(&table.buckets, index);
+
public fun borrow<K: drop, V>(self: &SmartTable<K, V>, key: K): &V {
+    let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key));
+    let bucket = table_with_length::borrow(&self.buckets, index);
     let i = 0;
     let len = vector::length(bucket);
     while (i < len) {
@@ -819,7 +819,7 @@ Acquire an immutable reference to the value which key maps to.
 Returns specified default value if there is no entry for key.
 
 
-
public fun borrow_with_default<K: copy, drop, V>(table: &smart_table::SmartTable<K, V>, key: K, default: &V): &V
+
public fun borrow_with_default<K: copy, drop, V>(self: &smart_table::SmartTable<K, V>, key: K, default: &V): &V
 
@@ -828,11 +828,11 @@ Returns specified default value if there is no entry for key. Implementation -
public fun borrow_with_default<K: copy + drop, V>(table: &SmartTable<K, V>, key: K, default: &V): &V {
-    if (!contains(table, copy key)) {
+
public fun borrow_with_default<K: copy + drop, V>(self: &SmartTable<K, V>, key: K, default: &V): &V {
+    if (!contains(self, copy key)) {
         default
     } else {
-        borrow(table, copy key)
+        borrow(self, copy key)
     }
 }
 
@@ -849,7 +849,7 @@ Acquire a mutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow_mut<K: drop, V>(table: &mut smart_table::SmartTable<K, V>, key: K): &mut V
+
public fun borrow_mut<K: drop, V>(self: &mut smart_table::SmartTable<K, V>, key: K): &mut V
 
@@ -858,9 +858,9 @@ Aborts if there is no entry for key. Implementation -
public fun borrow_mut<K: drop, V>(table: &mut SmartTable<K, V>, key: K): &mut V {
-    let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key));
-    let bucket = table_with_length::borrow_mut(&mut table.buckets, index);
+
public fun borrow_mut<K: drop, V>(self: &mut SmartTable<K, V>, key: K): &mut V {
+    let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key));
+    let bucket = table_with_length::borrow_mut(&mut self.buckets, index);
     let i = 0;
     let len = vector::length(bucket);
     while (i < len) {
@@ -886,7 +886,7 @@ Acquire a mutable reference to the value which key maps to.
 Insert the pair (key, default) first if there is no entry for key.
 
 
-
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut smart_table::SmartTable<K, V>, key: K, default: V): &mut V
+
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut smart_table::SmartTable<K, V>, key: K, default: V): &mut V
 
@@ -896,14 +896,14 @@ Insert the pair (key, default) first if there is no en
public fun borrow_mut_with_default<K: copy + drop, V: drop>(
-    table: &mut SmartTable<K, V>,
+    self: &mut SmartTable<K, V>,
     key: K,
     default: V
 ): &mut V {
-    if (!contains(table, copy key)) {
-        add(table, copy key, default)
+    if (!contains(self, copy key)) {
+        add(self, copy key, default)
     };
-    borrow_mut(table, key)
+    borrow_mut(self, key)
 }
 
@@ -918,7 +918,7 @@ Insert the pair (key, default) first if there is no en Returns true iff table contains an entry for key. -
public fun contains<K: drop, V>(table: &smart_table::SmartTable<K, V>, key: K): bool
+
public fun contains<K: drop, V>(self: &smart_table::SmartTable<K, V>, key: K): bool
 
@@ -927,10 +927,10 @@ Returns true iff table contains an Implementation -
public fun contains<K: drop, V>(table: &SmartTable<K, V>, key: K): bool {
+
public fun contains<K: drop, V>(self: &SmartTable<K, V>, key: K): bool {
     let hash = sip_hash_from_value(&key);
-    let index = bucket_index(table.level, table.num_buckets, hash);
-    let bucket = table_with_length::borrow(&table.buckets, index);
+    let index = bucket_index(self.level, self.num_buckets, hash);
+    let bucket = table_with_length::borrow(&self.buckets, index);
     vector::any(bucket, | entry | {
         let e: &Entry<K, V> = entry;
         e.hash == hash && &e.key == &key
@@ -950,7 +950,7 @@ Remove from table and return the v
 Aborts if there is no entry for key.
 
 
-
public fun remove<K: copy, drop, V>(table: &mut smart_table::SmartTable<K, V>, key: K): V
+
public fun remove<K: copy, drop, V>(self: &mut smart_table::SmartTable<K, V>, key: K): V
 
@@ -959,16 +959,16 @@ Aborts if there is no entry for key. Implementation -
public fun remove<K: copy + drop, V>(table: &mut SmartTable<K, V>, key: K): V {
-    let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key));
-    let bucket = table_with_length::borrow_mut(&mut table.buckets, index);
+
public fun remove<K: copy + drop, V>(self: &mut SmartTable<K, V>, key: K): V {
+    let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key));
+    let bucket = table_with_length::borrow_mut(&mut self.buckets, index);
     let i = 0;
     let len = vector::length(bucket);
     while (i < len) {
         let entry = vector::borrow(bucket, i);
         if (&entry.key == &key) {
             let Entry { hash: _, key: _, value } = vector::swap_remove(bucket, i);
-            table.size = table.size - 1;
+            self.size = self.size - 1;
             return value
         };
         i = i + 1;
@@ -989,7 +989,7 @@ Insert the pair (key, value) if there is no entry for
 update the value of the entry for key to value otherwise
 
 
-
public fun upsert<K: copy, drop, V: drop>(table: &mut smart_table::SmartTable<K, V>, key: K, value: V)
+
public fun upsert<K: copy, drop, V: drop>(self: &mut smart_table::SmartTable<K, V>, key: K, value: V)
 
@@ -998,11 +998,11 @@ update the value of the entry for key to value otherwi Implementation -
public fun upsert<K: copy + drop, V: drop>(table: &mut SmartTable<K, V>, key: K, value: V) {
-    if (!contains(table, copy key)) {
-        add(table, copy key, value)
+
public fun upsert<K: copy + drop, V: drop>(self: &mut SmartTable<K, V>, key: K, value: V) {
+    if (!contains(self, copy key)) {
+        add(self, copy key, value)
     } else {
-        let ref = borrow_mut(table, key);
+        let ref = borrow_mut(self, key);
         *ref = value;
     };
 }
@@ -1019,7 +1019,7 @@ update the value of the entry for key to value otherwi
 Returns the length of the table, i.e. the number of entries.
 
 
-
public fun length<K, V>(table: &smart_table::SmartTable<K, V>): u64
+
public fun length<K, V>(self: &smart_table::SmartTable<K, V>): u64
 
@@ -1028,8 +1028,8 @@ Returns the length of the table, i.e. the number of entries. Implementation -
public fun length<K, V>(table: &SmartTable<K, V>): u64 {
-    table.size
+
public fun length<K, V>(self: &SmartTable<K, V>): u64 {
+    self.size
 }
 
@@ -1044,7 +1044,7 @@ Returns the length of the table, i.e. the number of entries. Return the load factor of the hashtable. -
public fun load_factor<K, V>(table: &smart_table::SmartTable<K, V>): u64
+
public fun load_factor<K, V>(self: &smart_table::SmartTable<K, V>): u64
 
@@ -1053,8 +1053,8 @@ Return the load factor of the hashtable. Implementation -
public fun load_factor<K, V>(table: &SmartTable<K, V>): u64 {
-    table.size * 100 / table.num_buckets / table.target_bucket_size
+
public fun load_factor<K, V>(self: &SmartTable<K, V>): u64 {
+    self.size * 100 / self.num_buckets / self.target_bucket_size
 }
 
@@ -1069,7 +1069,7 @@ Return the load factor of the hashtable. Update split_load_threshold. -
public fun update_split_load_threshold<K, V>(table: &mut smart_table::SmartTable<K, V>, split_load_threshold: u8)
+
public fun update_split_load_threshold<K, V>(self: &mut smart_table::SmartTable<K, V>, split_load_threshold: u8)
 
@@ -1078,12 +1078,12 @@ Update split_load_threshold. Implementation -
public fun update_split_load_threshold<K, V>(table: &mut SmartTable<K, V>, split_load_threshold: u8) {
+
public fun update_split_load_threshold<K, V>(self: &mut SmartTable<K, V>, split_load_threshold: u8) {
     assert!(
         split_load_threshold <= 100 && split_load_threshold > 0,
         error::invalid_argument(EINVALID_LOAD_THRESHOLD_PERCENT)
     );
-    table.split_load_threshold = split_load_threshold;
+    self.split_load_threshold = split_load_threshold;
 }
 
@@ -1098,7 +1098,7 @@ Update split_load_threshold. Update target_bucket_size. -
public fun update_target_bucket_size<K, V>(table: &mut smart_table::SmartTable<K, V>, target_bucket_size: u64)
+
public fun update_target_bucket_size<K, V>(self: &mut smart_table::SmartTable<K, V>, target_bucket_size: u64)
 
@@ -1107,9 +1107,9 @@ Update target_bucket_size. Implementation -
public fun update_target_bucket_size<K, V>(table: &mut SmartTable<K, V>, target_bucket_size: u64) {
+
public fun update_target_bucket_size<K, V>(self: &mut SmartTable<K, V>, target_bucket_size: u64) {
     assert!(target_bucket_size > 0, error::invalid_argument(EINVALID_TARGET_BUCKET_SIZE));
-    table.target_bucket_size = target_bucket_size;
+    self.target_bucket_size = target_bucket_size;
 }
 
@@ -1124,7 +1124,7 @@ Update target_bucket_size. Apply the function to a reference of each key-value pair in the table. -
public fun for_each_ref<K, V>(table: &smart_table::SmartTable<K, V>, f: |(&K, &V)|)
+
public fun for_each_ref<K, V>(self: &smart_table::SmartTable<K, V>, f: |(&K, &V)|)
 
@@ -1133,11 +1133,11 @@ Apply the function to a reference of each key-value pair in the table. Implementation -
public inline fun for_each_ref<K, V>(table: &SmartTable<K, V>, f: |&K, &V|) {
+
public inline fun for_each_ref<K, V>(self: &SmartTable<K, V>, f: |&K, &V|) {
     let i = 0;
-    while (i < aptos_std::smart_table::num_buckets(table)) {
+    while (i < aptos_std::smart_table::num_buckets(self)) {
         vector::for_each_ref(
-            aptos_std::table_with_length::borrow(aptos_std::smart_table::borrow_buckets(table), i),
+            aptos_std::table_with_length::borrow(aptos_std::smart_table::borrow_buckets(self), i),
             |elem| {
                 let (key, value) = aptos_std::smart_table::borrow_kv(elem);
                 f(key, value)
@@ -1159,7 +1159,7 @@ Apply the function to a reference of each key-value pair in the table.
 Apply the function to a mutable reference of each key-value pair in the table.
 
 
-
public fun for_each_mut<K, V>(table: &mut smart_table::SmartTable<K, V>, f: |(&K, &mut V)|)
+
public fun for_each_mut<K, V>(self: &mut smart_table::SmartTable<K, V>, f: |(&K, &mut V)|)
 
@@ -1168,11 +1168,11 @@ Apply the function to a mutable reference of each key-value pair in the table. Implementation -
public inline fun for_each_mut<K, V>(table: &mut SmartTable<K, V>, f: |&K, &mut V|) {
+
public inline fun for_each_mut<K, V>(self: &mut SmartTable<K, V>, f: |&K, &mut V|) {
     let i = 0;
-    while (i < aptos_std::smart_table::num_buckets(table)) {
+    while (i < aptos_std::smart_table::num_buckets(self)) {
         vector::for_each_mut(
-            table_with_length::borrow_mut(aptos_std::smart_table::borrow_buckets_mut(table), i),
+            table_with_length::borrow_mut(aptos_std::smart_table::borrow_buckets_mut(self), i),
             |elem| {
                 let (key, value) = aptos_std::smart_table::borrow_kv_mut(elem);
                 f(key, value)
@@ -1194,7 +1194,7 @@ Apply the function to a mutable reference of each key-value pair in the table.
 Map the function over the references of key-value pairs in the table without modifying it.
 
 
-
public fun map_ref<K: copy, drop, store, V1, V2: store>(table: &smart_table::SmartTable<K, V1>, f: |&V1|V2): smart_table::SmartTable<K, V2>
+
public fun map_ref<K: copy, drop, store, V1, V2: store>(self: &smart_table::SmartTable<K, V1>, f: |&V1|V2): smart_table::SmartTable<K, V2>
 
@@ -1204,11 +1204,11 @@ Map the function over the references of key-value pairs in the table without mod
public inline fun map_ref<K: copy + drop + store, V1, V2: store>(
-    table: &SmartTable<K, V1>,
+    self: &SmartTable<K, V1>,
     f: |&V1|V2
 ): SmartTable<K, V2> {
     let new_table = new<K, V2>();
-    for_each_ref(table, |key, value| add(&mut new_table, *key, f(value)));
+    for_each_ref(self, |key, value| add(&mut new_table, *key, f(value)));
     new_table
 }
 
@@ -1224,7 +1224,7 @@ Map the function over the references of key-value pairs in the table without mod Return true if any key-value pair in the table satisfies the predicate. -
public fun any<K, V>(table: &smart_table::SmartTable<K, V>, p: |(&K, &V)|bool): bool
+
public fun any<K, V>(self: &smart_table::SmartTable<K, V>, p: |(&K, &V)|bool): bool
 
@@ -1234,13 +1234,13 @@ Return true if any key-value pair in the table satisfies the predicate.
public inline fun any<K, V>(
-    table: &SmartTable<K, V>,
+    self: &SmartTable<K, V>,
     p: |&K, &V|bool
 ): bool {
     let found = false;
     let i = 0;
-    while (i < aptos_std::smart_table::num_buckets(table)) {
-        found = vector::any(table_with_length::borrow(aptos_std::smart_table::borrow_buckets(table), i), |elem| {
+    while (i < aptos_std::smart_table::num_buckets(self)) {
+        found = vector::any(table_with_length::borrow(aptos_std::smart_table::borrow_buckets(self), i), |elem| {
             let (key, value) = aptos_std::smart_table::borrow_kv(elem);
             p(key, value)
         });
@@ -1261,7 +1261,7 @@ Return true if any key-value pair in the table satisfies the predicate.
 
 
 
-
public fun borrow_kv<K, V>(e: &smart_table::Entry<K, V>): (&K, &V)
+
public fun borrow_kv<K, V>(self: &smart_table::Entry<K, V>): (&K, &V)
 
@@ -1270,8 +1270,8 @@ Return true if any key-value pair in the table satisfies the predicate. Implementation -
public fun borrow_kv<K, V>(e: &Entry<K, V>): (&K, &V) {
-    (&e.key, &e.value)
+
public fun borrow_kv<K, V>(self: &Entry<K, V>): (&K, &V) {
+    (&self.key, &self.value)
 }
 
@@ -1285,7 +1285,7 @@ Return true if any key-value pair in the table satisfies the predicate. -
public fun borrow_kv_mut<K, V>(e: &mut smart_table::Entry<K, V>): (&mut K, &mut V)
+
public fun borrow_kv_mut<K, V>(self: &mut smart_table::Entry<K, V>): (&mut K, &mut V)
 
@@ -1294,8 +1294,8 @@ Return true if any key-value pair in the table satisfies the predicate. Implementation -
public fun borrow_kv_mut<K, V>(e: &mut Entry<K, V>): (&mut K, &mut V) {
-    (&mut e.key, &mut e.value)
+
public fun borrow_kv_mut<K, V>(self: &mut Entry<K, V>): (&mut K, &mut V) {
+    (&mut self.key, &mut self.value)
 }
 
@@ -1309,7 +1309,7 @@ Return true if any key-value pair in the table satisfies the predicate. -
public fun num_buckets<K, V>(table: &smart_table::SmartTable<K, V>): u64
+
public fun num_buckets<K, V>(self: &smart_table::SmartTable<K, V>): u64
 
@@ -1318,8 +1318,8 @@ Return true if any key-value pair in the table satisfies the predicate. Implementation -
public fun num_buckets<K, V>(table: &SmartTable<K, V>): u64 {
-    table.num_buckets
+
public fun num_buckets<K, V>(self: &SmartTable<K, V>): u64 {
+    self.num_buckets
 }
 
@@ -1333,7 +1333,7 @@ Return true if any key-value pair in the table satisfies the predicate. -
public fun borrow_buckets<K, V>(table: &smart_table::SmartTable<K, V>): &table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
+
public fun borrow_buckets<K, V>(self: &smart_table::SmartTable<K, V>): &table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
 
@@ -1342,8 +1342,8 @@ Return true if any key-value pair in the table satisfies the predicate. Implementation -
public fun borrow_buckets<K, V>(table: &SmartTable<K, V>): &TableWithLength<u64, vector<Entry<K, V>>> {
-    &table.buckets
+
public fun borrow_buckets<K, V>(self: &SmartTable<K, V>): &TableWithLength<u64, vector<Entry<K, V>>> {
+    &self.buckets
 }
 
@@ -1357,7 +1357,7 @@ Return true if any key-value pair in the table satisfies the predicate. -
public fun borrow_buckets_mut<K, V>(table: &mut smart_table::SmartTable<K, V>): &mut table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
+
public fun borrow_buckets_mut<K, V>(self: &mut smart_table::SmartTable<K, V>): &mut table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
 
@@ -1366,8 +1366,8 @@ Return true if any key-value pair in the table satisfies the predicate. Implementation -
public fun borrow_buckets_mut<K, V>(table: &mut SmartTable<K, V>): &mut TableWithLength<u64, vector<Entry<K, V>>> {
-    &mut table.buckets
+
public fun borrow_buckets_mut<K, V>(self: &mut SmartTable<K, V>): &mut TableWithLength<u64, vector<Entry<K, V>>> {
+    &mut self.buckets
 }
 
@@ -1472,7 +1472,7 @@ map_spec_has_key = spec_contains; ### Function `destroy` -
public fun destroy<K: drop, V: drop>(table: smart_table::SmartTable<K, V>)
+
public fun destroy<K: drop, V: drop>(self: smart_table::SmartTable<K, V>)
 
@@ -1488,7 +1488,7 @@ map_spec_has_key = spec_contains; ### Function `clear` -
public fun clear<K: drop, V: drop>(table: &mut smart_table::SmartTable<K, V>)
+
public fun clear<K: drop, V: drop>(self: &mut smart_table::SmartTable<K, V>)
 
@@ -1504,7 +1504,7 @@ map_spec_has_key = spec_contains; ### Function `add_all` -
public fun add_all<K, V>(table: &mut smart_table::SmartTable<K, V>, keys: vector<K>, values: vector<V>)
+
public fun add_all<K, V>(self: &mut smart_table::SmartTable<K, V>, keys: vector<K>, values: vector<V>)
 
@@ -1520,7 +1520,7 @@ map_spec_has_key = spec_contains; ### Function `to_simple_map` -
public fun to_simple_map<K: copy, drop, store, V: copy, store>(table: &smart_table::SmartTable<K, V>): simple_map::SimpleMap<K, V>
+
public fun to_simple_map<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>): simple_map::SimpleMap<K, V>
 
@@ -1536,7 +1536,7 @@ map_spec_has_key = spec_contains; ### Function `keys` -
public fun keys<K: copy, drop, store, V: copy, store>(table_ref: &smart_table::SmartTable<K, V>): vector<K>
+
public fun keys<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>): vector<K>
 
@@ -1552,7 +1552,7 @@ map_spec_has_key = spec_contains; ### Function `keys_paginated` -
public fun keys_paginated<K: copy, drop, store, V: copy, store>(table_ref: &smart_table::SmartTable<K, V>, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64): (vector<K>, option::Option<u64>, option::Option<u64>)
+
public fun keys_paginated<K: copy, drop, store, V: copy, store>(self: &smart_table::SmartTable<K, V>, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64): (vector<K>, option::Option<u64>, option::Option<u64>)
 
@@ -1568,7 +1568,7 @@ map_spec_has_key = spec_contains; ### Function `split_one_bucket` -
fun split_one_bucket<K, V>(table: &mut smart_table::SmartTable<K, V>)
+
fun split_one_bucket<K, V>(self: &mut smart_table::SmartTable<K, V>)
 
@@ -1600,7 +1600,7 @@ map_spec_has_key = spec_contains; ### Function `borrow_with_default` -
public fun borrow_with_default<K: copy, drop, V>(table: &smart_table::SmartTable<K, V>, key: K, default: &V): &V
+
public fun borrow_with_default<K: copy, drop, V>(self: &smart_table::SmartTable<K, V>, key: K, default: &V): &V
 
@@ -1616,7 +1616,7 @@ map_spec_has_key = spec_contains; ### Function `load_factor` -
public fun load_factor<K, V>(table: &smart_table::SmartTable<K, V>): u64
+
public fun load_factor<K, V>(self: &smart_table::SmartTable<K, V>): u64
 
@@ -1632,7 +1632,7 @@ map_spec_has_key = spec_contains; ### Function `update_split_load_threshold` -
public fun update_split_load_threshold<K, V>(table: &mut smart_table::SmartTable<K, V>, split_load_threshold: u8)
+
public fun update_split_load_threshold<K, V>(self: &mut smart_table::SmartTable<K, V>, split_load_threshold: u8)
 
@@ -1648,7 +1648,7 @@ map_spec_has_key = spec_contains; ### Function `update_target_bucket_size` -
public fun update_target_bucket_size<K, V>(table: &mut smart_table::SmartTable<K, V>, target_bucket_size: u64)
+
public fun update_target_bucket_size<K, V>(self: &mut smart_table::SmartTable<K, V>, target_bucket_size: u64)
 
@@ -1664,7 +1664,7 @@ map_spec_has_key = spec_contains; ### Function `borrow_kv` -
public fun borrow_kv<K, V>(e: &smart_table::Entry<K, V>): (&K, &V)
+
public fun borrow_kv<K, V>(self: &smart_table::Entry<K, V>): (&K, &V)
 
@@ -1680,7 +1680,7 @@ map_spec_has_key = spec_contains; ### Function `borrow_kv_mut` -
public fun borrow_kv_mut<K, V>(e: &mut smart_table::Entry<K, V>): (&mut K, &mut V)
+
public fun borrow_kv_mut<K, V>(self: &mut smart_table::Entry<K, V>): (&mut K, &mut V)
 
@@ -1696,7 +1696,7 @@ map_spec_has_key = spec_contains; ### Function `num_buckets` -
public fun num_buckets<K, V>(table: &smart_table::SmartTable<K, V>): u64
+
public fun num_buckets<K, V>(self: &smart_table::SmartTable<K, V>): u64
 
@@ -1712,7 +1712,7 @@ map_spec_has_key = spec_contains; ### Function `borrow_buckets` -
public fun borrow_buckets<K, V>(table: &smart_table::SmartTable<K, V>): &table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
+
public fun borrow_buckets<K, V>(self: &smart_table::SmartTable<K, V>): &table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
 
@@ -1728,7 +1728,7 @@ map_spec_has_key = spec_contains; ### Function `borrow_buckets_mut` -
public fun borrow_buckets_mut<K, V>(table: &mut smart_table::SmartTable<K, V>): &mut table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
+
public fun borrow_buckets_mut<K, V>(self: &mut smart_table::SmartTable<K, V>): &mut table_with_length::TableWithLength<u64, vector<smart_table::Entry<K, V>>>
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/smart_vector.md b/aptos-move/framework/aptos-stdlib/doc/smart_vector.md index 45c5e9ddf75ed..cb8748a05d55f 100644 --- a/aptos-move/framework/aptos-stdlib/doc/smart_vector.md +++ b/aptos-move/framework/aptos-stdlib/doc/smart_vector.md @@ -297,11 +297,11 @@ Create a vector of length 1 containing the passed in T. ## Function `destroy_empty` -Destroy the vector v. -Aborts if v is not empty. +Destroy the vector self. +Aborts if self is not empty. -
public fun destroy_empty<T>(v: smart_vector::SmartVector<T>)
+
public fun destroy_empty<T>(self: smart_vector::SmartVector<T>)
 
@@ -310,9 +310,9 @@ Aborts if v is not empty. Implementation -
public fun destroy_empty<T>(v: SmartVector<T>) {
-    assert!(is_empty(&v), error::invalid_argument(EVECTOR_NOT_EMPTY));
-    let SmartVector { inline_vec, big_vec, inline_capacity: _, bucket_size: _ } = v;
+
public fun destroy_empty<T>(self: SmartVector<T>) {
+    assert!(is_empty(&self), error::invalid_argument(EVECTOR_NOT_EMPTY));
+    let SmartVector { inline_vec, big_vec, inline_capacity: _, bucket_size: _ } = self;
     vector::destroy_empty(inline_vec);
     option::destroy_none(big_vec);
 }
@@ -329,7 +329,7 @@ Aborts if v is not empty.
 Destroy a vector completely when T has drop.
 
 
-
public fun destroy<T: drop>(v: smart_vector::SmartVector<T>)
+
public fun destroy<T: drop>(self: smart_vector::SmartVector<T>)
 
@@ -338,9 +338,9 @@ Destroy a vector completely when T has drop. Implementation -
public fun destroy<T: drop>(v: SmartVector<T>) {
-    clear(&mut v);
-    destroy_empty(v);
+
public fun destroy<T: drop>(self: SmartVector<T>) {
+    clear(&mut self);
+    destroy_empty(self);
 }
 
@@ -355,7 +355,7 @@ Destroy a vector completely when T has drop. Clear a vector completely when T has drop. -
public fun clear<T: drop>(v: &mut smart_vector::SmartVector<T>)
+
public fun clear<T: drop>(self: &mut smart_vector::SmartVector<T>)
 
@@ -364,10 +364,10 @@ Clear a vector completely when T has drop. Implementation -
public fun clear<T: drop>(v: &mut SmartVector<T>) {
-    v.inline_vec = vector[];
-    if (option::is_some(&v.big_vec)) {
-        big_vector::destroy(option::extract(&mut v.big_vec));
+
public fun clear<T: drop>(self: &mut SmartVector<T>) {
+    self.inline_vec = vector[];
+    if (option::is_some(&self.big_vec)) {
+        big_vector::destroy(option::extract(&mut self.big_vec));
     }
 }
 
@@ -380,11 +380,11 @@ Clear a vector completely when T has drop. ## Function `borrow` -Acquire an immutable reference to the ith T of the vector v. +Acquire an immutable reference to the ith T of the vector self. Aborts if i is out of bounds. -
public fun borrow<T>(v: &smart_vector::SmartVector<T>, i: u64): &T
+
public fun borrow<T>(self: &smart_vector::SmartVector<T>, i: u64): &T
 
@@ -393,13 +393,13 @@ Aborts if i is out of bounds. Implementation -
public fun borrow<T>(v: &SmartVector<T>, i: u64): &T {
-    assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let inline_len = vector::length(&v.inline_vec);
+
public fun borrow<T>(self: &SmartVector<T>, i: u64): &T {
+    assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    let inline_len = vector::length(&self.inline_vec);
     if (i < inline_len) {
-        vector::borrow(&v.inline_vec, i)
+        vector::borrow(&self.inline_vec, i)
     } else {
-        big_vector::borrow(option::borrow(&v.big_vec), i - inline_len)
+        big_vector::borrow(option::borrow(&self.big_vec), i - inline_len)
     }
 }
 
@@ -412,11 +412,11 @@ Aborts if i is out of bounds. ## Function `borrow_mut` -Return a mutable reference to the ith T in the vector v. +Return a mutable reference to the ith T in the vector self. Aborts if i is out of bounds. -
public fun borrow_mut<T>(v: &mut smart_vector::SmartVector<T>, i: u64): &mut T
+
public fun borrow_mut<T>(self: &mut smart_vector::SmartVector<T>, i: u64): &mut T
 
@@ -425,13 +425,13 @@ Aborts if i is out of bounds. Implementation -
public fun borrow_mut<T>(v: &mut SmartVector<T>, i: u64): &mut T {
-    assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let inline_len = vector::length(&v.inline_vec);
+
public fun borrow_mut<T>(self: &mut SmartVector<T>, i: u64): &mut T {
+    assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
+    let inline_len = vector::length(&self.inline_vec);
     if (i < inline_len) {
-        vector::borrow_mut(&mut v.inline_vec, i)
+        vector::borrow_mut(&mut self.inline_vec, i)
     } else {
-        big_vector::borrow_mut(option::borrow_mut(&mut v.big_vec), i - inline_len)
+        big_vector::borrow_mut(option::borrow_mut(&mut self.big_vec), i - inline_len)
     }
 }
 
@@ -444,12 +444,12 @@ Aborts if i is out of bounds. ## Function `append` -Empty and destroy the other vector, and push each of the Ts in the other vector onto the lhs vector in the +Empty and destroy the other vector, and push each of the Ts in the other vector onto the self vector in the same order as they occurred in other. Disclaimer: This function may be costly. Use it at your own discretion. -
public fun append<T: store>(lhs: &mut smart_vector::SmartVector<T>, other: smart_vector::SmartVector<T>)
+
public fun append<T: store>(self: &mut smart_vector::SmartVector<T>, other: smart_vector::SmartVector<T>)
 
@@ -458,16 +458,16 @@ Disclaimer: This function may be costly. Use it at your own discretion. Implementation -
public fun append<T: store>(lhs: &mut SmartVector<T>, other: SmartVector<T>) {
+
public fun append<T: store>(self: &mut SmartVector<T>, other: SmartVector<T>) {
     let other_len = length(&other);
     let half_other_len = other_len / 2;
     let i = 0;
     while (i < half_other_len) {
-        push_back(lhs, swap_remove(&mut other, i));
+        push_back(self, swap_remove(&mut other, i));
         i = i + 1;
     };
     while (i < other_len) {
-        push_back(lhs, pop_back(&mut other));
+        push_back(self, pop_back(&mut other));
         i = i + 1;
     };
     destroy_empty(other);
@@ -485,7 +485,7 @@ Disclaimer: This function may be costly. Use it at your own discretion.
 Add multiple values to the vector at once.
 
 
-
public fun add_all<T: store>(v: &mut smart_vector::SmartVector<T>, vals: vector<T>)
+
public fun add_all<T: store>(self: &mut smart_vector::SmartVector<T>, vals: vector<T>)
 
@@ -494,8 +494,8 @@ Add multiple values to the vector at once. Implementation -
public fun add_all<T: store>(v: &mut SmartVector<T>, vals: vector<T>) {
-    vector::for_each(vals, |val| { push_back(v, val); })
+
public fun add_all<T: store>(self: &mut SmartVector<T>, vals: vector<T>) {
+    vector::for_each(vals, |val| { push_back(self, val); })
 }
 
@@ -512,7 +512,7 @@ atomic view of the whole vector. Disclaimer: This function may be costly as the smart vector may be huge in size. Use it at your own discretion. -
public fun to_vector<T: copy, store>(v: &smart_vector::SmartVector<T>): vector<T>
+
public fun to_vector<T: copy, store>(self: &smart_vector::SmartVector<T>): vector<T>
 
@@ -521,10 +521,10 @@ Disclaimer: This function may be costly as the smart vector may be huge in size. Implementation -
public fun to_vector<T: store + copy>(v: &SmartVector<T>): vector<T> {
-    let res = v.inline_vec;
-    if (option::is_some(&v.big_vec)) {
-        let big_vec = option::borrow(&v.big_vec);
+
public fun to_vector<T: store + copy>(self: &SmartVector<T>): vector<T> {
+    let res = self.inline_vec;
+    if (option::is_some(&self.big_vec)) {
+        let big_vec = option::borrow(&self.big_vec);
         vector::append(&mut res, big_vector::to_vector(big_vec));
     };
     res
@@ -539,11 +539,11 @@ Disclaimer: This function may be costly as the smart vector may be huge in size.
 
 ## Function `push_back`
 
-Add T val to the end of the vector v. It grows the buckets when the current buckets are full.
+Add T val to the end of the vector self. It grows the buckets when the current buckets are full.
 This operation will cost more gas when it adds new bucket.
 
 
-
public fun push_back<T: store>(v: &mut smart_vector::SmartVector<T>, val: T)
+
public fun push_back<T: store>(self: &mut smart_vector::SmartVector<T>, val: T)
 
@@ -552,28 +552,28 @@ This operation will cost more gas when it adds new bucket. Implementation -
public fun push_back<T: store>(v: &mut SmartVector<T>, val: T) {
-    let len = length(v);
-    let inline_len = vector::length(&v.inline_vec);
+
public fun push_back<T: store>(self: &mut SmartVector<T>, val: T) {
+    let len = length(self);
+    let inline_len = vector::length(&self.inline_vec);
     if (len == inline_len) {
-        let bucket_size = if (option::is_some(&v.inline_capacity)) {
-            if (len < *option::borrow(&v.inline_capacity)) {
-                vector::push_back(&mut v.inline_vec, val);
+        let bucket_size = if (option::is_some(&self.inline_capacity)) {
+            if (len < *option::borrow(&self.inline_capacity)) {
+                vector::push_back(&mut self.inline_vec, val);
                 return
             };
-            *option::borrow(&v.bucket_size)
+            *option::borrow(&self.bucket_size)
         } else {
             let val_size = size_of_val(&val);
             if (val_size * (inline_len + 1) < 150 /* magic number */) {
-                vector::push_back(&mut v.inline_vec, val);
+                vector::push_back(&mut self.inline_vec, val);
                 return
             };
-            let estimated_avg_size = max((size_of_val(&v.inline_vec) + val_size) / (inline_len + 1), 1);
+            let estimated_avg_size = max((size_of_val(&self.inline_vec) + val_size) / (inline_len + 1), 1);
             max(1024 /* free_write_quota */ / estimated_avg_size, 1)
         };
-        option::fill(&mut v.big_vec, big_vector::empty(bucket_size));
+        option::fill(&mut self.big_vec, big_vector::empty(bucket_size));
     };
-    big_vector::push_back(option::borrow_mut(&mut v.big_vec), val);
+    big_vector::push_back(option::borrow_mut(&mut self.big_vec), val);
 }
 
@@ -585,11 +585,11 @@ This operation will cost more gas when it adds new bucket. ## Function `pop_back` -Pop an T from the end of vector v. It does shrink the buckets if they're empty. -Aborts if v is empty. +Pop an T from the end of vector self. It does shrink the buckets if they're empty. +Aborts if self is empty. -
public fun pop_back<T>(v: &mut smart_vector::SmartVector<T>): T
+
public fun pop_back<T>(self: &mut smart_vector::SmartVector<T>): T
 
@@ -598,9 +598,9 @@ Aborts if v is empty. Implementation -
public fun pop_back<T>(v: &mut SmartVector<T>): T {
-    assert!(!is_empty(v), error::invalid_state(EVECTOR_EMPTY));
-    let big_vec_wrapper = &mut v.big_vec;
+
public fun pop_back<T>(self: &mut SmartVector<T>): T {
+    assert!(!is_empty(self), error::invalid_state(EVECTOR_EMPTY));
+    let big_vec_wrapper = &mut self.big_vec;
     if (option::is_some(big_vec_wrapper)) {
         let big_vec = option::extract(big_vec_wrapper);
         let val = big_vector::pop_back(&mut big_vec);
@@ -611,7 +611,7 @@ Aborts if v is empty.
         };
         val
     } else {
-        vector::pop_back(&mut v.inline_vec)
+        vector::pop_back(&mut self.inline_vec)
     }
 }
 
@@ -624,12 +624,12 @@ Aborts if v is empty. ## Function `remove` -Remove the T at index i in the vector v and return the owned value that was previously stored at i in v. +Remove the T at index i in the vector self and return the owned value that was previously stored at i in self. All Ts occurring at indices greater than i will be shifted down by 1. Will abort if i is out of bounds. Disclaimer: This function may be costly. Use it at your own discretion. -
public fun remove<T>(v: &mut smart_vector::SmartVector<T>, i: u64): T
+
public fun remove<T>(self: &mut smart_vector::SmartVector<T>, i: u64): T
 
@@ -638,14 +638,14 @@ Disclaimer: This function may be costly. Use it at your own discretion. Implementation -
public fun remove<T>(v: &mut SmartVector<T>, i: u64): T {
-    let len = length(v);
+
public fun remove<T>(self: &mut SmartVector<T>, i: u64): T {
+    let len = length(self);
     assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let inline_len = vector::length(&v.inline_vec);
+    let inline_len = vector::length(&self.inline_vec);
     if (i < inline_len) {
-        vector::remove(&mut v.inline_vec, i)
+        vector::remove(&mut self.inline_vec, i)
     } else {
-        let big_vec_wrapper = &mut v.big_vec;
+        let big_vec_wrapper = &mut self.big_vec;
         let big_vec = option::extract(big_vec_wrapper);
         let val = big_vector::remove(&mut big_vec, i - inline_len);
         if (big_vector::is_empty(&big_vec)) {
@@ -666,12 +666,12 @@ Disclaimer: This function may be costly. Use it at your own discretion.
 
 ## Function `swap_remove`
 
-Swap the ith T of the vector v with the last T and then pop the vector.
+Swap the ith T of the vector self with the last T and then pop the vector.
 This is O(1), but does not preserve ordering of Ts in the vector.
 Aborts if i is out of bounds.
 
 
-
public fun swap_remove<T>(v: &mut smart_vector::SmartVector<T>, i: u64): T
+
public fun swap_remove<T>(self: &mut smart_vector::SmartVector<T>, i: u64): T
 
@@ -680,12 +680,12 @@ Aborts if i is out of bounds. Implementation -
public fun swap_remove<T>(v: &mut SmartVector<T>, i: u64): T {
-    let len = length(v);
+
public fun swap_remove<T>(self: &mut SmartVector<T>, i: u64): T {
+    let len = length(self);
     assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let inline_len = vector::length(&v.inline_vec);
-    let big_vec_wrapper = &mut v.big_vec;
-    let inline_vec = &mut v.inline_vec;
+    let inline_len = vector::length(&self.inline_vec);
+    let big_vec_wrapper = &mut self.big_vec;
+    let inline_vec = &mut self.inline_vec;
     if (i >= inline_len) {
         let big_vec = option::extract(big_vec_wrapper);
         let val = big_vector::swap_remove(&mut big_vec, i - inline_len);
@@ -720,10 +720,10 @@ Aborts if i is out of bounds.
 ## Function `swap`
 
 Swap the Ts at the i'th and j'th indices in the vector v. Will abort if either of i or j are out of bounds
-for v.
+for self.
 
 
-
public fun swap<T: store>(v: &mut smart_vector::SmartVector<T>, i: u64, j: u64)
+
public fun swap<T: store>(self: &mut smart_vector::SmartVector<T>, i: u64, j: u64)
 
@@ -732,20 +732,20 @@ for v. Implementation -
public fun swap<T: store>(v: &mut SmartVector<T>, i: u64, j: u64) {
+
public fun swap<T: store>(self: &mut SmartVector<T>, i: u64, j: u64) {
     if (i > j) {
-        return swap(v, j, i)
+        return swap(self, j, i)
     };
-    let len = length(v);
+    let len = length(self);
     assert!(j < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS));
-    let inline_len = vector::length(&v.inline_vec);
+    let inline_len = vector::length(&self.inline_vec);
     if (i >= inline_len) {
-        big_vector::swap(option::borrow_mut(&mut v.big_vec), i - inline_len, j - inline_len);
+        big_vector::swap(option::borrow_mut(&mut self.big_vec), i - inline_len, j - inline_len);
     } else if (j < inline_len) {
-        vector::swap(&mut v.inline_vec, i, j);
+        vector::swap(&mut self.inline_vec, i, j);
     } else {
-        let big_vec = option::borrow_mut(&mut v.big_vec);
-        let inline_vec = &mut v.inline_vec;
+        let big_vec = option::borrow_mut(&mut self.big_vec);
+        let inline_vec = &mut self.inline_vec;
         let element_i = vector::swap_remove(inline_vec, i);
         let element_j = big_vector::swap_remove(big_vec, j - inline_len);
         vector::push_back(inline_vec, element_j);
@@ -764,11 +764,11 @@ for v.
 
 ## Function `reverse`
 
-Reverse the order of the Ts in the vector v in-place.
+Reverse the order of the Ts in the vector self in-place.
 Disclaimer: This function may be costly. Use it at your own discretion.
 
 
-
public fun reverse<T: store>(v: &mut smart_vector::SmartVector<T>)
+
public fun reverse<T: store>(self: &mut smart_vector::SmartVector<T>)
 
@@ -777,33 +777,33 @@ Disclaimer: This function may be costly. Use it at your own discretion. Implementation -
public fun reverse<T: store>(v: &mut SmartVector<T>) {
-    let inline_len = vector::length(&v.inline_vec);
+
public fun reverse<T: store>(self: &mut SmartVector<T>) {
+    let inline_len = vector::length(&self.inline_vec);
     let i = 0;
     let new_inline_vec = vector[];
     // Push the last `inline_len` Ts into a temp vector.
     while (i < inline_len) {
-        vector::push_back(&mut new_inline_vec, pop_back(v));
+        vector::push_back(&mut new_inline_vec, pop_back(self));
         i = i + 1;
     };
     vector::reverse(&mut new_inline_vec);
     // Reverse the big_vector left if exists.
-    if (option::is_some(&v.big_vec)) {
-        big_vector::reverse(option::borrow_mut(&mut v.big_vec));
+    if (option::is_some(&self.big_vec)) {
+        big_vector::reverse(option::borrow_mut(&mut self.big_vec));
     };
     // Mem::swap the two vectors.
     let temp_vec = vector[];
-    while (!vector::is_empty(&mut v.inline_vec)) {
-        vector::push_back(&mut temp_vec, vector::pop_back(&mut v.inline_vec));
+    while (!vector::is_empty(&mut self.inline_vec)) {
+        vector::push_back(&mut temp_vec, vector::pop_back(&mut self.inline_vec));
     };
     vector::reverse(&mut temp_vec);
     while (!vector::is_empty(&mut new_inline_vec)) {
-        vector::push_back(&mut v.inline_vec, vector::pop_back(&mut new_inline_vec));
+        vector::push_back(&mut self.inline_vec, vector::pop_back(&mut new_inline_vec));
     };
     vector::destroy_empty(new_inline_vec);
     // Push the rest Ts originally left in inline_vector back to the end of the smart vector.
     while (!vector::is_empty(&mut temp_vec)) {
-        push_back(v, vector::pop_back(&mut temp_vec));
+        push_back(self, vector::pop_back(&mut temp_vec));
     };
     vector::destroy_empty(temp_vec);
 }
@@ -817,12 +817,12 @@ Disclaimer: This function may be costly. Use it at your own discretion.
 
 ## Function `index_of`
 
-Return (true, i) if val is in the vector v at index i.
+Return (true, i) if val is in the vector self at index i.
 Otherwise, returns (false, 0).
 Disclaimer: This function may be costly. Use it at your own discretion.
 
 
-
public fun index_of<T>(v: &smart_vector::SmartVector<T>, val: &T): (bool, u64)
+
public fun index_of<T>(self: &smart_vector::SmartVector<T>, val: &T): (bool, u64)
 
@@ -831,13 +831,13 @@ Disclaimer: This function may be costly. Use it at your own discretion. Implementation -
public fun index_of<T>(v: &SmartVector<T>, val: &T): (bool, u64) {
-    let (found, i) = vector::index_of(&v.inline_vec, val);
+
public fun index_of<T>(self: &SmartVector<T>, val: &T): (bool, u64) {
+    let (found, i) = vector::index_of(&self.inline_vec, val);
     if (found) {
         (true, i)
-    } else if (option::is_some(&v.big_vec)) {
-        let (found, i) = big_vector::index_of(option::borrow(&v.big_vec), val);
-        (found, i + vector::length(&v.inline_vec))
+    } else if (option::is_some(&self.big_vec)) {
+        let (found, i) = big_vector::index_of(option::borrow(&self.big_vec), val);
+        (found, i + vector::length(&self.inline_vec))
     } else {
         (false, 0)
     }
@@ -852,11 +852,11 @@ Disclaimer: This function may be costly. Use it at your own discretion.
 
 ## Function `contains`
 
-Return true if val is in the vector v.
+Return true if val is in the vector self.
 Disclaimer: This function may be costly. Use it at your own discretion.
 
 
-
public fun contains<T>(v: &smart_vector::SmartVector<T>, val: &T): bool
+
public fun contains<T>(self: &smart_vector::SmartVector<T>, val: &T): bool
 
@@ -865,9 +865,9 @@ Disclaimer: This function may be costly. Use it at your own discretion. Implementation -
public fun contains<T>(v: &SmartVector<T>, val: &T): bool {
-    if (is_empty(v)) return false;
-    let (exist, _) = index_of(v, val);
+
public fun contains<T>(self: &SmartVector<T>, val: &T): bool {
+    if (is_empty(self)) return false;
+    let (exist, _) = index_of(self, val);
     exist
 }
 
@@ -883,7 +883,7 @@ Disclaimer: This function may be costly. Use it at your own discretion. Return the length of the vector. -
public fun length<T>(v: &smart_vector::SmartVector<T>): u64
+
public fun length<T>(self: &smart_vector::SmartVector<T>): u64
 
@@ -892,11 +892,11 @@ Return the length of the vector. Implementation -
public fun length<T>(v: &SmartVector<T>): u64 {
-    vector::length(&v.inline_vec) + if (option::is_none(&v.big_vec)) {
+
public fun length<T>(self: &SmartVector<T>): u64 {
+    vector::length(&self.inline_vec) + if (option::is_none(&self.big_vec)) {
         0
     } else {
-        big_vector::length(option::borrow(&v.big_vec))
+        big_vector::length(option::borrow(&self.big_vec))
     }
 }
 
@@ -909,10 +909,10 @@ Return the length of the vector. ## Function `is_empty` -Return true if the vector v has no Ts and false otherwise. +Return true if the vector self has no Ts and false otherwise. -
public fun is_empty<T>(v: &smart_vector::SmartVector<T>): bool
+
public fun is_empty<T>(self: &smart_vector::SmartVector<T>): bool
 
@@ -921,8 +921,8 @@ Return true if the vector v has no Ts and Implementation -
public fun is_empty<T>(v: &SmartVector<T>): bool {
-    length(v) == 0
+
public fun is_empty<T>(self: &SmartVector<T>): bool {
+    length(self) == 0
 }
 
@@ -937,7 +937,7 @@ Return true if the vector v has no Ts and public fun for_each<T: store>(v: smart_vector::SmartVector<T>, f: |T|) +
public fun for_each<T: store>(self: smart_vector::SmartVector<T>, f: |T|)
 
@@ -946,9 +946,9 @@ Apply the function to each T in the vector, consuming it. Implementation -
public inline fun for_each<T: store>(v: SmartVector<T>, f: |T|) {
-    aptos_std::smart_vector::reverse(&mut v); // We need to reverse the vector to consume it efficiently
-    aptos_std::smart_vector::for_each_reverse(v, |e| f(e));
+
public inline fun for_each<T: store>(self: SmartVector<T>, f: |T|) {
+    aptos_std::smart_vector::reverse(&mut self); // We need to reverse the vector to consume it efficiently
+    aptos_std::smart_vector::for_each_reverse(self, |e| f(e));
 }
 
@@ -963,7 +963,7 @@ Apply the function to each T in the vector, consuming it. Apply the function to each T in the vector, consuming it. -
public fun for_each_reverse<T>(v: smart_vector::SmartVector<T>, f: |T|)
+
public fun for_each_reverse<T>(self: smart_vector::SmartVector<T>, f: |T|)
 
@@ -972,13 +972,13 @@ Apply the function to each T in the vector, consuming it. Implementation -
public inline fun for_each_reverse<T>(v: SmartVector<T>, f: |T|) {
-    let len = aptos_std::smart_vector::length(&v);
+
public inline fun for_each_reverse<T>(self: SmartVector<T>, f: |T|) {
+    let len = aptos_std::smart_vector::length(&self);
     while (len > 0) {
-        f(aptos_std::smart_vector::pop_back(&mut v));
+        f(aptos_std::smart_vector::pop_back(&mut self));
         len = len - 1;
     };
-    aptos_std::smart_vector::destroy_empty(v)
+    aptos_std::smart_vector::destroy_empty(self)
 }
 
@@ -993,7 +993,7 @@ Apply the function to each T in the vector, consuming it. Apply the function to a reference of each T in the vector. -
public fun for_each_ref<T>(v: &smart_vector::SmartVector<T>, f: |&T|)
+
public fun for_each_ref<T>(self: &smart_vector::SmartVector<T>, f: |&T|)
 
@@ -1002,11 +1002,11 @@ Apply the function to a reference of each T in the vector. Implementation -
public inline fun for_each_ref<T>(v: &SmartVector<T>, f: |&T|) {
+
public inline fun for_each_ref<T>(self: &SmartVector<T>, f: |&T|) {
     let i = 0;
-    let len = aptos_std::smart_vector::length(v);
+    let len = aptos_std::smart_vector::length(self);
     while (i < len) {
-        f(aptos_std::smart_vector::borrow(v, i));
+        f(aptos_std::smart_vector::borrow(self, i));
         i = i + 1
     }
 }
@@ -1023,7 +1023,7 @@ Apply the function to a reference of each T in the vector.
 Apply the function to a mutable reference to each T in the vector.
 
 
-
public fun for_each_mut<T>(v: &mut smart_vector::SmartVector<T>, f: |&mut T|)
+
public fun for_each_mut<T>(self: &mut smart_vector::SmartVector<T>, f: |&mut T|)
 
@@ -1032,11 +1032,11 @@ Apply the function to a mutable reference to each T in the vector. Implementation -
public inline fun for_each_mut<T>(v: &mut SmartVector<T>, f: |&mut T|) {
+
public inline fun for_each_mut<T>(self: &mut SmartVector<T>, f: |&mut T|) {
     let i = 0;
-    let len = aptos_std::smart_vector::length(v);
+    let len = aptos_std::smart_vector::length(self);
     while (i < len) {
-        f(aptos_std::smart_vector::borrow_mut(v, i));
+        f(aptos_std::smart_vector::borrow_mut(self, i));
         i = i + 1
     }
 }
@@ -1053,7 +1053,7 @@ Apply the function to a mutable reference to each T in the vector.
 Apply the function to a reference of each T in the vector with its index.
 
 
-
public fun enumerate_ref<T>(v: &smart_vector::SmartVector<T>, f: |(u64, &T)|)
+
public fun enumerate_ref<T>(self: &smart_vector::SmartVector<T>, f: |(u64, &T)|)
 
@@ -1062,11 +1062,11 @@ Apply the function to a reference of each T in the vector with its index. Implementation -
public inline fun enumerate_ref<T>(v: &SmartVector<T>, f: |u64, &T|) {
+
public inline fun enumerate_ref<T>(self: &SmartVector<T>, f: |u64, &T|) {
     let i = 0;
-    let len = aptos_std::smart_vector::length(v);
+    let len = aptos_std::smart_vector::length(self);
     while (i < len) {
-        f(i, aptos_std::smart_vector::borrow(v, i));
+        f(i, aptos_std::smart_vector::borrow(self, i));
         i = i + 1;
     };
 }
@@ -1083,7 +1083,7 @@ Apply the function to a reference of each T in the vector with its index.
 Apply the function to a mutable reference of each T in the vector with its index.
 
 
-
public fun enumerate_mut<T>(v: &mut smart_vector::SmartVector<T>, f: |(u64, &mut T)|)
+
public fun enumerate_mut<T>(self: &mut smart_vector::SmartVector<T>, f: |(u64, &mut T)|)
 
@@ -1092,11 +1092,11 @@ Apply the function to a mutable reference of each T in the vector with its index Implementation -
public inline fun enumerate_mut<T>(v: &mut SmartVector<T>, f: |u64, &mut T|) {
+
public inline fun enumerate_mut<T>(self: &mut SmartVector<T>, f: |u64, &mut T|) {
     let i = 0;
-    let len = length(v);
+    let len = length(self);
     while (i < len) {
-        f(i, borrow_mut(v, i));
+        f(i, borrow_mut(self, i));
         i = i + 1;
     };
 }
@@ -1114,7 +1114,7 @@ Fold the function over the Ts. For example, fold<Accumulator, T: store>(v: smart_vector::SmartVector<T>, init: Accumulator, f: |(Accumulator, T)|Accumulator): Accumulator
+
public fun fold<Accumulator, T: store>(self: smart_vector::SmartVector<T>, init: Accumulator, f: |(Accumulator, T)|Accumulator): Accumulator
 
@@ -1124,12 +1124,12 @@ Fold the function over the Ts. For example, fold<Accumulator, T: store>( - v: SmartVector<T>, + self: SmartVector<T>, init: Accumulator, f: |Accumulator, T|Accumulator ): Accumulator { let accu = init; - aptos_std::smart_vector::for_each(v, |elem| accu = f(accu, elem)); + aptos_std::smart_vector::for_each(self, |elem| accu = f(accu, elem)); accu }
@@ -1146,7 +1146,7 @@ Fold right like fold above but working right to left. For example, f(1, f(2, f(3, 0))) -
public fun foldr<Accumulator, T>(v: smart_vector::SmartVector<T>, init: Accumulator, f: |(T, Accumulator)|Accumulator): Accumulator
+
public fun foldr<Accumulator, T>(self: smart_vector::SmartVector<T>, init: Accumulator, f: |(T, Accumulator)|Accumulator): Accumulator
 
@@ -1156,12 +1156,12 @@ Fold right like fold above but working right to left. For example, public inline fun foldr<Accumulator, T>( - v: SmartVector<T>, + self: SmartVector<T>, init: Accumulator, f: |T, Accumulator|Accumulator ): Accumulator { let accu = init; - aptos_std::smart_vector::for_each_reverse(v, |elem| accu = f(elem, accu)); + aptos_std::smart_vector::for_each_reverse(self, |elem| accu = f(elem, accu)); accu }
@@ -1178,7 +1178,7 @@ Map the function over the references of the Ts of the vector, producing a new ve original vector. -
public fun map_ref<T1, T2: store>(v: &smart_vector::SmartVector<T1>, f: |&T1|T2): smart_vector::SmartVector<T2>
+
public fun map_ref<T1, T2: store>(self: &smart_vector::SmartVector<T1>, f: |&T1|T2): smart_vector::SmartVector<T2>
 
@@ -1188,11 +1188,11 @@ original vector.
public inline fun map_ref<T1, T2: store>(
-    v: &SmartVector<T1>,
+    self: &SmartVector<T1>,
     f: |&T1|T2
 ): SmartVector<T2> {
     let result = aptos_std::smart_vector::new<T2>();
-    aptos_std::smart_vector::for_each_ref(v, |elem| aptos_std::smart_vector::push_back(&mut result, f(elem)));
+    aptos_std::smart_vector::for_each_ref(self, |elem| aptos_std::smart_vector::push_back(&mut result, f(elem)));
     result
 }
 
@@ -1208,7 +1208,7 @@ original vector. Map the function over the Ts of the vector, producing a new vector. -
public fun map<T1: store, T2: store>(v: smart_vector::SmartVector<T1>, f: |T1|T2): smart_vector::SmartVector<T2>
+
public fun map<T1: store, T2: store>(self: smart_vector::SmartVector<T1>, f: |T1|T2): smart_vector::SmartVector<T2>
 
@@ -1218,11 +1218,11 @@ Map the function over the Ts of the vector, producing a new vector.
public inline fun map<T1: store, T2: store>(
-    v: SmartVector<T1>,
+    self: SmartVector<T1>,
     f: |T1|T2
 ): SmartVector<T2> {
     let result = aptos_std::smart_vector::new<T2>();
-    aptos_std::smart_vector::for_each(v, |elem| push_back(&mut result, f(elem)));
+    aptos_std::smart_vector::for_each(self, |elem| push_back(&mut result, f(elem)));
     result
 }
 
@@ -1238,7 +1238,7 @@ Map the function over the Ts of the vector, producing a new vector. Filter the vector using the boolean function, removing all Ts for which p(e) is not true. -
public fun filter<T: drop, store>(v: smart_vector::SmartVector<T>, p: |&T|bool): smart_vector::SmartVector<T>
+
public fun filter<T: drop, store>(self: smart_vector::SmartVector<T>, p: |&T|bool): smart_vector::SmartVector<T>
 
@@ -1248,11 +1248,11 @@ Filter the vector using the boolean function, removing all Ts for which p(
public inline fun filter<T: store + drop>(
-    v: SmartVector<T>,
+    self: SmartVector<T>,
     p: |&T|bool
 ): SmartVector<T> {
     let result = aptos_std::smart_vector::new<T>();
-    aptos_std::smart_vector::for_each(v, |elem| {
+    aptos_std::smart_vector::for_each(self, |elem| {
         if (p(&elem)) aptos_std::smart_vector::push_back(&mut result, elem);
     });
     result
@@ -1269,7 +1269,7 @@ Filter the vector using the boolean function, removing all Ts for which p(
 
 
 
-
public fun zip<T1: store, T2: store>(v1: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|)
+
public fun zip<T1: store, T2: store>(self: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|)
 
@@ -1278,11 +1278,11 @@ Filter the vector using the boolean function, removing all Ts for which p( Implementation -
public inline fun zip<T1: store, T2: store>(v1: SmartVector<T1>, v2: SmartVector<T2>, f: |T1, T2|) {
+
public inline fun zip<T1: store, T2: store>(self: SmartVector<T1>, v2: SmartVector<T2>, f: |T1, T2|) {
     // We need to reverse the vectors to consume it efficiently
-    aptos_std::smart_vector::reverse(&mut v1);
+    aptos_std::smart_vector::reverse(&mut self);
     aptos_std::smart_vector::reverse(&mut v2);
-    aptos_std::smart_vector::zip_reverse(v1, v2, |e1, e2| f(e1, e2));
+    aptos_std::smart_vector::zip_reverse(self, v2, |e1, e2| f(e1, e2));
 }
 
@@ -1298,7 +1298,7 @@ Apply the function to each pair of elements in the two given vectors in the reve This errors out if the vectors are not of the same length. -
public fun zip_reverse<T1, T2>(v1: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|)
+
public fun zip_reverse<T1, T2>(self: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|)
 
@@ -1308,19 +1308,19 @@ This errors out if the vectors are not of the same length.
public inline fun zip_reverse<T1, T2>(
-    v1: SmartVector<T1>,
+    self: SmartVector<T1>,
     v2: SmartVector<T2>,
     f: |T1, T2|,
 ) {
-    let len = aptos_std::smart_vector::length(&v1);
+    let len = aptos_std::smart_vector::length(&self);
     // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it
     // due to how inline functions work.
     assert!(len == aptos_std::smart_vector::length(&v2), 0x20005);
     while (len > 0) {
-        f(aptos_std::smart_vector::pop_back(&mut v1), aptos_std::smart_vector::pop_back(&mut v2));
+        f(aptos_std::smart_vector::pop_back(&mut self), aptos_std::smart_vector::pop_back(&mut v2));
         len = len - 1;
     };
-    aptos_std::smart_vector::destroy_empty(v1);
+    aptos_std::smart_vector::destroy_empty(self);
     aptos_std::smart_vector::destroy_empty(v2);
 }
 
@@ -1337,7 +1337,7 @@ Apply the function to the references of each pair of elements in the two given v This errors out if the vectors are not of the same length. -
public fun zip_ref<T1, T2>(v1: &smart_vector::SmartVector<T1>, v2: &smart_vector::SmartVector<T2>, f: |(&T1, &T2)|)
+
public fun zip_ref<T1, T2>(self: &smart_vector::SmartVector<T1>, v2: &smart_vector::SmartVector<T2>, f: |(&T1, &T2)|)
 
@@ -1347,17 +1347,17 @@ This errors out if the vectors are not of the same length.
public inline fun zip_ref<T1, T2>(
-    v1: &SmartVector<T1>,
+    self: &SmartVector<T1>,
     v2: &SmartVector<T2>,
     f: |&T1, &T2|,
 ) {
-    let len = aptos_std::smart_vector::length(v1);
+    let len = aptos_std::smart_vector::length(self);
     // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it
     // due to how inline functions work.
     assert!(len == aptos_std::smart_vector::length(v2), 0x20005);
     let i = 0;
     while (i < len) {
-        f(aptos_std::smart_vector::borrow(v1, i), aptos_std::smart_vector::borrow(v2, i));
+        f(aptos_std::smart_vector::borrow(self, i), aptos_std::smart_vector::borrow(v2, i));
         i = i + 1
     }
 }
@@ -1375,7 +1375,7 @@ Apply the function to mutable references to each pair of elements in the two giv
 This errors out if the vectors are not of the same length.
 
 
-
public fun zip_mut<T1, T2>(v1: &mut smart_vector::SmartVector<T1>, v2: &mut smart_vector::SmartVector<T2>, f: |(&mut T1, &mut T2)|)
+
public fun zip_mut<T1, T2>(self: &mut smart_vector::SmartVector<T1>, v2: &mut smart_vector::SmartVector<T2>, f: |(&mut T1, &mut T2)|)
 
@@ -1385,17 +1385,17 @@ This errors out if the vectors are not of the same length.
public inline fun zip_mut<T1, T2>(
-    v1: &mut SmartVector<T1>,
+    self: &mut SmartVector<T1>,
     v2: &mut SmartVector<T2>,
     f: |&mut T1, &mut T2|,
 ) {
     let i = 0;
-    let len = aptos_std::smart_vector::length(v1);
+    let len = aptos_std::smart_vector::length(self);
     // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it
     // due to how inline functions work.
     assert!(len == aptos_std::smart_vector::length(v2), 0x20005);
     while (i < len) {
-        f(aptos_std::smart_vector::borrow_mut(v1, i), aptos_std::smart_vector::borrow_mut(v2, i));
+        f(aptos_std::smart_vector::borrow_mut(self, i), aptos_std::smart_vector::borrow_mut(v2, i));
         i = i + 1
     }
 }
@@ -1412,7 +1412,7 @@ This errors out if the vectors are not of the same length.
 Map the function over the element pairs of the two vectors, producing a new vector.
 
 
-
public fun zip_map<T1: store, T2: store, NewT: store>(v1: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|NewT): smart_vector::SmartVector<NewT>
+
public fun zip_map<T1: store, T2: store, NewT: store>(self: smart_vector::SmartVector<T1>, v2: smart_vector::SmartVector<T2>, f: |(T1, T2)|NewT): smart_vector::SmartVector<NewT>
 
@@ -1422,16 +1422,16 @@ Map the function over the element pairs of the two vectors, producing a new vect
public inline fun zip_map<T1: store, T2: store, NewT: store>(
-    v1: SmartVector<T1>,
+    self: SmartVector<T1>,
     v2: SmartVector<T2>,
     f: |T1, T2|NewT
 ): SmartVector<NewT> {
     // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it
     // due to how inline functions work.
-    assert!(aptos_std::smart_vector::length(&v1) == aptos_std::smart_vector::length(&v2), 0x20005);
+    assert!(aptos_std::smart_vector::length(&self) == aptos_std::smart_vector::length(&v2), 0x20005);
 
     let result = aptos_std::smart_vector::new<NewT>();
-    aptos_std::smart_vector::zip(v1, v2, |e1, e2| push_back(&mut result, f(e1, e2)));
+    aptos_std::smart_vector::zip(self, v2, |e1, e2| push_back(&mut result, f(e1, e2)));
     result
 }
 
@@ -1448,7 +1448,7 @@ Map the function over the references of the element pairs of two vectors, produc values without modifying the original vectors. -
public fun zip_map_ref<T1, T2, NewT: store>(v1: &smart_vector::SmartVector<T1>, v2: &smart_vector::SmartVector<T2>, f: |(&T1, &T2)|NewT): smart_vector::SmartVector<NewT>
+
public fun zip_map_ref<T1, T2, NewT: store>(self: &smart_vector::SmartVector<T1>, v2: &smart_vector::SmartVector<T2>, f: |(&T1, &T2)|NewT): smart_vector::SmartVector<NewT>
 
@@ -1458,16 +1458,16 @@ values without modifying the original vectors.
public inline fun zip_map_ref<T1, T2, NewT: store>(
-    v1: &SmartVector<T1>,
+    self: &SmartVector<T1>,
     v2: &SmartVector<T2>,
     f: |&T1, &T2|NewT
 ): SmartVector<NewT> {
     // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it
     // due to how inline functions work.
-    assert!(aptos_std::smart_vector::length(v1) == aptos_std::smart_vector::length(v2), 0x20005);
+    assert!(aptos_std::smart_vector::length(self) == aptos_std::smart_vector::length(v2), 0x20005);
 
     let result = aptos_std::smart_vector::new<NewT>();
-    aptos_std::smart_vector::zip_ref(v1, v2, |e1, e2| push_back(&mut result, f(e1, e2)));
+    aptos_std::smart_vector::zip_ref(self, v2, |e1, e2| push_back(&mut result, f(e1, e2)));
     result
 }
 
@@ -1568,15 +1568,15 @@ values without modifying the original vectors. ### Function `destroy_empty` -
public fun destroy_empty<T>(v: smart_vector::SmartVector<T>)
+
public fun destroy_empty<T>(self: smart_vector::SmartVector<T>)
 
-
aborts_if !(is_empty(v));
-aborts_if len(v.inline_vec) != 0
-    || option::is_some(v.big_vec);
+
aborts_if !(is_empty(self));
+aborts_if len(self.inline_vec) != 0
+    || option::is_some(self.big_vec);
 
@@ -1586,15 +1586,15 @@ values without modifying the original vectors. ### Function `borrow` -
public fun borrow<T>(v: &smart_vector::SmartVector<T>, i: u64): &T
+
public fun borrow<T>(self: &smart_vector::SmartVector<T>, i: u64): &T
 
-
aborts_if i >= length(v);
-aborts_if option::is_some(v.big_vec) && (
-    (len(v.inline_vec) + big_vector::length<T>(option::borrow(v.big_vec))) > MAX_U64
+
aborts_if i >= length(self);
+aborts_if option::is_some(self.big_vec) && (
+    (len(self.inline_vec) + big_vector::length<T>(option::borrow(self.big_vec))) > MAX_U64
 );
 
@@ -1605,7 +1605,7 @@ values without modifying the original vectors. ### Function `append` -
public fun append<T: store>(lhs: &mut smart_vector::SmartVector<T>, other: smart_vector::SmartVector<T>)
+
public fun append<T: store>(self: &mut smart_vector::SmartVector<T>, other: smart_vector::SmartVector<T>)
 
@@ -1621,7 +1621,7 @@ values without modifying the original vectors. ### Function `push_back` -
public fun push_back<T: store>(v: &mut smart_vector::SmartVector<T>, val: T)
+
public fun push_back<T: store>(self: &mut smart_vector::SmartVector<T>, val: T)
 
@@ -1637,21 +1637,21 @@ values without modifying the original vectors. ### Function `pop_back` -
public fun pop_back<T>(v: &mut smart_vector::SmartVector<T>): T
+
public fun pop_back<T>(self: &mut smart_vector::SmartVector<T>): T
 
pragma verify_duration_estimate = 120;
-aborts_if  option::is_some(v.big_vec)
+aborts_if  option::is_some(self.big_vec)
     &&
-    (table_with_length::spec_len(option::borrow(v.big_vec).buckets) == 0);
-aborts_if is_empty(v);
-aborts_if option::is_some(v.big_vec) && (
-    (len(v.inline_vec) + big_vector::length<T>(option::borrow(v.big_vec))) > MAX_U64
+    (table_with_length::spec_len(option::borrow(self.big_vec).buckets) == 0);
+aborts_if is_empty(self);
+aborts_if option::is_some(self.big_vec) && (
+    (len(self.inline_vec) + big_vector::length<T>(option::borrow(self.big_vec))) > MAX_U64
 );
-ensures length(v) == length(old(v)) - 1;
+ensures length(self) == length(old(self)) - 1;
 
@@ -1661,7 +1661,7 @@ values without modifying the original vectors. ### Function `remove` -
public fun remove<T>(v: &mut smart_vector::SmartVector<T>, i: u64): T
+
public fun remove<T>(self: &mut smart_vector::SmartVector<T>, i: u64): T
 
@@ -1677,18 +1677,18 @@ values without modifying the original vectors. ### Function `swap_remove` -
public fun swap_remove<T>(v: &mut smart_vector::SmartVector<T>, i: u64): T
+
public fun swap_remove<T>(self: &mut smart_vector::SmartVector<T>, i: u64): T
 
pragma verify = false;
-aborts_if i >= length(v);
-aborts_if option::is_some(v.big_vec) && (
-    (len(v.inline_vec) + big_vector::length<T>(option::borrow(v.big_vec))) > MAX_U64
+aborts_if i >= length(self);
+aborts_if option::is_some(self.big_vec) && (
+    (len(self.inline_vec) + big_vector::length<T>(option::borrow(self.big_vec))) > MAX_U64
 );
-ensures length(v) == length(old(v)) - 1;
+ensures length(self) == length(old(self)) - 1;
 
@@ -1698,7 +1698,7 @@ values without modifying the original vectors. ### Function `swap` -
public fun swap<T: store>(v: &mut smart_vector::SmartVector<T>, i: u64, j: u64)
+
public fun swap<T: store>(self: &mut smart_vector::SmartVector<T>, i: u64, j: u64)
 
@@ -1714,13 +1714,14 @@ values without modifying the original vectors. ### Function `length` -
public fun length<T>(v: &smart_vector::SmartVector<T>): u64
+
public fun length<T>(self: &smart_vector::SmartVector<T>): u64
 
-
aborts_if option::is_some(v.big_vec) && len(v.inline_vec) + big_vector::length(option::spec_borrow(v.big_vec)) > MAX_U64;
+
aborts_if option::is_some(self.big_vec) && len(self.inline_vec) + big_vector::length(option::spec_borrow(
+    self.big_vec)) > MAX_U64;
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/table.md b/aptos-move/framework/aptos-stdlib/doc/table.md index d05ea82adb590..44f2e5627b7d3 100644 --- a/aptos-move/framework/aptos-stdlib/doc/table.md +++ b/aptos-move/framework/aptos-stdlib/doc/table.md @@ -140,7 +140,7 @@ key already exists. The entry itself is not stored in the table, and cannot be discovered from it. -
public fun add<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K, val: V)
+
public fun add<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K, val: V)
 
@@ -149,8 +149,8 @@ table, and cannot be discovered from it. Implementation -
public fun add<K: copy + drop, V>(table: &mut Table<K, V>, key: K, val: V) {
-    add_box<K, V, Box<V>>(table, key, Box { val })
+
public fun add<K: copy + drop, V>(self: &mut Table<K, V>, key: K, val: V) {
+    add_box<K, V, Box<V>>(self, key, Box { val })
 }
 
@@ -166,7 +166,7 @@ Acquire an immutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow<K: copy, drop, V>(table: &table::Table<K, V>, key: K): &V
+
public fun borrow<K: copy, drop, V>(self: &table::Table<K, V>, key: K): &V
 
@@ -175,8 +175,8 @@ Aborts if there is no entry for key. Implementation -
public fun borrow<K: copy + drop, V>(table: &Table<K, V>, key: K): &V {
-    &borrow_box<K, V, Box<V>>(table, key).val
+
public fun borrow<K: copy + drop, V>(self: &Table<K, V>, key: K): &V {
+    &borrow_box<K, V, Box<V>>(self, key).val
 }
 
@@ -192,7 +192,7 @@ Acquire an immutable reference to the value which key maps to. Returns specified default value if there is no entry for key. -
public fun borrow_with_default<K: copy, drop, V>(table: &table::Table<K, V>, key: K, default: &V): &V
+
public fun borrow_with_default<K: copy, drop, V>(self: &table::Table<K, V>, key: K, default: &V): &V
 
@@ -201,11 +201,11 @@ Returns specified default value if there is no entry for key. Implementation -
public fun borrow_with_default<K: copy + drop, V>(table: &Table<K, V>, key: K, default: &V): &V {
-    if (!contains(table, copy key)) {
+
public fun borrow_with_default<K: copy + drop, V>(self: &Table<K, V>, key: K, default: &V): &V {
+    if (!contains(self, copy key)) {
         default
     } else {
-        borrow(table, copy key)
+        borrow(self, copy key)
     }
 }
 
@@ -222,7 +222,7 @@ Acquire a mutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow_mut<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K): &mut V
+
public fun borrow_mut<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K): &mut V
 
@@ -231,8 +231,8 @@ Aborts if there is no entry for key. Implementation -
public fun borrow_mut<K: copy + drop, V>(table: &mut Table<K, V>, key: K): &mut V {
-    &mut borrow_box_mut<K, V, Box<V>>(table, key).val
+
public fun borrow_mut<K: copy + drop, V>(self: &mut Table<K, V>, key: K): &mut V {
+    &mut borrow_box_mut<K, V, Box<V>>(self, key).val
 }
 
@@ -248,7 +248,7 @@ Acquire a mutable reference to the value which key maps to. Insert the pair (key, default) first if there is no entry for key. -
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut table::Table<K, V>, key: K, default: V): &mut V
+
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut table::Table<K, V>, key: K, default: V): &mut V
 
@@ -257,11 +257,11 @@ Insert the pair (key, default) first if there is no en Implementation -
public fun borrow_mut_with_default<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, default: V): &mut V {
-    if (!contains(table, copy key)) {
-        add(table, copy key, default)
+
public fun borrow_mut_with_default<K: copy + drop, V: drop>(self: &mut Table<K, V>, key: K, default: V): &mut V {
+    if (!contains(self, copy key)) {
+        add(self, copy key, default)
     };
-    borrow_mut(table, key)
+    borrow_mut(self, key)
 }
 
@@ -277,7 +277,7 @@ Insert the pair (key, value) if there is no entry for update the value of the entry for key to value otherwise -
public fun upsert<K: copy, drop, V: drop>(table: &mut table::Table<K, V>, key: K, value: V)
+
public fun upsert<K: copy, drop, V: drop>(self: &mut table::Table<K, V>, key: K, value: V)
 
@@ -286,11 +286,11 @@ update the value of the entry for key to value otherwi Implementation -
public fun upsert<K: copy + drop, V: drop>(table: &mut Table<K, V>, key: K, value: V) {
-    if (!contains(table, copy key)) {
-        add(table, copy key, value)
+
public fun upsert<K: copy + drop, V: drop>(self: &mut Table<K, V>, key: K, value: V) {
+    if (!contains(self, copy key)) {
+        add(self, copy key, value)
     } else {
-        let ref = borrow_mut(table, key);
+        let ref = borrow_mut(self, key);
         *ref = value;
     };
 }
@@ -304,11 +304,11 @@ update the value of the entry for key to value otherwi
 
 ## Function `remove`
 
-Remove from table and return the value which key maps to.
+Remove from self and return the value which key maps to.
 Aborts if there is no entry for key.
 
 
-
public fun remove<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K): V
+
public fun remove<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K): V
 
@@ -317,8 +317,8 @@ Aborts if there is no entry for key. Implementation -
public fun remove<K: copy + drop, V>(table: &mut Table<K, V>, key: K): V {
-    let Box { val } = remove_box<K, V, Box<V>>(table, key);
+
public fun remove<K: copy + drop, V>(self: &mut Table<K, V>, key: K): V {
+    let Box { val } = remove_box<K, V, Box<V>>(self, key);
     val
 }
 
@@ -331,10 +331,10 @@ Aborts if there is no entry for key. ## Function `contains` -Returns true iff table contains an entry for key. +Returns true iff self contains an entry for key. -
public fun contains<K: copy, drop, V>(table: &table::Table<K, V>, key: K): bool
+
public fun contains<K: copy, drop, V>(self: &table::Table<K, V>, key: K): bool
 
@@ -343,8 +343,8 @@ Returns true iff table contains an Implementation -
public fun contains<K: copy + drop, V>(table: &Table<K, V>, key: K): bool {
-    contains_box<K, V, Box<V>>(table, key)
+
public fun contains<K: copy + drop, V>(self: &Table<K, V>, key: K): bool {
+    contains_box<K, V, Box<V>>(self, key)
 }
 
@@ -358,7 +358,7 @@ Returns true iff table contains an -
public(friend) fun destroy<K: copy, drop, V>(table: table::Table<K, V>)
+
public(friend) fun destroy<K: copy, drop, V>(self: table::Table<K, V>)
 
@@ -367,9 +367,9 @@ Returns true iff table contains an Implementation -
public(friend) fun destroy<K: copy + drop, V>(table: Table<K, V>) {
-    destroy_empty_box<K, V, Box<V>>(&table);
-    drop_unchecked_box<K, V, Box<V>>(table)
+
public(friend) fun destroy<K: copy + drop, V>(self: Table<K, V>) {
+    destroy_empty_box<K, V, Box<V>>(&self);
+    drop_unchecked_box<K, V, Box<V>>(self)
 }
 
@@ -618,7 +618,7 @@ Returns true iff table contains an ### Function `add` -
public fun add<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K, val: V)
+
public fun add<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K, val: V)
 
@@ -634,7 +634,7 @@ Returns true iff table contains an ### Function `borrow` -
public fun borrow<K: copy, drop, V>(table: &table::Table<K, V>, key: K): &V
+
public fun borrow<K: copy, drop, V>(self: &table::Table<K, V>, key: K): &V
 
@@ -650,7 +650,7 @@ Returns true iff table contains an ### Function `borrow_mut` -
public fun borrow_mut<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K): &mut V
+
public fun borrow_mut<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K): &mut V
 
@@ -666,7 +666,7 @@ Returns true iff table contains an ### Function `borrow_mut_with_default` -
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut table::Table<K, V>, key: K, default: V): &mut V
+
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut table::Table<K, V>, key: K, default: V): &mut V
 
@@ -682,7 +682,7 @@ Returns true iff table contains an ### Function `upsert` -
public fun upsert<K: copy, drop, V: drop>(table: &mut table::Table<K, V>, key: K, value: V)
+
public fun upsert<K: copy, drop, V: drop>(self: &mut table::Table<K, V>, key: K, value: V)
 
@@ -698,7 +698,7 @@ Returns true iff table contains an ### Function `remove` -
public fun remove<K: copy, drop, V>(table: &mut table::Table<K, V>, key: K): V
+
public fun remove<K: copy, drop, V>(self: &mut table::Table<K, V>, key: K): V
 
@@ -714,7 +714,7 @@ Returns true iff table contains an ### Function `contains` -
public fun contains<K: copy, drop, V>(table: &table::Table<K, V>, key: K): bool
+
public fun contains<K: copy, drop, V>(self: &table::Table<K, V>, key: K): bool
 
@@ -766,7 +766,7 @@ Returns true iff table contains an ### Function `destroy` -
public(friend) fun destroy<K: copy, drop, V>(table: table::Table<K, V>)
+
public(friend) fun destroy<K: copy, drop, V>(self: table::Table<K, V>)
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/table_with_length.md b/aptos-move/framework/aptos-stdlib/doc/table_with_length.md index 1701578615e97..9bf3b27091eba 100644 --- a/aptos-move/framework/aptos-stdlib/doc/table_with_length.md +++ b/aptos-move/framework/aptos-stdlib/doc/table_with_length.md @@ -141,7 +141,7 @@ Create a new Table. Destroy a table. The table must be empty to succeed. -
public fun destroy_empty<K: copy, drop, V>(table: table_with_length::TableWithLength<K, V>)
+
public fun destroy_empty<K: copy, drop, V>(self: table_with_length::TableWithLength<K, V>)
 
@@ -150,9 +150,9 @@ Destroy a table. The table must be empty to succeed. Implementation -
public fun destroy_empty<K: copy + drop, V>(table: TableWithLength<K, V>) {
-    assert!(table.length == 0, error::invalid_state(ENOT_EMPTY));
-    let TableWithLength { inner, length: _ } = table;
+
public fun destroy_empty<K: copy + drop, V>(self: TableWithLength<K, V>) {
+    assert!(self.length == 0, error::invalid_state(ENOT_EMPTY));
+    let TableWithLength { inner, length: _ } = self;
     table::destroy(inner)
 }
 
@@ -170,7 +170,7 @@ key already exists. The entry itself is not stored in the table, and cannot be discovered from it. -
public fun add<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
+
public fun add<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
 
@@ -179,9 +179,9 @@ table, and cannot be discovered from it. Implementation -
public fun add<K: copy + drop, V>(table: &mut TableWithLength<K, V>, key: K, val: V) {
-    table::add(&mut table.inner, key, val);
-    table.length = table.length + 1;
+
public fun add<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K, val: V) {
+    table::add(&mut self.inner, key, val);
+    self.length = self.length + 1;
 }
 
@@ -197,7 +197,7 @@ Acquire an immutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>, key: K): &V
+
public fun borrow<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): &V
 
@@ -206,8 +206,8 @@ Aborts if there is no entry for key. Implementation -
public fun borrow<K: copy + drop, V>(table: &TableWithLength<K, V>, key: K): &V {
-    table::borrow(&table.inner, key)
+
public fun borrow<K: copy + drop, V>(self: &TableWithLength<K, V>, key: K): &V {
+    table::borrow(&self.inner, key)
 }
 
@@ -223,7 +223,7 @@ Acquire a mutable reference to the value which key maps to. Aborts if there is no entry for key. -
public fun borrow_mut<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
+
public fun borrow_mut<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
 
@@ -232,8 +232,8 @@ Aborts if there is no entry for key. Implementation -
public fun borrow_mut<K: copy + drop, V>(table: &mut TableWithLength<K, V>, key: K): &mut V {
-    table::borrow_mut(&mut table.inner, key)
+
public fun borrow_mut<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K): &mut V {
+    table::borrow_mut(&mut self.inner, key)
 }
 
@@ -248,7 +248,7 @@ Aborts if there is no entry for key. Returns the length of the table, i.e. the number of entries. -
public fun length<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>): u64
+
public fun length<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): u64
 
@@ -257,8 +257,8 @@ Returns the length of the table, i.e. the number of entries. Implementation -
public fun length<K: copy + drop, V>(table: &TableWithLength<K, V>): u64 {
-    table.length
+
public fun length<K: copy + drop, V>(self: &TableWithLength<K, V>): u64 {
+    self.length
 }
 
@@ -273,7 +273,7 @@ Returns the length of the table, i.e. the number of entries. Returns true if this table is empty. -
public fun empty<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>): bool
+
public fun empty<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): bool
 
@@ -282,8 +282,8 @@ Returns true if this table is empty. Implementation -
public fun empty<K: copy + drop, V>(table: &TableWithLength<K, V>): bool {
-    table.length == 0
+
public fun empty<K: copy + drop, V>(self: &TableWithLength<K, V>): bool {
+    self.length == 0
 }
 
@@ -299,7 +299,7 @@ Acquire a mutable reference to the value which key maps to. Insert the pair (key, default) first if there is no entry for key. -
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
+
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
 
@@ -308,13 +308,13 @@ Insert the pair (key, default) first if there is no en Implementation -
public fun borrow_mut_with_default<K: copy + drop, V: drop>(table: &mut TableWithLength<K, V>, key: K, default: V): &mut V {
-    if (table::contains(&table.inner, key)) {
-        table::borrow_mut(&mut table.inner, key)
+
public fun borrow_mut_with_default<K: copy + drop, V: drop>(self: &mut TableWithLength<K, V>, key: K, default: V): &mut V {
+    if (table::contains(&self.inner, key)) {
+        table::borrow_mut(&mut self.inner, key)
     } else {
-        table::add(&mut table.inner, key, default);
-        table.length = table.length + 1;
-        table::borrow_mut(&mut table.inner, key)
+        table::add(&mut self.inner, key, default);
+        self.length = self.length + 1;
+        table::borrow_mut(&mut self.inner, key)
     }
 }
 
@@ -331,7 +331,7 @@ Insert the pair (key, value) if there is no entry for update the value of the entry for key to value otherwise -
public fun upsert<K: copy, drop, V: drop>(table: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
+
public fun upsert<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
 
@@ -340,11 +340,11 @@ update the value of the entry for key to value otherwi Implementation -
public fun upsert<K: copy + drop, V: drop>(table: &mut TableWithLength<K, V>, key: K, value: V) {
-    if (!table::contains(&table.inner, key)) {
-        add(table, copy key, value)
+
public fun upsert<K: copy + drop, V: drop>(self: &mut TableWithLength<K, V>, key: K, value: V) {
+    if (!table::contains(&self.inner, key)) {
+        add(self, copy key, value)
     } else {
-        let ref = table::borrow_mut(&mut table.inner, key);
+        let ref = table::borrow_mut(&mut self.inner, key);
         *ref = value;
     };
 }
@@ -362,7 +362,7 @@ Remove from table and return the v
 Aborts if there is no entry for key.
 
 
-
public fun remove<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K): V
+
public fun remove<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): V
 
@@ -371,9 +371,9 @@ Aborts if there is no entry for key. Implementation -
public fun remove<K: copy + drop, V>(table: &mut TableWithLength<K, V>, key: K): V {
-    let val = table::remove(&mut table.inner, key);
-    table.length = table.length - 1;
+
public fun remove<K: copy + drop, V>(self: &mut TableWithLength<K, V>, key: K): V {
+    let val = table::remove(&mut self.inner, key);
+    self.length = self.length - 1;
     val
 }
 
@@ -389,7 +389,7 @@ Aborts if there is no entry for key. Returns true iff table contains an entry for key. -
public fun contains<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>, key: K): bool
+
public fun contains<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): bool
 
@@ -398,8 +398,8 @@ Returns true iff table contains an Implementation -
public fun contains<K: copy + drop, V>(table: &TableWithLength<K, V>, key: K): bool {
-    table::contains(&table.inner, key)
+
public fun contains<K: copy + drop, V>(self: &TableWithLength<K, V>, key: K): bool {
+    table::contains(&self.inner, key)
 }
 
@@ -481,7 +481,7 @@ Returns true iff table contains an ### Function `destroy_empty` -
public fun destroy_empty<K: copy, drop, V>(table: table_with_length::TableWithLength<K, V>)
+
public fun destroy_empty<K: copy, drop, V>(self: table_with_length::TableWithLength<K, V>)
 
@@ -497,7 +497,7 @@ Returns true iff table contains an ### Function `add` -
public fun add<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
+
public fun add<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K, val: V)
 
@@ -513,7 +513,7 @@ Returns true iff table contains an ### Function `borrow` -
public fun borrow<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>, key: K): &V
+
public fun borrow<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): &V
 
@@ -529,7 +529,7 @@ Returns true iff table contains an ### Function `borrow_mut` -
public fun borrow_mut<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
+
public fun borrow_mut<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): &mut V
 
@@ -545,7 +545,7 @@ Returns true iff table contains an ### Function `length` -
public fun length<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>): u64
+
public fun length<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): u64
 
@@ -561,7 +561,7 @@ Returns true iff table contains an ### Function `empty` -
public fun empty<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>): bool
+
public fun empty<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>): bool
 
@@ -577,7 +577,7 @@ Returns true iff table contains an ### Function `borrow_mut_with_default` -
public fun borrow_mut_with_default<K: copy, drop, V: drop>(table: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
+
public fun borrow_mut_with_default<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, default: V): &mut V
 
@@ -594,7 +594,7 @@ Returns true iff table contains an ### Function `upsert` -
public fun upsert<K: copy, drop, V: drop>(table: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
+
public fun upsert<K: copy, drop, V: drop>(self: &mut table_with_length::TableWithLength<K, V>, key: K, value: V)
 
@@ -610,7 +610,7 @@ Returns true iff table contains an ### Function `remove` -
public fun remove<K: copy, drop, V>(table: &mut table_with_length::TableWithLength<K, V>, key: K): V
+
public fun remove<K: copy, drop, V>(self: &mut table_with_length::TableWithLength<K, V>, key: K): V
 
@@ -626,7 +626,7 @@ Returns true iff table contains an ### Function `contains` -
public fun contains<K: copy, drop, V>(table: &table_with_length::TableWithLength<K, V>, key: K): bool
+
public fun contains<K: copy, drop, V>(self: &table_with_length::TableWithLength<K, V>, key: K): bool
 
diff --git a/aptos-move/framework/aptos-stdlib/doc/type_info.md b/aptos-move/framework/aptos-stdlib/doc/type_info.md index 1d9687e05701a..42c8b37b8b58f 100644 --- a/aptos-move/framework/aptos-stdlib/doc/type_info.md +++ b/aptos-move/framework/aptos-stdlib/doc/type_info.md @@ -93,7 +93,7 @@ -
public fun account_address(type_info: &type_info::TypeInfo): address
+
public fun account_address(self: &type_info::TypeInfo): address
 
@@ -102,8 +102,8 @@ Implementation -
public fun account_address(type_info: &TypeInfo): address {
-    type_info.account_address
+
public fun account_address(self: &TypeInfo): address {
+    self.account_address
 }
 
@@ -117,7 +117,7 @@ -
public fun module_name(type_info: &type_info::TypeInfo): vector<u8>
+
public fun module_name(self: &type_info::TypeInfo): vector<u8>
 
@@ -126,8 +126,8 @@ Implementation -
public fun module_name(type_info: &TypeInfo): vector<u8> {
-    type_info.module_name
+
public fun module_name(self: &TypeInfo): vector<u8> {
+    self.module_name
 }
 
@@ -141,7 +141,7 @@ -
public fun struct_name(type_info: &type_info::TypeInfo): vector<u8>
+
public fun struct_name(self: &type_info::TypeInfo): vector<u8>
 
@@ -150,8 +150,8 @@ Implementation -
public fun struct_name(type_info: &TypeInfo): vector<u8> {
-    type_info.struct_name
+
public fun struct_name(self: &TypeInfo): vector<u8> {
+    self.struct_name
 }
 
@@ -283,7 +283,6 @@ analysis of vector size dynamism.
public fun size_of_val<T>(val_ref: &T): u64 {
-    // Return vector length of vectorized BCS representation.
     vector::length(&bcs::to_bytes(val_ref))
 }
 
@@ -451,8 +450,7 @@ analysis of vector size dynamism. -
aborts_if false;
-ensures result == spec_size_of_val<T>(val_ref);
+
ensures result == spec_size_of_val<T>(val_ref);
 
diff --git a/aptos-move/framework/aptos-stdlib/sources/any.move b/aptos-move/framework/aptos-stdlib/sources/any.move index d2851b77f44b4..480ff2460f09a 100644 --- a/aptos-move/framework/aptos-stdlib/sources/any.move +++ b/aptos-move/framework/aptos-stdlib/sources/any.move @@ -36,14 +36,14 @@ module aptos_std::any { } /// Unpack a value from the `Any` representation. This aborts if the value has not the expected type `T`. - public fun unpack(x: Any): T { - assert!(type_info::type_name() == x.type_name, error::invalid_argument(ETYPE_MISMATCH)); - from_bytes(x.data) + public fun unpack(self: Any): T { + assert!(type_info::type_name() == self.type_name, error::invalid_argument(ETYPE_MISMATCH)); + from_bytes(self.data) } /// Returns the type name of this Any - public fun type_name(x: &Any): &String { - &x.type_name + public fun type_name(self: &Any): &String { + &self.type_name } #[test_only] diff --git a/aptos-move/framework/aptos-stdlib/sources/any.spec.move b/aptos-move/framework/aptos-stdlib/sources/any.spec.move index 2e55009e4bed3..47501c83d9e27 100644 --- a/aptos-move/framework/aptos-stdlib/sources/any.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/any.spec.move @@ -15,28 +15,28 @@ spec aptos_std::any { ensures [abstract] from_bcs::deserializable(result.data); } - spec unpack(x: Any): T { + spec unpack(self: Any): T { use aptos_std::from_bcs; include UnpackAbortsIf; - ensures result == from_bcs::deserialize(x.data); + ensures result == from_bcs::deserialize(self.data); } spec schema UnpackAbortsIf { use aptos_std::from_bcs; - x: Any; - aborts_if type_info::type_name() != x.type_name; - aborts_if !from_bcs::deserializable(x.data); + self: Any; + aborts_if type_info::type_name() != self.type_name; + aborts_if !from_bcs::deserializable(self.data); } spec schema UnpackRequirement { use aptos_std::from_bcs; - x: Any; - requires type_info::type_name() == x.type_name; - requires from_bcs::deserializable(x.data); + self: Any; + requires type_info::type_name() == self.type_name; + requires from_bcs::deserializable(self.data); } - spec type_name(x: &Any): &String { + spec type_name(self: &Any): &String { aborts_if false; - ensures result == x.type_name; + ensures result == self.type_name; } } diff --git a/aptos-move/framework/aptos-stdlib/sources/capability.move b/aptos-move/framework/aptos-stdlib/sources/capability.move index b61c18ccc15e8..f68c7b0144589 100644 --- a/aptos-move/framework/aptos-stdlib/sources/capability.move +++ b/aptos-move/framework/aptos-stdlib/sources/capability.move @@ -146,34 +146,34 @@ module aptos_std::capability { /// Returns the root address associated with the given capability token. Only the owner /// of the feature can do this. - public fun root_addr(cap: Cap, _feature_witness: &Feature): address { - cap.root + public fun root_addr(self: Cap, _feature_witness: &Feature): address { + self.root } /// Returns the root address associated with the given linear capability token. - public fun linear_root_addr(cap: LinearCap, _feature_witness: &Feature): address { - cap.root + public fun linear_root_addr(self: LinearCap, _feature_witness: &Feature): address { + self.root } /// Registers a delegation relation. If the relation already exists, this function does /// nothing. // TODO: explore whether this should be idempotent like now or abort - public fun delegate(cap: Cap, _feature_witness: &Feature, to: &signer) + public fun delegate(self: Cap, _feature_witness: &Feature, to: &signer) acquires CapState { let addr = signer::address_of(to); if (exists>(addr)) return; - move_to(to, CapDelegateState { root: cap.root }); - add_element(&mut borrow_global_mut>(cap.root).delegates, addr); + move_to(to, CapDelegateState { root: self.root }); + add_element(&mut borrow_global_mut>(self.root).delegates, addr); } /// Revokes a delegation relation. If no relation exists, this function does nothing. // TODO: explore whether this should be idempotent like now or abort - public fun revoke(cap: Cap, _feature_witness: &Feature, from: address) + public fun revoke(self: Cap, _feature_witness: &Feature, from: address) acquires CapState, CapDelegateState { if (!exists>(from)) return; let CapDelegateState { root: _root } = move_from>(from); - remove_element(&mut borrow_global_mut>(cap.root).delegates, &from); + remove_element(&mut borrow_global_mut>(self.root).delegates, &from); } /// Helper to remove an element from a vector. diff --git a/aptos-move/framework/aptos-stdlib/sources/capability.spec.move b/aptos-move/framework/aptos-stdlib/sources/capability.spec.move index d7c6ba949f0a3..ef9af60e38197 100644 --- a/aptos-move/framework/aptos-stdlib/sources/capability.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/capability.spec.move @@ -44,14 +44,14 @@ spec aptos_std::capability { aborts_if !spec_has_delegate_cap(addr) && !spec_has_cap(addr); } - spec delegate(cap: Cap, _feature_witness: &Feature, to: &signer) { + spec delegate(self: Cap, _feature_witness: &Feature, to: &signer) { let addr = signer::address_of(to); ensures spec_has_delegate_cap(addr); - ensures !old(spec_has_delegate_cap(addr)) ==> global>(addr).root == cap.root; - ensures !old(spec_has_delegate_cap(addr)) ==> vector::spec_contains(spec_delegates(cap.root), addr); + ensures !old(spec_has_delegate_cap(addr)) ==> global>(addr).root == self.root; + ensures !old(spec_has_delegate_cap(addr)) ==> vector::spec_contains(spec_delegates(self.root), addr); } - spec revoke(cap: Cap, _feature_witness: &Feature, from: address) { + spec revoke(self: Cap, _feature_witness: &Feature, from: address) { ensures !spec_has_delegate_cap(from); // TODO: this cannot be proved. See issue #7422 // ensures old(spec_has_delegate_cap(from)) diff --git a/aptos-move/framework/aptos-stdlib/sources/comparator.move b/aptos-move/framework/aptos-stdlib/sources/comparator.move index 869b486b4ba5b..2a1a979613afe 100644 --- a/aptos-move/framework/aptos-stdlib/sources/comparator.move +++ b/aptos-move/framework/aptos-stdlib/sources/comparator.move @@ -11,16 +11,16 @@ module aptos_std::comparator { inner: u8, } - public fun is_equal(result: &Result): bool { - result.inner == EQUAL + public fun is_equal(self: &Result): bool { + self.inner == EQUAL } - public fun is_smaller_than(result: &Result): bool { - result.inner == SMALLER + public fun is_smaller_than(self: &Result): bool { + self.inner == SMALLER } - public fun is_greater_than(result: &Result): bool { - result.inner == GREATER + public fun is_greater_than(self: &Result): bool { + self.inner == GREATER } // Performs a comparison of two types after BCS serialization. diff --git a/aptos-move/framework/aptos-stdlib/sources/comparator.spec.move b/aptos-move/framework/aptos-stdlib/sources/comparator.spec.move index 0c14b444ad6e8..5e7d8e96a28a1 100644 --- a/aptos-move/framework/aptos-stdlib/sources/comparator.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/comparator.spec.move @@ -3,21 +3,21 @@ spec aptos_std::comparator { invariant inner == EQUAL || inner == SMALLER || inner == GREATER; } - spec is_equal(result: &Result): bool { + spec is_equal(self: &Result): bool { aborts_if false; - let res = result; + let res = self; ensures result == (res.inner == EQUAL); } - spec is_smaller_than(result: &Result): bool { + spec is_smaller_than(self: &Result): bool { aborts_if false; - let res = result; + let res = self; ensures result == (res.inner == SMALLER); } - spec is_greater_than(result: &Result): bool { + spec is_greater_than(self: &Result): bool { aborts_if false; - let res = result; + let res = self; ensures result == (res.inner == GREATER); } diff --git a/aptos-move/framework/aptos-stdlib/sources/copyable_any.move b/aptos-move/framework/aptos-stdlib/sources/copyable_any.move index b12303a3f9237..cecb0a028bf9a 100644 --- a/aptos-move/framework/aptos-stdlib/sources/copyable_any.move +++ b/aptos-move/framework/aptos-stdlib/sources/copyable_any.move @@ -24,14 +24,14 @@ module aptos_std::copyable_any { } /// Unpack a value from the `Any` representation. This aborts if the value has not the expected type `T`. - public fun unpack(x: Any): T { - assert!(type_info::type_name() == x.type_name, error::invalid_argument(ETYPE_MISMATCH)); - from_bytes(x.data) + public fun unpack(self: Any): T { + assert!(type_info::type_name() == self.type_name, error::invalid_argument(ETYPE_MISMATCH)); + from_bytes(self.data) } /// Returns the type name of this Any - public fun type_name(x: &Any): &String { - &x.type_name + public fun type_name(self: &Any): &String { + &self.type_name } #[test_only] diff --git a/aptos-move/framework/aptos-stdlib/sources/copyable_any.spec.move b/aptos-move/framework/aptos-stdlib/sources/copyable_any.spec.move index d1d64a81a4fac..f9a27e4c33838 100644 --- a/aptos-move/framework/aptos-stdlib/sources/copyable_any.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/copyable_any.spec.move @@ -16,21 +16,21 @@ spec aptos_std::copyable_any { ensures [abstract] from_bcs::deserializable(result.data); } - spec unpack(x: Any): T { + spec unpack(self: Any): T { use aptos_std::from_bcs; include UnpackAbortsIf; - ensures result == from_bcs::deserialize(x.data); + ensures result == from_bcs::deserialize(self.data); } spec schema UnpackAbortsIf { use aptos_std::from_bcs; - x: Any; - aborts_if type_info::type_name() != x.type_name; - aborts_if !from_bcs::deserializable(x.data); + self: Any; + aborts_if type_info::type_name() != self.type_name; + aborts_if !from_bcs::deserializable(self.data); } - spec type_name(x: &Any): &String { + spec type_name(self: &Any): &String { aborts_if false; - ensures result == x.type_name; + ensures result == self.type_name; } } diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.move index a7eca39732823..ce381e9e0d7cc 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.move @@ -40,17 +40,17 @@ module aptos_std::big_vector { v } - /// Destroy the vector `v`. - /// Aborts if `v` is not empty. - public fun destroy_empty(v: BigVector) { - assert!(is_empty(&v), error::invalid_argument(EVECTOR_NOT_EMPTY)); - let BigVector { buckets, end_index: _, bucket_size: _ } = v; + /// Destroy the vector `self`. + /// Aborts if `self` is not empty. + public fun destroy_empty(self: BigVector) { + assert!(is_empty(&self), error::invalid_argument(EVECTOR_NOT_EMPTY)); + let BigVector { buckets, end_index: _, bucket_size: _ } = self; table_with_length::destroy_empty(buckets); } - /// Destroy the vector `v` if T has `drop` - public fun destroy(v: BigVector) { - let BigVector { buckets, end_index, bucket_size: _ } = v; + /// Destroy the vector `self` if T has `drop` + public fun destroy(self: BigVector) { + let BigVector { buckets, end_index, bucket_size: _ } = self; let i = 0; while (end_index > 0) { let num_elements = vector::length(&table_with_length::remove(&mut buckets, i)); @@ -60,93 +60,93 @@ module aptos_std::big_vector { table_with_length::destroy_empty(buckets); } - /// Acquire an immutable reference to the `i`th element of the vector `v`. + /// Acquire an immutable reference to the `i`th element of the vector `self`. /// Aborts if `i` is out of bounds. - public fun borrow(v: &BigVector, i: u64): &T { - assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - vector::borrow(table_with_length::borrow(&v.buckets, i / v.bucket_size), i % v.bucket_size) + public fun borrow(self: &BigVector, i: u64): &T { + assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + vector::borrow(table_with_length::borrow(&self.buckets, i / self.bucket_size), i % self.bucket_size) } - /// Return a mutable reference to the `i`th element in the vector `v`. + /// Return a mutable reference to the `i`th element in the vector `self`. /// Aborts if `i` is out of bounds. - public fun borrow_mut(v: &mut BigVector, i: u64): &mut T { - assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - vector::borrow_mut(table_with_length::borrow_mut(&mut v.buckets, i / v.bucket_size), i % v.bucket_size) + public fun borrow_mut(self: &mut BigVector, i: u64): &mut T { + assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + vector::borrow_mut(table_with_length::borrow_mut(&mut self.buckets, i / self.bucket_size), i % self.bucket_size) } - /// Empty and destroy the other vector, and push each of the elements in the other vector onto the lhs vector in the + /// Empty and destroy the other vector, and push each of the elements in the other vector onto the self vector in the /// same order as they occurred in other. /// Disclaimer: This function is costly. Use it at your own discretion. - public fun append(lhs: &mut BigVector, other: BigVector) { + public fun append(self: &mut BigVector, other: BigVector) { let other_len = length(&other); let half_other_len = other_len / 2; let i = 0; while (i < half_other_len) { - push_back(lhs, swap_remove(&mut other, i)); + push_back(self, swap_remove(&mut other, i)); i = i + 1; }; while (i < other_len) { - push_back(lhs, pop_back(&mut other)); + push_back(self, pop_back(&mut other)); i = i + 1; }; destroy_empty(other); } - /// Add element `val` to the end of the vector `v`. It grows the buckets when the current buckets are full. + /// Add element `val` to the end of the vector `self`. It grows the buckets when the current buckets are full. /// This operation will cost more gas when it adds new bucket. - public fun push_back(v: &mut BigVector, val: T) { - let num_buckets = table_with_length::length(&v.buckets); - if (v.end_index == num_buckets * v.bucket_size) { - table_with_length::add(&mut v.buckets, num_buckets, vector::empty()); - vector::push_back(table_with_length::borrow_mut(&mut v.buckets, num_buckets), val); + public fun push_back(self: &mut BigVector, val: T) { + let num_buckets = table_with_length::length(&self.buckets); + if (self.end_index == num_buckets * self.bucket_size) { + table_with_length::add(&mut self.buckets, num_buckets, vector::empty()); + vector::push_back(table_with_length::borrow_mut(&mut self.buckets, num_buckets), val); } else { - vector::push_back(table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1), val); + vector::push_back(table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1), val); }; - v.end_index = v.end_index + 1; + self.end_index = self.end_index + 1; } - /// Pop an element from the end of vector `v`. It doesn't shrink the buckets even if they're empty. + /// Pop an element from the end of vector `self`. It doesn't shrink the buckets even if they're empty. /// Call `shrink_to_fit` explicity to deallocate empty buckets. - /// Aborts if `v` is empty. - public fun pop_back(v: &mut BigVector): T { - assert!(!is_empty(v), error::invalid_state(EVECTOR_EMPTY)); - let num_buckets = table_with_length::length(&v.buckets); - let last_bucket = table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1); + /// Aborts if `self` is empty. + public fun pop_back(self: &mut BigVector): T { + assert!(!is_empty(self), error::invalid_state(EVECTOR_EMPTY)); + let num_buckets = table_with_length::length(&self.buckets); + let last_bucket = table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1); let val = vector::pop_back(last_bucket); // Shrink the table if the last vector is empty. if (vector::is_empty(last_bucket)) { move last_bucket; - vector::destroy_empty(table_with_length::remove(&mut v.buckets, num_buckets - 1)); + vector::destroy_empty(table_with_length::remove(&mut self.buckets, num_buckets - 1)); }; - v.end_index = v.end_index - 1; + self.end_index = self.end_index - 1; val } - /// Remove the element at index i in the vector v and return the owned value that was previously stored at i in v. + /// Remove the element at index i in the vector v and return the owned value that was previously stored at i in self. /// All elements occurring at indices greater than i will be shifted down by 1. Will abort if i is out of bounds. /// Disclaimer: This function is costly. Use it at your own discretion. - public fun remove(v: &mut BigVector, i: u64): T { - let len = length(v); + public fun remove(self: &mut BigVector, i: u64): T { + let len = length(self); assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let num_buckets = table_with_length::length(&v.buckets); - let cur_bucket_index = i / v.bucket_size + 1; - let cur_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index - 1); - let res = vector::remove(cur_bucket, i % v.bucket_size); - v.end_index = v.end_index - 1; + let num_buckets = table_with_length::length(&self.buckets); + let cur_bucket_index = i / self.bucket_size + 1; + let cur_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index - 1); + let res = vector::remove(cur_bucket, i % self.bucket_size); + self.end_index = self.end_index - 1; move cur_bucket; while ({ spec { invariant cur_bucket_index <= num_buckets; - invariant table_with_length::spec_len(v.buckets) == num_buckets; + invariant table_with_length::spec_len(self.buckets) == num_buckets; }; (cur_bucket_index < num_buckets) }) { // remove one element from the start of current vector - let cur_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index); + let cur_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index); let t = vector::remove(cur_bucket, 0); move cur_bucket; // and put it at the end of the last one - let prev_bucket = table_with_length::borrow_mut(&mut v.buckets, cur_bucket_index - 1); + let prev_bucket = table_with_length::borrow_mut(&mut self.buckets, cur_bucket_index - 1); vector::push_back(prev_bucket, t); cur_bucket_index = cur_bucket_index + 1; }; @@ -155,50 +155,50 @@ module aptos_std::big_vector { }; // Shrink the table if the last vector is empty. - let last_bucket = table_with_length::borrow_mut(&mut v.buckets, num_buckets - 1); + let last_bucket = table_with_length::borrow_mut(&mut self.buckets, num_buckets - 1); if (vector::is_empty(last_bucket)) { move last_bucket; - vector::destroy_empty(table_with_length::remove(&mut v.buckets, num_buckets - 1)); + vector::destroy_empty(table_with_length::remove(&mut self.buckets, num_buckets - 1)); }; res } - /// Swap the `i`th element of the vector `v` with the last element and then pop the vector. + /// Swap the `i`th element of the vector `self` with the last element and then pop the vector. /// This is O(1), but does not preserve ordering of elements in the vector. /// Aborts if `i` is out of bounds. - public fun swap_remove(v: &mut BigVector, i: u64): T { - assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let last_val = pop_back(v); + public fun swap_remove(self: &mut BigVector, i: u64): T { + assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + let last_val = pop_back(self); // if the requested value is the last one, return it - if (v.end_index == i) { + if (self.end_index == i) { return last_val }; // because the lack of mem::swap, here we swap remove the requested value from the bucket // and append the last_val to the bucket then swap the last bucket val back - let bucket = table_with_length::borrow_mut(&mut v.buckets, i / v.bucket_size); + let bucket = table_with_length::borrow_mut(&mut self.buckets, i / self.bucket_size); let bucket_len = vector::length(bucket); - let val = vector::swap_remove(bucket, i % v.bucket_size); + let val = vector::swap_remove(bucket, i % self.bucket_size); vector::push_back(bucket, last_val); - vector::swap(bucket, i % v.bucket_size, bucket_len - 1); + vector::swap(bucket, i % self.bucket_size, bucket_len - 1); val } - /// Swap the elements at the i'th and j'th indices in the vector v. Will abort if either of i or j are out of bounds - /// for v. - public fun swap(v: &mut BigVector, i: u64, j: u64) { - assert!(i < length(v) && j < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let i_bucket_index = i / v.bucket_size; - let j_bucket_index = j / v.bucket_size; - let i_vector_index = i % v.bucket_size; - let j_vector_index = j % v.bucket_size; + /// Swap the elements at the i'th and j'th indices in the vector self. Will abort if either of i or j are out of bounds + /// for self. + public fun swap(self: &mut BigVector, i: u64, j: u64) { + assert!(i < length(self) && j < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + let i_bucket_index = i / self.bucket_size; + let j_bucket_index = j / self.bucket_size; + let i_vector_index = i % self.bucket_size; + let j_vector_index = j % self.bucket_size; if (i_bucket_index == j_bucket_index) { - vector::swap(table_with_length::borrow_mut(&mut v.buckets, i_bucket_index), i_vector_index, j_vector_index); + vector::swap(table_with_length::borrow_mut(&mut self.buckets, i_bucket_index), i_vector_index, j_vector_index); return }; // If i and j are in different buckets, take the buckets out first for easy mutation. - let bucket_i = table_with_length::remove(&mut v.buckets, i_bucket_index); - let bucket_j = table_with_length::remove(&mut v.buckets, j_bucket_index); + let bucket_i = table_with_length::remove(&mut self.buckets, i_bucket_index); + let bucket_j = table_with_length::remove(&mut self.buckets, j_bucket_index); // Get the elements from buckets by calling `swap_remove`. let element_i = vector::swap_remove(&mut bucket_i, i_vector_index); let element_j = vector::swap_remove(&mut bucket_j, j_vector_index); @@ -211,23 +211,23 @@ module aptos_std::big_vector { vector::swap(&mut bucket_i, i_vector_index, last_index_in_bucket_i); vector::swap(&mut bucket_j, j_vector_index, last_index_in_bucket_j); // Add back the buckets. - table_with_length::add(&mut v.buckets, i_bucket_index, bucket_i); - table_with_length::add(&mut v.buckets, j_bucket_index, bucket_j); + table_with_length::add(&mut self.buckets, i_bucket_index, bucket_i); + table_with_length::add(&mut self.buckets, j_bucket_index, bucket_j); } - /// Reverse the order of the elements in the vector v in-place. + /// Reverse the order of the elements in the vector self in-place. /// Disclaimer: This function is costly. Use it at your own discretion. - public fun reverse(v: &mut BigVector) { + public fun reverse(self: &mut BigVector) { let new_buckets = vector[]; let push_bucket = vector[]; - let num_buckets = table_with_length::length(&v.buckets); + let num_buckets = table_with_length::length(&self.buckets); let num_buckets_left = num_buckets; while (num_buckets_left > 0) { - let pop_bucket = table_with_length::remove(&mut v.buckets, num_buckets_left - 1); + let pop_bucket = table_with_length::remove(&mut self.buckets, num_buckets_left - 1); vector::for_each_reverse(pop_bucket, |val| { vector::push_back(&mut push_bucket, val); - if (vector::length(&push_bucket) == v.bucket_size) { + if (vector::length(&push_bucket) == self.bucket_size) { vector::push_back(&mut new_buckets, push_bucket); push_bucket = vector[]; }; @@ -243,61 +243,61 @@ module aptos_std::big_vector { vector::reverse(&mut new_buckets); let i = 0; - assert!(table_with_length::length(&v.buckets) == 0, 0); + assert!(table_with_length::length(&self.buckets) == 0, 0); while (i < num_buckets) { - table_with_length::add(&mut v.buckets, i, vector::pop_back(&mut new_buckets)); + table_with_length::add(&mut self.buckets, i, vector::pop_back(&mut new_buckets)); i = i + 1; }; vector::destroy_empty(new_buckets); } - /// Return the index of the first occurrence of an element in v that is equal to e. Returns (true, index) if such an + /// Return the index of the first occurrence of an element in self that is equal to e. Returns (true, index) if such an /// element was found, and (false, 0) otherwise. /// Disclaimer: This function is costly. Use it at your own discretion. - public fun index_of(v: &BigVector, val: &T): (bool, u64) { - let num_buckets = table_with_length::length(&v.buckets); + public fun index_of(self: &BigVector, val: &T): (bool, u64) { + let num_buckets = table_with_length::length(&self.buckets); let bucket_index = 0; while (bucket_index < num_buckets) { - let cur = table_with_length::borrow(&v.buckets, bucket_index); + let cur = table_with_length::borrow(&self.buckets, bucket_index); let (found, i) = vector::index_of(cur, val); if (found) { - return (true, bucket_index * v.bucket_size + i) + return (true, bucket_index * self.bucket_size + i) }; bucket_index = bucket_index + 1; }; (false, 0) } - /// Return if an element equal to e exists in the vector v. + /// Return if an element equal to e exists in the vector self. /// Disclaimer: This function is costly. Use it at your own discretion. - public fun contains(v: &BigVector, val: &T): bool { - if (is_empty(v)) return false; - let (exist, _) = index_of(v, val); + public fun contains(self: &BigVector, val: &T): bool { + if (is_empty(self)) return false; + let (exist, _) = index_of(self, val); exist } /// Convert a big vector to a native vector, which is supposed to be called mostly by view functions to get an /// atomic view of the whole vector. /// Disclaimer: This function may be costly as the big vector may be huge in size. Use it at your own discretion. - public fun to_vector(v: &BigVector): vector { + public fun to_vector(self: &BigVector): vector { let res = vector[]; - let num_buckets = table_with_length::length(&v.buckets); + let num_buckets = table_with_length::length(&self.buckets); let i = 0; while (i < num_buckets) { - vector::append(&mut res, *table_with_length::borrow(&v.buckets, i)); + vector::append(&mut res, *table_with_length::borrow(&self.buckets, i)); i = i + 1; }; res } /// Return the length of the vector. - public fun length(v: &BigVector): u64 { - v.end_index + public fun length(self: &BigVector): u64 { + self.end_index } /// Return `true` if the vector `v` has no elements and `false` otherwise. - public fun is_empty(v: &BigVector): bool { - length(v) == 0 + public fun is_empty(self: &BigVector): bool { + length(self) == 0 } #[test] diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.spec.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.spec.move index 5556d4d3d2a8f..86e971c13025a 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/big_vector.spec.move @@ -52,75 +52,75 @@ spec aptos_std::big_vector { ensures result.bucket_size == bucket_size; } - spec destroy_empty(v: BigVector) { - aborts_if !is_empty(v); + spec destroy_empty(self: BigVector) { + aborts_if !is_empty(self); } - spec borrow(v: &BigVector, i: u64): &T { - aborts_if i >= length(v); - ensures result == spec_at(v, i); + spec borrow(self: &BigVector, i: u64): &T { + aborts_if i >= length(self); + ensures result == spec_at(self, i); } - spec borrow_mut(v: &mut BigVector, i: u64): &mut T { - aborts_if i >= length(v); - ensures result == spec_at(v, i); + spec borrow_mut(self: &mut BigVector, i: u64): &mut T { + aborts_if i >= length(self); + ensures result == spec_at(self, i); } - spec push_back(v: &mut BigVector, val: T) { - let num_buckets = spec_table_len(v.buckets); + spec push_back(self: &mut BigVector, val: T) { + let num_buckets = spec_table_len(self.buckets); include PushbackAbortsIf; - ensures length(v) == length(old(v)) + 1; - ensures v.end_index == old(v.end_index) + 1; - ensures spec_at(v, v.end_index-1) == val; - ensures forall i in 0..v.end_index-1: spec_at(v, i) == spec_at(old(v), i); - ensures v.bucket_size == old(v).bucket_size; + ensures length(self) == length(old(self)) + 1; + ensures self.end_index == old(self.end_index) + 1; + ensures spec_at(self, self.end_index-1) == val; + ensures forall i in 0..self.end_index-1: spec_at(self, i) == spec_at(old(self), i); + ensures self.bucket_size == old(self).bucket_size; } spec schema PushbackAbortsIf { - v: BigVector; - let num_buckets = spec_table_len(v.buckets); - aborts_if num_buckets * v.bucket_size > MAX_U64; - aborts_if v.end_index + 1 > MAX_U64; + self: BigVector; + let num_buckets = spec_table_len(self.buckets); + aborts_if num_buckets * self.bucket_size > MAX_U64; + aborts_if self.end_index + 1 > MAX_U64; } - spec pop_back(v: &mut BigVector): T { - aborts_if is_empty(v); - ensures length(v) == length(old(v)) - 1; - ensures result == old(spec_at(v, v.end_index-1)); - ensures forall i in 0..v.end_index: spec_at(v, i) == spec_at(old(v), i); + spec pop_back(self: &mut BigVector): T { + aborts_if is_empty(self); + ensures length(self) == length(old(self)) - 1; + ensures result == old(spec_at(self, self.end_index-1)); + ensures forall i in 0..self.end_index: spec_at(self, i) == spec_at(old(self), i); } - spec swap_remove(v: &mut BigVector, i: u64): T { + spec swap_remove(self: &mut BigVector, i: u64): T { pragma verify_duration_estimate = 120; - aborts_if i >= length(v); - ensures length(v) == length(old(v)) - 1; - ensures result == spec_at(old(v), i); + aborts_if i >= length(self); + ensures length(self) == length(old(self)) - 1; + ensures result == spec_at(old(self), i); } - spec swap(v: &mut BigVector, i: u64, j: u64) { + spec swap(self: &mut BigVector, i: u64, j: u64) { pragma verify_duration_estimate = 1000; - aborts_if i >= length(v) || j >= length(v); - ensures length(v) == length(old(v)); - ensures spec_at(v, i) == spec_at(old(v), j); - ensures spec_at(v, j) == spec_at(old(v), i); - ensures forall idx in 0..length(v) + aborts_if i >= length(self) || j >= length(self); + ensures length(self) == length(old(self)); + ensures spec_at(self, i) == spec_at(old(self), j); + ensures spec_at(self, j) == spec_at(old(self), i); + ensures forall idx in 0..length(self) where idx != i && idx != j: - spec_at(v, idx) == spec_at(old(v), idx); + spec_at(self, idx) == spec_at(old(self), idx); } - spec append(lhs: &mut BigVector, other: BigVector) { + spec append(self: &mut BigVector, other: BigVector) { pragma verify=false; } - spec remove(v: &mut BigVector, i: u64): T { + spec remove(self: &mut BigVector, i: u64): T { pragma verify=false; } - spec reverse(v: &mut BigVector) { + spec reverse(self: &mut BigVector) { pragma verify=false; } - spec index_of(v: &BigVector, val: &T): (bool, u64) { + spec index_of(self: &BigVector, val: &T): (bool, u64) { pragma verify=false; } diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.move index 60a9565d0a221..c9c36712738da 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.move @@ -95,34 +95,34 @@ module aptos_std::smart_table { /// Destroy empty table. /// Aborts if it's not empty. - public fun destroy_empty(table: SmartTable) { - assert!(table.size == 0, error::invalid_argument(ENOT_EMPTY)); + public fun destroy_empty(self: SmartTable) { + assert!(self.size == 0, error::invalid_argument(ENOT_EMPTY)); let i = 0; - while (i < table.num_buckets) { - vector::destroy_empty(table_with_length::remove(&mut table.buckets, i)); + while (i < self.num_buckets) { + vector::destroy_empty(table_with_length::remove(&mut self.buckets, i)); i = i + 1; }; - let SmartTable { buckets, num_buckets: _, level: _, size: _, split_load_threshold: _, target_bucket_size: _ } = table; + let SmartTable { buckets, num_buckets: _, level: _, size: _, split_load_threshold: _, target_bucket_size: _ } = self; table_with_length::destroy_empty(buckets); } /// Destroy a table completely when V has `drop`. - public fun destroy(table: SmartTable) { - clear(&mut table); - destroy_empty(table); + public fun destroy(self: SmartTable) { + clear(&mut self); + destroy_empty(self); } /// Clear a table completely when T has `drop`. - public fun clear(table: &mut SmartTable) { - *table_with_length::borrow_mut(&mut table.buckets, 0) = vector::empty(); + public fun clear(self: &mut SmartTable) { + *table_with_length::borrow_mut(&mut self.buckets, 0) = vector::empty(); let i = 1; - while (i < table.num_buckets) { - table_with_length::remove(&mut table.buckets, i); + while (i < self.num_buckets) { + table_with_length::remove(&mut self.buckets, i); i = i + 1; }; - table.num_buckets = 1; - table.level = 0; - table.size = 0; + self.num_buckets = 1; + self.level = 0; + self.size = 0; } /// Add (key, value) pair in the hash map, it may grow one bucket if current load factor exceeds the threshold. @@ -130,10 +130,10 @@ module aptos_std::smart_table { /// For standard linear hash algorithm, it is stored as a variable but `num_buckets` here could be leveraged. /// Abort if `key` already exists. /// Note: This method may occasionally cost much more gas when triggering bucket split. - public fun add(table: &mut SmartTable, key: K, value: V) { + public fun add(self: &mut SmartTable, key: K, value: V) { let hash = sip_hash_from_value(&key); - let index = bucket_index(table.level, table.num_buckets, hash); - let bucket = table_with_length::borrow_mut(&mut table.buckets, index); + let index = bucket_index(self.level, self.num_buckets, hash); + let bucket = table_with_length::borrow_mut(&mut self.buckets, index); // We set a per-bucket limit here with a upper bound (10000) that nobody should normally reach. assert!(vector::length(bucket) <= 10000, error::permission_denied(EEXCEED_MAX_BUCKET_SIZE)); assert!(vector::all(bucket, | entry | { @@ -141,21 +141,21 @@ module aptos_std::smart_table { &e.key != &key }), error::invalid_argument(EALREADY_EXIST)); let e = Entry { hash, key, value }; - if (table.target_bucket_size == 0) { + if (self.target_bucket_size == 0) { let estimated_entry_size = max(size_of_val(&e), 1); - table.target_bucket_size = max(1024 /* free_write_quota */ / estimated_entry_size, 1); + self.target_bucket_size = max(1024 /* free_write_quota */ / estimated_entry_size, 1); }; vector::push_back(bucket, e); - table.size = table.size + 1; + self.size = self.size + 1; - if (load_factor(table) >= (table.split_load_threshold as u64)) { - split_one_bucket(table); + if (load_factor(self) >= (self.split_load_threshold as u64)) { + split_one_bucket(self); } } /// Add multiple key/value pairs to the smart table. The keys must not already exist. - public fun add_all(table: &mut SmartTable, keys: vector, values: vector) { - vector::zip(keys, values, |key, value| { add(table, key, value); }); + public fun add_all(self: &mut SmartTable, keys: vector, values: vector) { + vector::zip(keys, values, |key, value| { add(self, key, value); }); } inline fun unzip_entries(entries: &vector>): (vector, vector) { @@ -173,12 +173,12 @@ module aptos_std::smart_table { /// view of the whole table. /// Disclaimer: This function may be costly as the smart table may be huge in size. Use it at your own discretion. public fun to_simple_map( - table: &SmartTable, + self: &SmartTable, ): SimpleMap { let i = 0; let res = simple_map::new(); - while (i < table.num_buckets) { - let (keys, values) = unzip_entries(table_with_length::borrow(&table.buckets, i)); + while (i < self.num_buckets) { + let (keys, values) = unzip_entries(table_with_length::borrow(&self.buckets, i)); simple_map::add_all(&mut res, keys, values); i = i + 1; }; @@ -190,9 +190,9 @@ module aptos_std::smart_table { /// For a large enough smart table this function will fail due to execution gas limits, and /// `keys_paginated` should be used instead. public fun keys( - table_ref: &SmartTable + self: &SmartTable ): vector { - let (keys, _, _) = keys_paginated(table_ref, 0, 0, length(table_ref)); + let (keys, _, _) = keys_paginated(self, 0, 0, length(self)); keys } @@ -210,7 +210,7 @@ module aptos_std::smart_table { /// returned bucket index and vector index value options are both none, which means that /// pagination is complete. For an example, see `test_keys()`. public fun keys_paginated( - table_ref: &SmartTable, + self: &SmartTable, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64, @@ -219,8 +219,8 @@ module aptos_std::smart_table { Option, Option, ) { - let num_buckets = table_ref.num_buckets; - let buckets_ref = &table_ref.buckets; + let num_buckets = self.num_buckets; + let buckets_ref = &self.buckets; assert!(starting_bucket_index < num_buckets, EINVALID_BUCKET_INDEX); let bucket_ref = table_with_length::borrow(buckets_ref, starting_bucket_index); let bucket_length = vector::length(bucket_ref); @@ -262,23 +262,23 @@ module aptos_std::smart_table { } /// Decide which is the next bucket to split and split it into two with the elements inside the bucket. - fun split_one_bucket(table: &mut SmartTable) { - let new_bucket_index = table.num_buckets; + fun split_one_bucket(self: &mut SmartTable) { + let new_bucket_index = self.num_buckets; // the next bucket to split is num_bucket without the most significant bit. - let to_split = new_bucket_index ^ (1 << table.level); - table.num_buckets = new_bucket_index + 1; + let to_split = new_bucket_index ^ (1 << self.level); + self.num_buckets = new_bucket_index + 1; // if the whole level is splitted once, bump the level. - if (to_split + 1 == 1 << table.level) { - table.level = table.level + 1; + if (to_split + 1 == 1 << self.level) { + self.level = self.level + 1; }; - let old_bucket = table_with_length::borrow_mut(&mut table.buckets, to_split); + let old_bucket = table_with_length::borrow_mut(&mut self.buckets, to_split); // partition the bucket, [0..p) stays in old bucket, [p..len) goes to new bucket let p = vector::partition(old_bucket, |e| { let entry: &Entry = e; // Explicit type to satisfy compiler - bucket_index(table.level, table.num_buckets, entry.hash) != new_bucket_index + bucket_index(self.level, self.num_buckets, entry.hash) != new_bucket_index }); let new_bucket = vector::trim_reverse(old_bucket, p); - table_with_length::add(&mut table.buckets, new_bucket_index, new_bucket); + table_with_length::add(&mut self.buckets, new_bucket_index, new_bucket); } /// Return the expected bucket index to find the hash. @@ -297,9 +297,9 @@ module aptos_std::smart_table { /// Acquire an immutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow(table: &SmartTable, key: K): &V { - let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key)); - let bucket = table_with_length::borrow(&table.buckets, index); + public fun borrow(self: &SmartTable, key: K): &V { + let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key)); + let bucket = table_with_length::borrow(&self.buckets, index); let i = 0; let len = vector::length(bucket); while (i < len) { @@ -314,19 +314,19 @@ module aptos_std::smart_table { /// Acquire an immutable reference to the value which `key` maps to. /// Returns specified default value if there is no entry for `key`. - public fun borrow_with_default(table: &SmartTable, key: K, default: &V): &V { - if (!contains(table, copy key)) { + public fun borrow_with_default(self: &SmartTable, key: K, default: &V): &V { + if (!contains(self, copy key)) { default } else { - borrow(table, copy key) + borrow(self, copy key) } } /// Acquire a mutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow_mut(table: &mut SmartTable, key: K): &mut V { - let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key)); - let bucket = table_with_length::borrow_mut(&mut table.buckets, index); + public fun borrow_mut(self: &mut SmartTable, key: K): &mut V { + let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key)); + let bucket = table_with_length::borrow_mut(&mut self.buckets, index); let i = 0; let len = vector::length(bucket); while (i < len) { @@ -342,21 +342,21 @@ module aptos_std::smart_table { /// Acquire a mutable reference to the value which `key` maps to. /// Insert the pair (`key`, `default`) first if there is no entry for `key`. public fun borrow_mut_with_default( - table: &mut SmartTable, + self: &mut SmartTable, key: K, default: V ): &mut V { - if (!contains(table, copy key)) { - add(table, copy key, default) + if (!contains(self, copy key)) { + add(self, copy key, default) }; - borrow_mut(table, key) + borrow_mut(self, key) } /// Returns true iff `table` contains an entry for `key`. - public fun contains(table: &SmartTable, key: K): bool { + public fun contains(self: &SmartTable, key: K): bool { let hash = sip_hash_from_value(&key); - let index = bucket_index(table.level, table.num_buckets, hash); - let bucket = table_with_length::borrow(&table.buckets, index); + let index = bucket_index(self.level, self.num_buckets, hash); + let bucket = table_with_length::borrow(&self.buckets, index); vector::any(bucket, | entry | { let e: &Entry = entry; e.hash == hash && &e.key == &key @@ -365,16 +365,16 @@ module aptos_std::smart_table { /// Remove from `table` and return the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun remove(table: &mut SmartTable, key: K): V { - let index = bucket_index(table.level, table.num_buckets, sip_hash_from_value(&key)); - let bucket = table_with_length::borrow_mut(&mut table.buckets, index); + public fun remove(self: &mut SmartTable, key: K): V { + let index = bucket_index(self.level, self.num_buckets, sip_hash_from_value(&key)); + let bucket = table_with_length::borrow_mut(&mut self.buckets, index); let i = 0; let len = vector::length(bucket); while (i < len) { let entry = vector::borrow(bucket, i); if (&entry.key == &key) { let Entry { hash: _, key: _, value } = vector::swap_remove(bucket, i); - table.size = table.size - 1; + self.size = self.size - 1; return value }; i = i + 1; @@ -384,46 +384,46 @@ module aptos_std::smart_table { /// Insert the pair (`key`, `value`) if there is no entry for `key`. /// update the value of the entry for `key` to `value` otherwise - public fun upsert(table: &mut SmartTable, key: K, value: V) { - if (!contains(table, copy key)) { - add(table, copy key, value) + public fun upsert(self: &mut SmartTable, key: K, value: V) { + if (!contains(self, copy key)) { + add(self, copy key, value) } else { - let ref = borrow_mut(table, key); + let ref = borrow_mut(self, key); *ref = value; }; } /// Returns the length of the table, i.e. the number of entries. - public fun length(table: &SmartTable): u64 { - table.size + public fun length(self: &SmartTable): u64 { + self.size } /// Return the load factor of the hashtable. - public fun load_factor(table: &SmartTable): u64 { - table.size * 100 / table.num_buckets / table.target_bucket_size + public fun load_factor(self: &SmartTable): u64 { + self.size * 100 / self.num_buckets / self.target_bucket_size } /// Update `split_load_threshold`. - public fun update_split_load_threshold(table: &mut SmartTable, split_load_threshold: u8) { + public fun update_split_load_threshold(self: &mut SmartTable, split_load_threshold: u8) { assert!( split_load_threshold <= 100 && split_load_threshold > 0, error::invalid_argument(EINVALID_LOAD_THRESHOLD_PERCENT) ); - table.split_load_threshold = split_load_threshold; + self.split_load_threshold = split_load_threshold; } /// Update `target_bucket_size`. - public fun update_target_bucket_size(table: &mut SmartTable, target_bucket_size: u64) { + public fun update_target_bucket_size(self: &mut SmartTable, target_bucket_size: u64) { assert!(target_bucket_size > 0, error::invalid_argument(EINVALID_TARGET_BUCKET_SIZE)); - table.target_bucket_size = target_bucket_size; + self.target_bucket_size = target_bucket_size; } /// Apply the function to a reference of each key-value pair in the table. - public inline fun for_each_ref(table: &SmartTable, f: |&K, &V|) { + public inline fun for_each_ref(self: &SmartTable, f: |&K, &V|) { let i = 0; - while (i < aptos_std::smart_table::num_buckets(table)) { + while (i < aptos_std::smart_table::num_buckets(self)) { vector::for_each_ref( - aptos_std::table_with_length::borrow(aptos_std::smart_table::borrow_buckets(table), i), + aptos_std::table_with_length::borrow(aptos_std::smart_table::borrow_buckets(self), i), |elem| { let (key, value) = aptos_std::smart_table::borrow_kv(elem); f(key, value) @@ -434,11 +434,11 @@ module aptos_std::smart_table { } /// Apply the function to a mutable reference of each key-value pair in the table. - public inline fun for_each_mut(table: &mut SmartTable, f: |&K, &mut V|) { + public inline fun for_each_mut(self: &mut SmartTable, f: |&K, &mut V|) { let i = 0; - while (i < aptos_std::smart_table::num_buckets(table)) { + while (i < aptos_std::smart_table::num_buckets(self)) { vector::for_each_mut( - table_with_length::borrow_mut(aptos_std::smart_table::borrow_buckets_mut(table), i), + table_with_length::borrow_mut(aptos_std::smart_table::borrow_buckets_mut(self), i), |elem| { let (key, value) = aptos_std::smart_table::borrow_kv_mut(elem); f(key, value) @@ -450,23 +450,23 @@ module aptos_std::smart_table { /// Map the function over the references of key-value pairs in the table without modifying it. public inline fun map_ref( - table: &SmartTable, + self: &SmartTable, f: |&V1|V2 ): SmartTable { let new_table = new(); - for_each_ref(table, |key, value| add(&mut new_table, *key, f(value))); + for_each_ref(self, |key, value| add(&mut new_table, *key, f(value))); new_table } /// Return true if any key-value pair in the table satisfies the predicate. public inline fun any( - table: &SmartTable, + self: &SmartTable, p: |&K, &V|bool ): bool { let found = false; let i = 0; - while (i < aptos_std::smart_table::num_buckets(table)) { - found = vector::any(table_with_length::borrow(aptos_std::smart_table::borrow_buckets(table), i), |elem| { + while (i < aptos_std::smart_table::num_buckets(self)) { + found = vector::any(table_with_length::borrow(aptos_std::smart_table::borrow_buckets(self), i), |elem| { let (key, value) = aptos_std::smart_table::borrow_kv(elem); p(key, value) }); @@ -477,24 +477,24 @@ module aptos_std::smart_table { } // Helper functions to circumvent the scope issue of inline functions. - public fun borrow_kv(e: &Entry): (&K, &V) { - (&e.key, &e.value) + public fun borrow_kv(self: &Entry): (&K, &V) { + (&self.key, &self.value) } - public fun borrow_kv_mut(e: &mut Entry): (&mut K, &mut V) { - (&mut e.key, &mut e.value) + public fun borrow_kv_mut(self: &mut Entry): (&mut K, &mut V) { + (&mut self.key, &mut self.value) } - public fun num_buckets(table: &SmartTable): u64 { - table.num_buckets + public fun num_buckets(self: &SmartTable): u64 { + self.num_buckets } - public fun borrow_buckets(table: &SmartTable): &TableWithLength>> { - &table.buckets + public fun borrow_buckets(self: &SmartTable): &TableWithLength>> { + &self.buckets } - public fun borrow_buckets_mut(table: &mut SmartTable): &mut TableWithLength>> { - &mut table.buckets + public fun borrow_buckets_mut(self: &mut SmartTable): &mut TableWithLength>> { + &mut self.buckets } diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.spec.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.spec.move index 113bb4f06cabf..d905a0a40bb3a 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_table.spec.move @@ -22,15 +22,15 @@ spec aptos_std::smart_table { pragma verify = false; } - spec destroy(table: SmartTable) { + spec destroy(self: SmartTable) { pragma verify = false; } - spec clear(table: &mut SmartTable) { + spec clear(self: &mut SmartTable) { pragma verify = false; } - spec split_one_bucket(table: &mut SmartTable) { + spec split_one_bucket(self: &mut SmartTable) { pragma verify = false; } @@ -38,26 +38,26 @@ spec aptos_std::smart_table { pragma verify = false; } - spec borrow_with_default(table: &SmartTable, key: K, default: &V): &V { + spec borrow_with_default(self: &SmartTable, key: K, default: &V): &V { pragma verify = false; } - spec load_factor(table: &SmartTable): u64 { + spec load_factor(self: &SmartTable): u64 { pragma verify = false; } spec to_simple_map( - table: &SmartTable, + self: &SmartTable, ): SimpleMap { pragma verify = false; } - spec keys(table_ref: &SmartTable): vector { + spec keys(self: &SmartTable): vector { pragma verify = false; } spec keys_paginated( - table_ref: &SmartTable, + self: &SmartTable, starting_bucket_index: u64, starting_vector_index: u64, num_keys_to_get: u64, @@ -69,35 +69,35 @@ spec aptos_std::smart_table { pragma verify = false; } - spec add_all(table: &mut SmartTable, keys: vector, values: vector) { + spec add_all(self: &mut SmartTable, keys: vector, values: vector) { pragma verify = false; } - spec update_split_load_threshold(table: &mut SmartTable, split_load_threshold: u8) { + spec update_split_load_threshold(self: &mut SmartTable, split_load_threshold: u8) { pragma verify = false; } - spec update_target_bucket_size(table: &mut SmartTable, target_bucket_size: u64) { + spec update_target_bucket_size(self: &mut SmartTable, target_bucket_size: u64) { pragma verify = false; } - spec borrow_kv(e: &Entry): (&K, &V) { + spec borrow_kv(self: &Entry): (&K, &V) { pragma verify = false; } - spec borrow_kv_mut(e: &mut Entry): (&mut K, &mut V) { + spec borrow_kv_mut(self: &mut Entry): (&mut K, &mut V) { pragma verify = false; } - spec num_buckets(table: &SmartTable): u64 { + spec num_buckets(self: &SmartTable): u64 { pragma verify = false; } - spec borrow_buckets(table: &SmartTable): &TableWithLength>> { + spec borrow_buckets(self: &SmartTable): &TableWithLength>> { pragma verify = false; } - spec borrow_buckets_mut(table: &mut SmartTable): &mut TableWithLength>> { + spec borrow_buckets_mut(self: &mut SmartTable): &mut TableWithLength>> { pragma verify = false; } diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.move index 10f3c816b2fa7..5fa0d13977f17 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.move @@ -67,119 +67,119 @@ module aptos_std::smart_vector { v } - /// Destroy the vector `v`. - /// Aborts if `v` is not empty. - public fun destroy_empty(v: SmartVector) { - assert!(is_empty(&v), error::invalid_argument(EVECTOR_NOT_EMPTY)); - let SmartVector { inline_vec, big_vec, inline_capacity: _, bucket_size: _ } = v; + /// Destroy the vector `self`. + /// Aborts if `self` is not empty. + public fun destroy_empty(self: SmartVector) { + assert!(is_empty(&self), error::invalid_argument(EVECTOR_NOT_EMPTY)); + let SmartVector { inline_vec, big_vec, inline_capacity: _, bucket_size: _ } = self; vector::destroy_empty(inline_vec); option::destroy_none(big_vec); } /// Destroy a vector completely when T has `drop`. - public fun destroy(v: SmartVector) { - clear(&mut v); - destroy_empty(v); + public fun destroy(self: SmartVector) { + clear(&mut self); + destroy_empty(self); } /// Clear a vector completely when T has `drop`. - public fun clear(v: &mut SmartVector) { - v.inline_vec = vector[]; - if (option::is_some(&v.big_vec)) { - big_vector::destroy(option::extract(&mut v.big_vec)); + public fun clear(self: &mut SmartVector) { + self.inline_vec = vector[]; + if (option::is_some(&self.big_vec)) { + big_vector::destroy(option::extract(&mut self.big_vec)); } } - /// Acquire an immutable reference to the `i`th T of the vector `v`. + /// Acquire an immutable reference to the `i`th T of the vector `self`. /// Aborts if `i` is out of bounds. - public fun borrow(v: &SmartVector, i: u64): &T { - assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let inline_len = vector::length(&v.inline_vec); + public fun borrow(self: &SmartVector, i: u64): &T { + assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + let inline_len = vector::length(&self.inline_vec); if (i < inline_len) { - vector::borrow(&v.inline_vec, i) + vector::borrow(&self.inline_vec, i) } else { - big_vector::borrow(option::borrow(&v.big_vec), i - inline_len) + big_vector::borrow(option::borrow(&self.big_vec), i - inline_len) } } - /// Return a mutable reference to the `i`th T in the vector `v`. + /// Return a mutable reference to the `i`th T in the vector `self`. /// Aborts if `i` is out of bounds. - public fun borrow_mut(v: &mut SmartVector, i: u64): &mut T { - assert!(i < length(v), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let inline_len = vector::length(&v.inline_vec); + public fun borrow_mut(self: &mut SmartVector, i: u64): &mut T { + assert!(i < length(self), error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); + let inline_len = vector::length(&self.inline_vec); if (i < inline_len) { - vector::borrow_mut(&mut v.inline_vec, i) + vector::borrow_mut(&mut self.inline_vec, i) } else { - big_vector::borrow_mut(option::borrow_mut(&mut v.big_vec), i - inline_len) + big_vector::borrow_mut(option::borrow_mut(&mut self.big_vec), i - inline_len) } } - /// Empty and destroy the other vector, and push each of the Ts in the other vector onto the lhs vector in the + /// Empty and destroy the other vector, and push each of the Ts in the other vector onto the self vector in the /// same order as they occurred in other. /// Disclaimer: This function may be costly. Use it at your own discretion. - public fun append(lhs: &mut SmartVector, other: SmartVector) { + public fun append(self: &mut SmartVector, other: SmartVector) { let other_len = length(&other); let half_other_len = other_len / 2; let i = 0; while (i < half_other_len) { - push_back(lhs, swap_remove(&mut other, i)); + push_back(self, swap_remove(&mut other, i)); i = i + 1; }; while (i < other_len) { - push_back(lhs, pop_back(&mut other)); + push_back(self, pop_back(&mut other)); i = i + 1; }; destroy_empty(other); } /// Add multiple values to the vector at once. - public fun add_all(v: &mut SmartVector, vals: vector) { - vector::for_each(vals, |val| { push_back(v, val); }) + public fun add_all(self: &mut SmartVector, vals: vector) { + vector::for_each(vals, |val| { push_back(self, val); }) } /// Convert a smart vector to a native vector, which is supposed to be called mostly by view functions to get an /// atomic view of the whole vector. /// Disclaimer: This function may be costly as the smart vector may be huge in size. Use it at your own discretion. - public fun to_vector(v: &SmartVector): vector { - let res = v.inline_vec; - if (option::is_some(&v.big_vec)) { - let big_vec = option::borrow(&v.big_vec); + public fun to_vector(self: &SmartVector): vector { + let res = self.inline_vec; + if (option::is_some(&self.big_vec)) { + let big_vec = option::borrow(&self.big_vec); vector::append(&mut res, big_vector::to_vector(big_vec)); }; res } - /// Add T `val` to the end of the vector `v`. It grows the buckets when the current buckets are full. + /// Add T `val` to the end of the vector `self`. It grows the buckets when the current buckets are full. /// This operation will cost more gas when it adds new bucket. - public fun push_back(v: &mut SmartVector, val: T) { - let len = length(v); - let inline_len = vector::length(&v.inline_vec); + public fun push_back(self: &mut SmartVector, val: T) { + let len = length(self); + let inline_len = vector::length(&self.inline_vec); if (len == inline_len) { - let bucket_size = if (option::is_some(&v.inline_capacity)) { - if (len < *option::borrow(&v.inline_capacity)) { - vector::push_back(&mut v.inline_vec, val); + let bucket_size = if (option::is_some(&self.inline_capacity)) { + if (len < *option::borrow(&self.inline_capacity)) { + vector::push_back(&mut self.inline_vec, val); return }; - *option::borrow(&v.bucket_size) + *option::borrow(&self.bucket_size) } else { let val_size = size_of_val(&val); if (val_size * (inline_len + 1) < 150 /* magic number */) { - vector::push_back(&mut v.inline_vec, val); + vector::push_back(&mut self.inline_vec, val); return }; - let estimated_avg_size = max((size_of_val(&v.inline_vec) + val_size) / (inline_len + 1), 1); + let estimated_avg_size = max((size_of_val(&self.inline_vec) + val_size) / (inline_len + 1), 1); max(1024 /* free_write_quota */ / estimated_avg_size, 1) }; - option::fill(&mut v.big_vec, big_vector::empty(bucket_size)); + option::fill(&mut self.big_vec, big_vector::empty(bucket_size)); }; - big_vector::push_back(option::borrow_mut(&mut v.big_vec), val); + big_vector::push_back(option::borrow_mut(&mut self.big_vec), val); } - /// Pop an T from the end of vector `v`. It does shrink the buckets if they're empty. - /// Aborts if `v` is empty. - public fun pop_back(v: &mut SmartVector): T { - assert!(!is_empty(v), error::invalid_state(EVECTOR_EMPTY)); - let big_vec_wrapper = &mut v.big_vec; + /// Pop an T from the end of vector `self`. It does shrink the buckets if they're empty. + /// Aborts if `self` is empty. + public fun pop_back(self: &mut SmartVector): T { + assert!(!is_empty(self), error::invalid_state(EVECTOR_EMPTY)); + let big_vec_wrapper = &mut self.big_vec; if (option::is_some(big_vec_wrapper)) { let big_vec = option::extract(big_vec_wrapper); let val = big_vector::pop_back(&mut big_vec); @@ -190,21 +190,21 @@ module aptos_std::smart_vector { }; val } else { - vector::pop_back(&mut v.inline_vec) + vector::pop_back(&mut self.inline_vec) } } - /// Remove the T at index i in the vector v and return the owned value that was previously stored at i in v. + /// Remove the T at index i in the vector self and return the owned value that was previously stored at i in self. /// All Ts occurring at indices greater than i will be shifted down by 1. Will abort if i is out of bounds. /// Disclaimer: This function may be costly. Use it at your own discretion. - public fun remove(v: &mut SmartVector, i: u64): T { - let len = length(v); + public fun remove(self: &mut SmartVector, i: u64): T { + let len = length(self); assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let inline_len = vector::length(&v.inline_vec); + let inline_len = vector::length(&self.inline_vec); if (i < inline_len) { - vector::remove(&mut v.inline_vec, i) + vector::remove(&mut self.inline_vec, i) } else { - let big_vec_wrapper = &mut v.big_vec; + let big_vec_wrapper = &mut self.big_vec; let big_vec = option::extract(big_vec_wrapper); let val = big_vector::remove(&mut big_vec, i - inline_len); if (big_vector::is_empty(&big_vec)) { @@ -216,15 +216,15 @@ module aptos_std::smart_vector { } } - /// Swap the `i`th T of the vector `v` with the last T and then pop the vector. + /// Swap the `i`th T of the vector `self` with the last T and then pop the vector. /// This is O(1), but does not preserve ordering of Ts in the vector. /// Aborts if `i` is out of bounds. - public fun swap_remove(v: &mut SmartVector, i: u64): T { - let len = length(v); + public fun swap_remove(self: &mut SmartVector, i: u64): T { + let len = length(self); assert!(i < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let inline_len = vector::length(&v.inline_vec); - let big_vec_wrapper = &mut v.big_vec; - let inline_vec = &mut v.inline_vec; + let inline_len = vector::length(&self.inline_vec); + let big_vec_wrapper = &mut self.big_vec; + let inline_vec = &mut self.inline_vec; if (i >= inline_len) { let big_vec = option::extract(big_vec_wrapper); let val = big_vector::swap_remove(&mut big_vec, i - inline_len); @@ -250,21 +250,21 @@ module aptos_std::smart_vector { } /// Swap the Ts at the i'th and j'th indices in the vector v. Will abort if either of i or j are out of bounds - /// for v. - public fun swap(v: &mut SmartVector, i: u64, j: u64) { + /// for self. + public fun swap(self: &mut SmartVector, i: u64, j: u64) { if (i > j) { - return swap(v, j, i) + return swap(self, j, i) }; - let len = length(v); + let len = length(self); assert!(j < len, error::invalid_argument(EINDEX_OUT_OF_BOUNDS)); - let inline_len = vector::length(&v.inline_vec); + let inline_len = vector::length(&self.inline_vec); if (i >= inline_len) { - big_vector::swap(option::borrow_mut(&mut v.big_vec), i - inline_len, j - inline_len); + big_vector::swap(option::borrow_mut(&mut self.big_vec), i - inline_len, j - inline_len); } else if (j < inline_len) { - vector::swap(&mut v.inline_vec, i, j); + vector::swap(&mut self.inline_vec, i, j); } else { - let big_vec = option::borrow_mut(&mut v.big_vec); - let inline_vec = &mut v.inline_vec; + let big_vec = option::borrow_mut(&mut self.big_vec); + let inline_vec = &mut self.inline_vec; let element_i = vector::swap_remove(inline_vec, i); let element_j = big_vector::swap_remove(big_vec, j - inline_len); vector::push_back(inline_vec, element_j); @@ -274,128 +274,128 @@ module aptos_std::smart_vector { } } - /// Reverse the order of the Ts in the vector v in-place. + /// Reverse the order of the Ts in the vector self in-place. /// Disclaimer: This function may be costly. Use it at your own discretion. - public fun reverse(v: &mut SmartVector) { - let inline_len = vector::length(&v.inline_vec); + public fun reverse(self: &mut SmartVector) { + let inline_len = vector::length(&self.inline_vec); let i = 0; let new_inline_vec = vector[]; // Push the last `inline_len` Ts into a temp vector. while (i < inline_len) { - vector::push_back(&mut new_inline_vec, pop_back(v)); + vector::push_back(&mut new_inline_vec, pop_back(self)); i = i + 1; }; vector::reverse(&mut new_inline_vec); // Reverse the big_vector left if exists. - if (option::is_some(&v.big_vec)) { - big_vector::reverse(option::borrow_mut(&mut v.big_vec)); + if (option::is_some(&self.big_vec)) { + big_vector::reverse(option::borrow_mut(&mut self.big_vec)); }; // Mem::swap the two vectors. let temp_vec = vector[]; - while (!vector::is_empty(&mut v.inline_vec)) { - vector::push_back(&mut temp_vec, vector::pop_back(&mut v.inline_vec)); + while (!vector::is_empty(&mut self.inline_vec)) { + vector::push_back(&mut temp_vec, vector::pop_back(&mut self.inline_vec)); }; vector::reverse(&mut temp_vec); while (!vector::is_empty(&mut new_inline_vec)) { - vector::push_back(&mut v.inline_vec, vector::pop_back(&mut new_inline_vec)); + vector::push_back(&mut self.inline_vec, vector::pop_back(&mut new_inline_vec)); }; vector::destroy_empty(new_inline_vec); // Push the rest Ts originally left in inline_vector back to the end of the smart vector. while (!vector::is_empty(&mut temp_vec)) { - push_back(v, vector::pop_back(&mut temp_vec)); + push_back(self, vector::pop_back(&mut temp_vec)); }; vector::destroy_empty(temp_vec); } - /// Return `(true, i)` if `val` is in the vector `v` at index `i`. + /// Return `(true, i)` if `val` is in the vector `self` at index `i`. /// Otherwise, returns `(false, 0)`. /// Disclaimer: This function may be costly. Use it at your own discretion. - public fun index_of(v: &SmartVector, val: &T): (bool, u64) { - let (found, i) = vector::index_of(&v.inline_vec, val); + public fun index_of(self: &SmartVector, val: &T): (bool, u64) { + let (found, i) = vector::index_of(&self.inline_vec, val); if (found) { (true, i) - } else if (option::is_some(&v.big_vec)) { - let (found, i) = big_vector::index_of(option::borrow(&v.big_vec), val); - (found, i + vector::length(&v.inline_vec)) + } else if (option::is_some(&self.big_vec)) { + let (found, i) = big_vector::index_of(option::borrow(&self.big_vec), val); + (found, i + vector::length(&self.inline_vec)) } else { (false, 0) } } - /// Return true if `val` is in the vector `v`. + /// Return true if `val` is in the vector `self`. /// Disclaimer: This function may be costly. Use it at your own discretion. - public fun contains(v: &SmartVector, val: &T): bool { - if (is_empty(v)) return false; - let (exist, _) = index_of(v, val); + public fun contains(self: &SmartVector, val: &T): bool { + if (is_empty(self)) return false; + let (exist, _) = index_of(self, val); exist } /// Return the length of the vector. - public fun length(v: &SmartVector): u64 { - vector::length(&v.inline_vec) + if (option::is_none(&v.big_vec)) { + public fun length(self: &SmartVector): u64 { + vector::length(&self.inline_vec) + if (option::is_none(&self.big_vec)) { 0 } else { - big_vector::length(option::borrow(&v.big_vec)) + big_vector::length(option::borrow(&self.big_vec)) } } - /// Return `true` if the vector `v` has no Ts and `false` otherwise. - public fun is_empty(v: &SmartVector): bool { - length(v) == 0 + /// Return `true` if the vector `self` has no Ts and `false` otherwise. + public fun is_empty(self: &SmartVector): bool { + length(self) == 0 } /// Apply the function to each T in the vector, consuming it. - public inline fun for_each(v: SmartVector, f: |T|) { - aptos_std::smart_vector::reverse(&mut v); // We need to reverse the vector to consume it efficiently - aptos_std::smart_vector::for_each_reverse(v, |e| f(e)); + public inline fun for_each(self: SmartVector, f: |T|) { + aptos_std::smart_vector::reverse(&mut self); // We need to reverse the vector to consume it efficiently + aptos_std::smart_vector::for_each_reverse(self, |e| f(e)); } /// Apply the function to each T in the vector, consuming it. - public inline fun for_each_reverse(v: SmartVector, f: |T|) { - let len = aptos_std::smart_vector::length(&v); + public inline fun for_each_reverse(self: SmartVector, f: |T|) { + let len = aptos_std::smart_vector::length(&self); while (len > 0) { - f(aptos_std::smart_vector::pop_back(&mut v)); + f(aptos_std::smart_vector::pop_back(&mut self)); len = len - 1; }; - aptos_std::smart_vector::destroy_empty(v) + aptos_std::smart_vector::destroy_empty(self) } /// Apply the function to a reference of each T in the vector. - public inline fun for_each_ref(v: &SmartVector, f: |&T|) { + public inline fun for_each_ref(self: &SmartVector, f: |&T|) { let i = 0; - let len = aptos_std::smart_vector::length(v); + let len = aptos_std::smart_vector::length(self); while (i < len) { - f(aptos_std::smart_vector::borrow(v, i)); + f(aptos_std::smart_vector::borrow(self, i)); i = i + 1 } } /// Apply the function to a mutable reference to each T in the vector. - public inline fun for_each_mut(v: &mut SmartVector, f: |&mut T|) { + public inline fun for_each_mut(self: &mut SmartVector, f: |&mut T|) { let i = 0; - let len = aptos_std::smart_vector::length(v); + let len = aptos_std::smart_vector::length(self); while (i < len) { - f(aptos_std::smart_vector::borrow_mut(v, i)); + f(aptos_std::smart_vector::borrow_mut(self, i)); i = i + 1 } } /// Apply the function to a reference of each T in the vector with its index. - public inline fun enumerate_ref(v: &SmartVector, f: |u64, &T|) { + public inline fun enumerate_ref(self: &SmartVector, f: |u64, &T|) { let i = 0; - let len = aptos_std::smart_vector::length(v); + let len = aptos_std::smart_vector::length(self); while (i < len) { - f(i, aptos_std::smart_vector::borrow(v, i)); + f(i, aptos_std::smart_vector::borrow(self, i)); i = i + 1; }; } /// Apply the function to a mutable reference of each T in the vector with its index. - public inline fun enumerate_mut(v: &mut SmartVector, f: |u64, &mut T|) { + public inline fun enumerate_mut(self: &mut SmartVector, f: |u64, &mut T|) { let i = 0; - let len = length(v); + let len = length(self); while (i < len) { - f(i, borrow_mut(v, i)); + f(i, borrow_mut(self, i)); i = i + 1; }; } @@ -403,100 +403,100 @@ module aptos_std::smart_vector { /// Fold the function over the Ts. For example, `fold(vector[1,2,3], 0, f)` will execute /// `f(f(f(0, 1), 2), 3)` public inline fun fold( - v: SmartVector, + self: SmartVector, init: Accumulator, f: |Accumulator, T|Accumulator ): Accumulator { let accu = init; - aptos_std::smart_vector::for_each(v, |elem| accu = f(accu, elem)); + aptos_std::smart_vector::for_each(self, |elem| accu = f(accu, elem)); accu } /// Fold right like fold above but working right to left. For example, `fold(vector[1,2,3], 0, f)` will execute /// `f(1, f(2, f(3, 0)))` public inline fun foldr( - v: SmartVector, + self: SmartVector, init: Accumulator, f: |T, Accumulator|Accumulator ): Accumulator { let accu = init; - aptos_std::smart_vector::for_each_reverse(v, |elem| accu = f(elem, accu)); + aptos_std::smart_vector::for_each_reverse(self, |elem| accu = f(elem, accu)); accu } /// Map the function over the references of the Ts of the vector, producing a new vector without modifying the /// original vector. public inline fun map_ref( - v: &SmartVector, + self: &SmartVector, f: |&T1|T2 ): SmartVector { let result = aptos_std::smart_vector::new(); - aptos_std::smart_vector::for_each_ref(v, |elem| aptos_std::smart_vector::push_back(&mut result, f(elem))); + aptos_std::smart_vector::for_each_ref(self, |elem| aptos_std::smart_vector::push_back(&mut result, f(elem))); result } /// Map the function over the Ts of the vector, producing a new vector. public inline fun map( - v: SmartVector, + self: SmartVector, f: |T1|T2 ): SmartVector { let result = aptos_std::smart_vector::new(); - aptos_std::smart_vector::for_each(v, |elem| push_back(&mut result, f(elem))); + aptos_std::smart_vector::for_each(self, |elem| push_back(&mut result, f(elem))); result } /// Filter the vector using the boolean function, removing all Ts for which `p(e)` is not true. public inline fun filter( - v: SmartVector, + self: SmartVector, p: |&T|bool ): SmartVector { let result = aptos_std::smart_vector::new(); - aptos_std::smart_vector::for_each(v, |elem| { + aptos_std::smart_vector::for_each(self, |elem| { if (p(&elem)) aptos_std::smart_vector::push_back(&mut result, elem); }); result } - public inline fun zip(v1: SmartVector, v2: SmartVector, f: |T1, T2|) { + public inline fun zip(self: SmartVector, v2: SmartVector, f: |T1, T2|) { // We need to reverse the vectors to consume it efficiently - aptos_std::smart_vector::reverse(&mut v1); + aptos_std::smart_vector::reverse(&mut self); aptos_std::smart_vector::reverse(&mut v2); - aptos_std::smart_vector::zip_reverse(v1, v2, |e1, e2| f(e1, e2)); + aptos_std::smart_vector::zip_reverse(self, v2, |e1, e2| f(e1, e2)); } /// Apply the function to each pair of elements in the two given vectors in the reverse order, consuming them. /// This errors out if the vectors are not of the same length. public inline fun zip_reverse( - v1: SmartVector, + self: SmartVector, v2: SmartVector, f: |T1, T2|, ) { - let len = aptos_std::smart_vector::length(&v1); + let len = aptos_std::smart_vector::length(&self); // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it // due to how inline functions work. assert!(len == aptos_std::smart_vector::length(&v2), 0x20005); while (len > 0) { - f(aptos_std::smart_vector::pop_back(&mut v1), aptos_std::smart_vector::pop_back(&mut v2)); + f(aptos_std::smart_vector::pop_back(&mut self), aptos_std::smart_vector::pop_back(&mut v2)); len = len - 1; }; - aptos_std::smart_vector::destroy_empty(v1); + aptos_std::smart_vector::destroy_empty(self); aptos_std::smart_vector::destroy_empty(v2); } /// Apply the function to the references of each pair of elements in the two given vectors. /// This errors out if the vectors are not of the same length. public inline fun zip_ref( - v1: &SmartVector, + self: &SmartVector, v2: &SmartVector, f: |&T1, &T2|, ) { - let len = aptos_std::smart_vector::length(v1); + let len = aptos_std::smart_vector::length(self); // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it // due to how inline functions work. assert!(len == aptos_std::smart_vector::length(v2), 0x20005); let i = 0; while (i < len) { - f(aptos_std::smart_vector::borrow(v1, i), aptos_std::smart_vector::borrow(v2, i)); + f(aptos_std::smart_vector::borrow(self, i), aptos_std::smart_vector::borrow(v2, i)); i = i + 1 } } @@ -504,49 +504,49 @@ module aptos_std::smart_vector { /// Apply the function to mutable references to each pair of elements in the two given vectors. /// This errors out if the vectors are not of the same length. public inline fun zip_mut( - v1: &mut SmartVector, + self: &mut SmartVector, v2: &mut SmartVector, f: |&mut T1, &mut T2|, ) { let i = 0; - let len = aptos_std::smart_vector::length(v1); + let len = aptos_std::smart_vector::length(self); // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it // due to how inline functions work. assert!(len == aptos_std::smart_vector::length(v2), 0x20005); while (i < len) { - f(aptos_std::smart_vector::borrow_mut(v1, i), aptos_std::smart_vector::borrow_mut(v2, i)); + f(aptos_std::smart_vector::borrow_mut(self, i), aptos_std::smart_vector::borrow_mut(v2, i)); i = i + 1 } } /// Map the function over the element pairs of the two vectors, producing a new vector. public inline fun zip_map( - v1: SmartVector, + self: SmartVector, v2: SmartVector, f: |T1, T2|NewT ): SmartVector { // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it // due to how inline functions work. - assert!(aptos_std::smart_vector::length(&v1) == aptos_std::smart_vector::length(&v2), 0x20005); + assert!(aptos_std::smart_vector::length(&self) == aptos_std::smart_vector::length(&v2), 0x20005); let result = aptos_std::smart_vector::new(); - aptos_std::smart_vector::zip(v1, v2, |e1, e2| push_back(&mut result, f(e1, e2))); + aptos_std::smart_vector::zip(self, v2, |e1, e2| push_back(&mut result, f(e1, e2))); result } /// Map the function over the references of the element pairs of two vectors, producing a new vector from the return /// values without modifying the original vectors. public inline fun zip_map_ref( - v1: &SmartVector, + self: &SmartVector, v2: &SmartVector, f: |&T1, &T2|NewT ): SmartVector { // We can't use the constant ESMART_VECTORS_LENGTH_MISMATCH here as all calling code would then need to define it // due to how inline functions work. - assert!(aptos_std::smart_vector::length(v1) == aptos_std::smart_vector::length(v2), 0x20005); + assert!(aptos_std::smart_vector::length(self) == aptos_std::smart_vector::length(v2), 0x20005); let result = aptos_std::smart_vector::new(); - aptos_std::smart_vector::zip_ref(v1, v2, |e1, e2| push_back(&mut result, f(e1, e2))); + aptos_std::smart_vector::zip_ref(self, v2, |e1, e2| push_back(&mut result, f(e1, e2))); result } diff --git a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.spec.move b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.spec.move index c1af495eaa9ed..40c77c5b94309 100644 --- a/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/data_structures/smart_vector.spec.move @@ -13,7 +13,8 @@ spec aptos_std::smart_vector { } spec length { - aborts_if option::is_some(v.big_vec) && len(v.inline_vec) + big_vector::length(option::spec_borrow(v.big_vec)) > MAX_U64; + aborts_if option::is_some(self.big_vec) && len(self.inline_vec) + big_vector::length(option::spec_borrow( + self.big_vec)) > MAX_U64; } spec empty { @@ -25,19 +26,19 @@ spec aptos_std::smart_vector { } spec destroy_empty { - aborts_if !(is_empty(v)); - aborts_if len(v.inline_vec) != 0 - || option::is_some(v.big_vec); + aborts_if !(is_empty(self)); + aborts_if len(self.inline_vec) != 0 + || option::is_some(self.big_vec); } spec borrow { - aborts_if i >= length(v); - aborts_if option::is_some(v.big_vec) && ( - (len(v.inline_vec) + big_vector::length(option::borrow(v.big_vec))) > MAX_U64 + aborts_if i >= length(self); + aborts_if option::is_some(self.big_vec) && ( + (len(self.inline_vec) + big_vector::length(option::borrow(self.big_vec))) > MAX_U64 ); } - spec push_back(v: &mut SmartVector, val: T) { + spec push_back(self: &mut SmartVector, val: T) { // use aptos_std::big_vector; // use aptos_std::type_info; pragma verify = false; // TODO: set to false because of timeout @@ -65,24 +66,24 @@ spec aptos_std::smart_vector { pragma verify_duration_estimate = 120; // TODO: set because of timeout (property proved) - aborts_if option::is_some(v.big_vec) + aborts_if option::is_some(self.big_vec) && - (table_with_length::spec_len(option::borrow(v.big_vec).buckets) == 0); - aborts_if is_empty(v); - aborts_if option::is_some(v.big_vec) && ( - (len(v.inline_vec) + big_vector::length(option::borrow(v.big_vec))) > MAX_U64 + (table_with_length::spec_len(option::borrow(self.big_vec).buckets) == 0); + aborts_if is_empty(self); + aborts_if option::is_some(self.big_vec) && ( + (len(self.inline_vec) + big_vector::length(option::borrow(self.big_vec))) > MAX_U64 ); - ensures length(v) == length(old(v)) - 1; + ensures length(self) == length(old(self)) - 1; } spec swap_remove { pragma verify = false; // TODO: set because of timeout - aborts_if i >= length(v); - aborts_if option::is_some(v.big_vec) && ( - (len(v.inline_vec) + big_vector::length(option::borrow(v.big_vec))) > MAX_U64 + aborts_if i >= length(self); + aborts_if option::is_some(self.big_vec) && ( + (len(self.inline_vec) + big_vector::length(option::borrow(self.big_vec))) > MAX_U64 ); - ensures length(v) == length(old(v)) - 1; + ensures length(self) == length(old(self)) - 1; } spec swap { diff --git a/aptos-move/framework/aptos-stdlib/sources/fixed_point64.move b/aptos-move/framework/aptos-stdlib/sources/fixed_point64.move index b24ec894e6c3f..dcf464b025afd 100644 --- a/aptos-move/framework/aptos-stdlib/sources/fixed_point64.move +++ b/aptos-move/framework/aptos-stdlib/sources/fixed_point64.move @@ -29,22 +29,22 @@ module aptos_std::fixed_point64 { /// Abort code on calculation result is negative. const ENEGATIVE_RESULT: u64 = 0x10006; - /// Returns x - y. x must be not less than y. - public fun sub(x: FixedPoint64, y: FixedPoint64): FixedPoint64 { - let x_raw = get_raw_value(x); + /// Returns self - y. self must be not less than y. + public fun sub(self: FixedPoint64, y: FixedPoint64): FixedPoint64 { + let x_raw = get_raw_value(self); let y_raw = get_raw_value(y); assert!(x_raw >= y_raw, ENEGATIVE_RESULT); create_from_raw_value(x_raw - y_raw) } spec sub { pragma opaque; - aborts_if x.value < y.value with ENEGATIVE_RESULT; - ensures result.value == x.value - y.value; + aborts_if self.value < y.value with ENEGATIVE_RESULT; + ensures result.value == self.value - y.value; } - /// Returns x + y. The result cannot be greater than MAX_U128. - public fun add(x: FixedPoint64, y: FixedPoint64): FixedPoint64 { - let x_raw = get_raw_value(x); + /// Returns self + y. The result cannot be greater than MAX_U128. + public fun add(self: FixedPoint64, y: FixedPoint64): FixedPoint64 { + let x_raw = get_raw_value(self); let y_raw = get_raw_value(y); let result = (x_raw as u256) + (y_raw as u256); assert!(result <= MAX_U128, ERATIO_OUT_OF_RANGE); @@ -52,8 +52,8 @@ module aptos_std::fixed_point64 { } spec add { pragma opaque; - aborts_if (x.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE; - ensures result.value == x.value + y.value; + aborts_if (self.value as u256) + (y.value as u256) > MAX_U128 with ERATIO_OUT_OF_RANGE; + ensures result.value == self.value + y.value; } /// Multiply a u128 integer by a fixed-point number, truncating any @@ -94,7 +94,7 @@ module aptos_std::fixed_point64 { assert!(unscaled_product <= MAX_U128, EMULTIPLICATION); create_from_raw_value((unscaled_product as u128)) } - + /// Divide a u128 integer by a fixed-point number, truncating any /// fractional part of the quotient. This will abort if the divisor @@ -183,13 +183,13 @@ module aptos_std::fixed_point64 { /// Accessor for the raw u128 value. Other less common operations, such as /// adding or subtracting FixedPoint64 values, can be done using the raw /// values directly. - public fun get_raw_value(num: FixedPoint64): u128 { - num.value + public fun get_raw_value(self: FixedPoint64): u128 { + self.value } /// Returns true if the ratio is zero. - public fun is_zero(num: FixedPoint64): bool { - num.value == 0 + public fun is_zero(self: FixedPoint64): bool { + self.value == 0 } /// Returns the smaller of the two FixedPoint64 numbers. @@ -234,89 +234,89 @@ module aptos_std::fixed_point64 { } } - /// Returns true if num1 <= num2 - public fun less_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value <= num2.value + /// Returns true if self <= num2 + public fun less_or_equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value <= num2.value } spec less_or_equal { pragma opaque; aborts_if false; - ensures result == spec_less_or_equal(num1, num2); + ensures result == spec_less_or_equal(self, num2); } - spec fun spec_less_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value <= num2.value + spec fun spec_less_or_equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value <= num2.value } - /// Returns true if num1 < num2 - public fun less(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value < num2.value + /// Returns true if self < num2 + public fun less(self: FixedPoint64, num2: FixedPoint64): bool { + self.value < num2.value } spec less { pragma opaque; aborts_if false; - ensures result == spec_less(num1, num2); + ensures result == spec_less(self, num2); } - spec fun spec_less(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value < num2.value + spec fun spec_less(self: FixedPoint64, num2: FixedPoint64): bool { + self.value < num2.value } - /// Returns true if num1 >= num2 - public fun greater_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value >= num2.value + /// Returns true if self >= num2 + public fun greater_or_equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value >= num2.value } spec greater_or_equal { pragma opaque; aborts_if false; - ensures result == spec_greater_or_equal(num1, num2); + ensures result == spec_greater_or_equal(self, num2); } - spec fun spec_greater_or_equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value >= num2.value + spec fun spec_greater_or_equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value >= num2.value } - /// Returns true if num1 > num2 - public fun greater(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value > num2.value + /// Returns true if self > num2 + public fun greater(self: FixedPoint64, num2: FixedPoint64): bool { + self.value > num2.value } spec greater { pragma opaque; aborts_if false; - ensures result == spec_greater(num1, num2); + ensures result == spec_greater(self, num2); } - spec fun spec_greater(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value > num2.value + spec fun spec_greater(self: FixedPoint64, num2: FixedPoint64): bool { + self.value > num2.value } - /// Returns true if num1 = num2 - public fun equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value == num2.value + /// Returns true if self = num2 + public fun equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value == num2.value } spec equal { pragma opaque; aborts_if false; - ensures result == spec_equal(num1, num2); + ensures result == spec_equal(self, num2); } - spec fun spec_equal(num1: FixedPoint64, num2: FixedPoint64): bool { - num1.value == num2.value + spec fun spec_equal(self: FixedPoint64, num2: FixedPoint64): bool { + self.value == num2.value } - /// Returns true if num1 almost equals to num2, which means abs(num1-num2) <= precision - public fun almost_equal(num1: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool { - if (num1.value > num2.value) { - (num1.value - num2.value <= precision.value) + /// Returns true if self almost equals to num2, which means abs(num1-num2) <= precision + public fun almost_equal(self: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool { + if (self.value > num2.value) { + (self.value - num2.value <= precision.value) } else { - (num2.value - num1.value <= precision.value) + (num2.value - self.value <= precision.value) } } spec almost_equal { pragma opaque; aborts_if false; - ensures result == spec_almost_equal(num1, num2, precision); + ensures result == spec_almost_equal(self, num2, precision); } - spec fun spec_almost_equal(num1: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool { - if (num1.value > num2.value) { - (num1.value - num2.value <= precision.value) + spec fun spec_almost_equal(self: FixedPoint64, num2: FixedPoint64, precision: FixedPoint64): bool { + if (self.value > num2.value) { + (self.value - num2.value <= precision.value) } else { - (num2.value - num1.value <= precision.value) + (num2.value - self.value <= precision.value) } } /// Create a fixedpoint value from a u128 value. @@ -340,27 +340,27 @@ module aptos_std::fixed_point64 { } /// Returns the largest integer less than or equal to a given number. - public fun floor(num: FixedPoint64): u128 { - num.value >> 64 + public fun floor(self: FixedPoint64): u128 { + self.value >> 64 } spec floor { pragma opaque; aborts_if false; - ensures result == spec_floor(num); + ensures result == spec_floor(self); } - spec fun spec_floor(val: FixedPoint64): u128 { - let fractional = val.value % (1 << 64); + spec fun spec_floor(self: FixedPoint64): u128 { + let fractional = self.value % (1 << 64); if (fractional == 0) { - val.value >> 64 + self.value >> 64 } else { - (val.value - fractional) >> 64 + (self.value - fractional) >> 64 } } /// Rounds up the given FixedPoint64 to the next largest integer. - public fun ceil(num: FixedPoint64): u128 { - let floored_num = floor(num) << 64; - if (num.value == floored_num) { + public fun ceil(self: FixedPoint64): u128 { + let floored_num = floor(self) << 64; + if (self.value == floored_num) { return floored_num >> 64 }; let val = ((floored_num as u256) + (1 << 64)); @@ -371,41 +371,41 @@ module aptos_std::fixed_point64 { pragma verify_duration_estimate = 1000; pragma opaque; aborts_if false; - ensures result == spec_ceil(num); + ensures result == spec_ceil(self); } - spec fun spec_ceil(val: FixedPoint64): u128 { - let fractional = val.value % (1 << 64); + spec fun spec_ceil(self: FixedPoint64): u128 { + let fractional = self.value % (1 << 64); let one = 1 << 64; if (fractional == 0) { - val.value >> 64 + self.value >> 64 } else { - (val.value - fractional + one) >> 64 + (self.value - fractional + one) >> 64 } } /// Returns the value of a FixedPoint64 to the nearest integer. - public fun round(num: FixedPoint64): u128 { - let floored_num = floor(num) << 64; + public fun round(self: FixedPoint64): u128 { + let floored_num = floor(self) << 64; let boundary = floored_num + ((1 << 64) / 2); - if (num.value < boundary) { + if (self.value < boundary) { floored_num >> 64 } else { - ceil(num) + ceil(self) } } spec round { pragma opaque; aborts_if false; - ensures result == spec_round(num); + ensures result == spec_round(self); } - spec fun spec_round(val: FixedPoint64): u128 { - let fractional = val.value % (1 << 64); + spec fun spec_round(self: FixedPoint64): u128 { + let fractional = self.value % (1 << 64); let boundary = (1 << 64) / 2; let one = 1 << 64; if (fractional < boundary) { - (val.value - fractional) >> 64 + (self.value - fractional) >> 64 } else { - (val.value - fractional + one) >> 64 + (self.value - fractional + one) >> 64 } } diff --git a/aptos-move/framework/aptos-stdlib/sources/from_bcs.move b/aptos-move/framework/aptos-stdlib/sources/from_bcs.move index 1d7c3c542ee60..3eb6e2177d2f4 100644 --- a/aptos-move/framework/aptos-stdlib/sources/from_bcs.move +++ b/aptos-move/framework/aptos-stdlib/sources/from_bcs.move @@ -1,6 +1,6 @@ /// This module provides a number of functions to convert _primitive_ types from their representation in `std::bcs` /// to values. This is the opposite of `bcs::to_bytes`. Note that it is not safe to define a generic public `from_bytes` -/// function because this can violate implicit struct invariants, therefore only primitive types are offerred. If +/// function because this can violate implicit struct invariants, therefore only primitive types are offered. If /// a general conversion back-and-force is needed, consider the `aptos_std::Any` type which preserves invariants. /// /// Example: diff --git a/aptos-move/framework/aptos-stdlib/sources/math128.move b/aptos-move/framework/aptos-stdlib/sources/math128.move index 6528153699fb4..df239dbca0846 100644 --- a/aptos-move/framework/aptos-stdlib/sources/math128.move +++ b/aptos-move/framework/aptos-stdlib/sources/math128.move @@ -39,6 +39,15 @@ module aptos_std::math128 { large } + /// Return least common multiple of `a` & `b` + public inline fun lcm(a: u128, b: u128): u128 { + if (a == 0 || b == 0) { + 0 + } else { + a / gcd(a, b) * b + } + } + /// Returns a * b / c going through u256 to prevent intermediate overflow public inline fun mul_div(a: u128, b: u128, c: u128): u128 { // Inline functions cannot take constants, as then every module using it needs the constant @@ -193,6 +202,28 @@ module aptos_std::math128 { assert!(gcd(462, 1071) == 21, 0); } + #[test] + fun test_lcm() { + assert!(lcm(0, 0) == 0, 0); + assert!(lcm(0, 1) == 0, 0); + assert!(lcm(1, 0) == 0, 0); + assert!(lcm(1, 1) == 1, 0); + assert!(lcm(1024, 144) == 9216, 0); + assert!(lcm(2, 17) == 34, 0); + assert!(lcm(17, 2) == 34, 0); + assert!(lcm(24, 54) == 216, 0); + assert!(lcm(115, 9) == 1035, 0); + assert!(lcm(101, 14) == 1414, 0); + assert!(lcm(110, 5) == 110, 0); + assert!(lcm(100, 8) == 200, 0); + assert!(lcm(32, 6) == 96, 0); + assert!(lcm(110, 13) == 1430, 0); + assert!(lcm(117, 13) == 117, 0); + assert!(lcm(100, 125) == 500, 0); + assert!(lcm(101, 3) == 303, 0); + assert!(lcm(115, 16) == 1840, 0); + } + #[test] public entry fun test_max() { let result = max(3u128, 6u128); diff --git a/aptos-move/framework/aptos-stdlib/sources/math64.move b/aptos-move/framework/aptos-stdlib/sources/math64.move index 50fd38ed3f6ab..88cd90a68f605 100644 --- a/aptos-move/framework/aptos-stdlib/sources/math64.move +++ b/aptos-move/framework/aptos-stdlib/sources/math64.move @@ -37,6 +37,15 @@ module aptos_std::math64 { large } + /// Returns least common multiple of `a` & `b`. + public inline fun lcm(a: u64, b: u64): u64 { + if (a == 0 || b == 0) { + 0 + } else { + a / gcd(a, b) * b + } + } + /// Returns a * b / c going through u128 to prevent intermediate overflow public inline fun mul_div(a: u64, b: u64, c: u64): u64 { // Inline functions cannot take constants, as then every module using it needs the constant @@ -167,6 +176,28 @@ module aptos_std::math64 { assert!(gcd(462, 1071) == 21, 0); } + #[test] + fun test_lcm() { + assert!(lcm(0, 0) == 0, 0); + assert!(lcm(0, 1) == 0, 0); + assert!(lcm(1, 0) == 0, 0); + assert!(lcm(1, 1) == 1, 0); + assert!(lcm(1024, 144) == 9216, 0); + assert!(lcm(2, 17) == 34, 0); + assert!(lcm(17, 2) == 34, 0); + assert!(lcm(24, 54) == 216, 0); + assert!(lcm(115, 9) == 1035, 0); + assert!(lcm(101, 14) == 1414, 0); + assert!(lcm(110, 5) == 110, 0); + assert!(lcm(100, 8) == 200, 0); + assert!(lcm(32, 6) == 96, 0); + assert!(lcm(110, 13) == 1430, 0); + assert!(lcm(117, 13) == 117, 0); + assert!(lcm(100, 125) == 500, 0); + assert!(lcm(101, 3) == 303, 0); + assert!(lcm(115, 16) == 1840, 0); + } + #[test] public entry fun test_max_64() { let result = max(3u64, 6u64); diff --git a/aptos-move/framework/aptos-stdlib/sources/pool_u64.move b/aptos-move/framework/aptos-stdlib/sources/pool_u64.move index f1aaea9fd947f..7d049fdf37133 100644 --- a/aptos-move/framework/aptos-stdlib/sources/pool_u64.move +++ b/aptos-move/framework/aptos-stdlib/sources/pool_u64.move @@ -72,8 +72,8 @@ module aptos_std::pool_u64 { } /// Destroy an empty pool. This will fail if the pool has any balance of coins. - public fun destroy_empty(pool: Pool) { - assert!(pool.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY)); + public fun destroy_empty(self: Pool) { + assert!(self.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY)); let Pool { shareholders_limit: _, total_coins: _, @@ -81,73 +81,73 @@ module aptos_std::pool_u64 { shares: _, shareholders: _, scaling_factor: _, - } = pool; + } = self; } - /// Return `pool`'s total balance of coins. - public fun total_coins(pool: &Pool): u64 { - pool.total_coins + /// Return `self`'s total balance of coins. + public fun total_coins(self: &Pool): u64 { + self.total_coins } - /// Return the total number of shares across all shareholders in `pool`. - public fun total_shares(pool: &Pool): u64 { - pool.total_shares + /// Return the total number of shares across all shareholders in `self`. + public fun total_shares(self: &Pool): u64 { + self.total_shares } - /// Return true if `shareholder` is in `pool`. - public fun contains(pool: &Pool, shareholder: address): bool { - simple_map::contains_key(&pool.shares, &shareholder) + /// Return true if `shareholder` is in `self`. + public fun contains(self: &Pool, shareholder: address): bool { + simple_map::contains_key(&self.shares, &shareholder) } - /// Return the number of shares of `stakeholder` in `pool`. - public fun shares(pool: &Pool, shareholder: address): u64 { - if (contains(pool, shareholder)) { - *simple_map::borrow(&pool.shares, &shareholder) + /// Return the number of shares of `stakeholder` in `self`. + public fun shares(self: &Pool, shareholder: address): u64 { + if (contains(self, shareholder)) { + *simple_map::borrow(&self.shares, &shareholder) } else { 0 } } - /// Return the balance in coins of `shareholder` in `pool.` - public fun balance(pool: &Pool, shareholder: address): u64 { - let num_shares = shares(pool, shareholder); - shares_to_amount(pool, num_shares) + /// Return the balance in coins of `shareholder` in `self`. + public fun balance(self: &Pool, shareholder: address): u64 { + let num_shares = shares(self, shareholder); + shares_to_amount(self, num_shares) } - /// Return the list of shareholders in `pool`. - public fun shareholders(pool: &Pool): vector
{ - pool.shareholders + /// Return the list of shareholders in `self`. + public fun shareholders(self: &Pool): vector
{ + self.shareholders } - /// Return the number of shareholders in `pool`. - public fun shareholders_count(pool: &Pool): u64 { - vector::length(&pool.shareholders) + /// Return the number of shareholders in `self`. + public fun shareholders_count(self: &Pool): u64 { + vector::length(&self.shareholders) } - /// Update `pool`'s total balance of coins. - public fun update_total_coins(pool: &mut Pool, new_total_coins: u64) { - pool.total_coins = new_total_coins; + /// Update `self`'s total balance of coins. + public fun update_total_coins(self: &mut Pool, new_total_coins: u64) { + self.total_coins = new_total_coins; } /// Allow an existing or new shareholder to add their coins to the pool in exchange for new shares. - public fun buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u64 { + public fun buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u64 { if (coins_amount == 0) return 0; - let new_shares = amount_to_shares(pool, coins_amount); - assert!(MAX_U64 - pool.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); - assert!(MAX_U64 - pool.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); + let new_shares = amount_to_shares(self, coins_amount); + assert!(MAX_U64 - self.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); + assert!(MAX_U64 - self.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); - pool.total_coins = pool.total_coins + coins_amount; - pool.total_shares = pool.total_shares + new_shares; - add_shares(pool, shareholder, new_shares); + self.total_coins = self.total_coins + coins_amount; + self.total_shares = self.total_shares + new_shares; + add_shares(self, shareholder, new_shares); new_shares } - /// Add the number of shares directly for `shareholder` in `pool`. + /// Add the number of shares directly for `shareholder` in `self`. /// This would dilute other shareholders if the pool's balance of coins didn't change. - fun add_shares(pool: &mut Pool, shareholder: address, new_shares: u64): u64 { - if (contains(pool, shareholder)) { - let existing_shares = simple_map::borrow_mut(&mut pool.shares, &shareholder); + fun add_shares(self: &mut Pool, shareholder: address, new_shares: u64): u64 { + if (contains(self, shareholder)) { + let existing_shares = simple_map::borrow_mut(&mut self.shares, &shareholder); let current_shares = *existing_shares; assert!(MAX_U64 - current_shares >= new_shares, error::invalid_argument(ESHAREHOLDER_SHARES_OVERFLOW)); @@ -155,110 +155,110 @@ module aptos_std::pool_u64 { *existing_shares } else if (new_shares > 0) { assert!( - vector::length(&pool.shareholders) < pool.shareholders_limit, + vector::length(&self.shareholders) < self.shareholders_limit, error::invalid_state(ETOO_MANY_SHAREHOLDERS), ); - vector::push_back(&mut pool.shareholders, shareholder); - simple_map::add(&mut pool.shares, shareholder, new_shares); + vector::push_back(&mut self.shareholders, shareholder); + simple_map::add(&mut self.shares, shareholder, new_shares); new_shares } else { new_shares } } - /// Allow `shareholder` to redeem their shares in `pool` for coins. - public fun redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 { - assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES)); + /// Allow `shareholder` to redeem their shares in `self` for coins. + public fun redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 { + assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES)); if (shares_to_redeem == 0) return 0; - let redeemed_coins = shares_to_amount(pool, shares_to_redeem); - pool.total_coins = pool.total_coins - redeemed_coins; - pool.total_shares = pool.total_shares - shares_to_redeem; - deduct_shares(pool, shareholder, shares_to_redeem); + let redeemed_coins = shares_to_amount(self, shares_to_redeem); + self.total_coins = self.total_coins - redeemed_coins; + self.total_shares = self.total_shares - shares_to_redeem; + deduct_shares(self, shareholder, shares_to_redeem); redeemed_coins } /// Transfer shares from `shareholder_1` to `shareholder_2`. public fun transfer_shares( - pool: &mut Pool, + self: &mut Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64, ) { - assert!(contains(pool, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES)); + assert!(contains(self, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES)); if (shares_to_transfer == 0) return; - deduct_shares(pool, shareholder_1, shares_to_transfer); - add_shares(pool, shareholder_2, shares_to_transfer); + deduct_shares(self, shareholder_1, shares_to_transfer); + add_shares(self, shareholder_2, shares_to_transfer); } - /// Directly deduct `shareholder`'s number of shares in `pool` and return the number of remaining shares. - fun deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u64): u64 { - assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES)); + /// Directly deduct `shareholder`'s number of shares in `self` and return the number of remaining shares. + fun deduct_shares(self: &mut Pool, shareholder: address, num_shares: u64): u64 { + assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES)); - let existing_shares = simple_map::borrow_mut(&mut pool.shares, &shareholder); + let existing_shares = simple_map::borrow_mut(&mut self.shares, &shareholder); *existing_shares = *existing_shares - num_shares; // Remove the shareholder completely if they have no shares left. let remaining_shares = *existing_shares; if (remaining_shares == 0) { - let (_, shareholder_index) = vector::index_of(&pool.shareholders, &shareholder); - vector::remove(&mut pool.shareholders, shareholder_index); - simple_map::remove(&mut pool.shares, &shareholder); + let (_, shareholder_index) = vector::index_of(&self.shareholders, &shareholder); + vector::remove(&mut self.shareholders, shareholder_index); + simple_map::remove(&mut self.shares, &shareholder); }; remaining_shares } - /// Return the number of new shares `coins_amount` can buy in `pool`. + /// Return the number of new shares `coins_amount` can buy in `self`. /// `amount` needs to big enough to avoid rounding number. - public fun amount_to_shares(pool: &Pool, coins_amount: u64): u64 { - amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins) + public fun amount_to_shares(self: &Pool, coins_amount: u64): u64 { + amount_to_shares_with_total_coins(self, coins_amount, self.total_coins) } - /// Return the number of new shares `coins_amount` can buy in `pool` with a custom total coins number. + /// Return the number of new shares `coins_amount` can buy in `self` with a custom total coins number. /// `amount` needs to big enough to avoid rounding number. - public fun amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u64 { + public fun amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u64 { // No shares yet so amount is worth the same number of shares. - if (pool.total_coins == 0 || pool.total_shares == 0) { + if (self.total_coins == 0 || self.total_shares == 0) { // Multiply by scaling factor to minimize rounding errors during internal calculations for buy ins/redeems. // This can overflow but scaling factor is expected to be chosen carefully so this would not overflow. - coins_amount * pool.scaling_factor + coins_amount * self.scaling_factor } else { // Shares price = total_coins / total existing shares. // New number of shares = new_amount / shares_price = new_amount * existing_shares / total_amount. // We rearrange the calc and do multiplication first to avoid rounding errors. - multiply_then_divide(pool, coins_amount, pool.total_shares, total_coins) + multiply_then_divide(self, coins_amount, self.total_shares, total_coins) } } - /// Return the number of coins `shares` are worth in `pool`. + /// Return the number of coins `shares` are worth in `self`. /// `shares` needs to big enough to avoid rounding number. - public fun shares_to_amount(pool: &Pool, shares: u64): u64 { - shares_to_amount_with_total_coins(pool, shares, pool.total_coins) + public fun shares_to_amount(self: &Pool, shares: u64): u64 { + shares_to_amount_with_total_coins(self, shares, self.total_coins) } - /// Return the number of coins `shares` are worth in `pool` with a custom total coins number. + /// Return the number of coins `shares` are worth in `self` with a custom total coins number. /// `shares` needs to big enough to avoid rounding number. - public fun shares_to_amount_with_total_coins(pool: &Pool, shares: u64, total_coins: u64): u64 { + public fun shares_to_amount_with_total_coins(self: &Pool, shares: u64, total_coins: u64): u64 { // No shares or coins yet so shares are worthless. - if (pool.total_coins == 0 || pool.total_shares == 0) { + if (self.total_coins == 0 || self.total_shares == 0) { 0 } else { // Shares price = total_coins / total existing shares. // Shares worth = shares * shares price = shares * total_coins / total existing shares. // We rearrange the calc and do multiplication first to avoid rounding errors. - multiply_then_divide(pool, shares, total_coins, pool.total_shares) + multiply_then_divide(self, shares, total_coins, self.total_shares) } } - public fun multiply_then_divide(_pool: &Pool, x: u64, y: u64, z: u64): u64 { + public fun multiply_then_divide(self: &Pool, x: u64, y: u64, z: u64): u64 { let result = (to_u128(x) * to_u128(y)) / to_u128(z); (result as u64) } @@ -268,7 +268,7 @@ module aptos_std::pool_u64 { } #[test_only] - public fun destroy_pool(pool: Pool) { + public fun destroy_pool(self: Pool) { let Pool { shareholders_limit: _, total_coins: _, @@ -276,7 +276,7 @@ module aptos_std::pool_u64 { shares: _, shareholders: _, scaling_factor: _, - } = pool; + } = self; } #[test] diff --git a/aptos-move/framework/aptos-stdlib/sources/pool_u64.spec.move b/aptos-move/framework/aptos-stdlib/sources/pool_u64.spec.move index 96e77747769e4..3ad0b4b6af1fc 100644 --- a/aptos-move/framework/aptos-stdlib/sources/pool_u64.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/pool_u64.spec.move @@ -30,9 +30,9 @@ spec aptos_std::pool_u64 { simple_map::spec_contains_key(pool.shares, shareholder) } - spec contains(pool: &Pool, shareholder: address): bool { + spec contains(self: &Pool, shareholder: address): bool { aborts_if false; - ensures result == spec_contains(pool, shareholder); + ensures result == spec_contains(self, shareholder); } spec fun spec_shares(pool: Pool, shareholder: address): u64 { @@ -44,62 +44,62 @@ spec aptos_std::pool_u64 { } } - spec shares(pool: &Pool, shareholder: address): u64 { + spec shares(self: &Pool, shareholder: address): u64 { aborts_if false; - ensures result == spec_shares(pool, shareholder); + ensures result == spec_shares(self, shareholder); } - spec balance(pool: &Pool, shareholder: address): u64 { - let shares = spec_shares(pool, shareholder); - let total_coins = pool.total_coins; - aborts_if pool.total_coins > 0 && pool.total_shares > 0 && (shares * total_coins) / pool.total_shares > MAX_U64; - ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins); + spec balance(self: &Pool, shareholder: address): u64 { + let shares = spec_shares(self, shareholder); + let total_coins = self.total_coins; + aborts_if self.total_coins > 0 && self.total_shares > 0 && (shares * total_coins) / self.total_shares > MAX_U64; + ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins); } - spec buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u64 { - let new_shares = spec_amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins); - aborts_if pool.total_coins + coins_amount > MAX_U64; - aborts_if pool.total_shares + new_shares > MAX_U64; + spec buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u64 { + let new_shares = spec_amount_to_shares_with_total_coins(self, coins_amount, self.total_coins); + aborts_if self.total_coins + coins_amount > MAX_U64; + aborts_if self.total_shares + new_shares > MAX_U64; include coins_amount > 0 ==> AddSharesAbortsIf { new_shares: new_shares }; include coins_amount > 0 ==> AddSharesEnsures { new_shares: new_shares }; - ensures pool.total_coins == old(pool.total_coins) + coins_amount; - ensures pool.total_shares == old(pool.total_shares) + new_shares; + ensures self.total_coins == old(self.total_coins) + coins_amount; + ensures self.total_shares == old(self.total_shares) + new_shares; ensures result == new_shares; } - spec add_shares(pool: &mut Pool, shareholder: address, new_shares: u64): u64 { + spec add_shares(self: &mut Pool, shareholder: address, new_shares: u64): u64 { include AddSharesAbortsIf; include AddSharesEnsures; - let key_exists = simple_map::spec_contains_key(pool.shares, shareholder); - ensures result == if (key_exists) { simple_map::spec_get(pool.shares, shareholder) } + let key_exists = simple_map::spec_contains_key(self.shares, shareholder); + ensures result == if (key_exists) { simple_map::spec_get(self.shares, shareholder) } else { new_shares }; } spec schema AddSharesAbortsIf { - pool: Pool; + self: Pool; shareholder: address; new_shares: u64; - let key_exists = simple_map::spec_contains_key(pool.shares, shareholder); - let current_shares = simple_map::spec_get(pool.shares, shareholder); + let key_exists = simple_map::spec_contains_key(self.shares, shareholder); + let current_shares = simple_map::spec_get(self.shares, shareholder); aborts_if key_exists && current_shares + new_shares > MAX_U64; - aborts_if !key_exists && new_shares > 0 && len(pool.shareholders) >= pool.shareholders_limit; + aborts_if !key_exists && new_shares > 0 && len(self.shareholders) >= self.shareholders_limit; } spec schema AddSharesEnsures { - pool: Pool; + self: Pool; shareholder: address; new_shares: u64; - let key_exists = simple_map::spec_contains_key(pool.shares, shareholder); - let current_shares = simple_map::spec_get(pool.shares, shareholder); + let key_exists = simple_map::spec_contains_key(self.shares, shareholder); + let current_shares = simple_map::spec_get(self.shares, shareholder); ensures key_exists ==> - pool.shares == simple_map::spec_set(old(pool.shares), shareholder, current_shares + new_shares); + self.shares == simple_map::spec_set(old(self.shares), shareholder, current_shares + new_shares); ensures (!key_exists && new_shares > 0) ==> - pool.shares == simple_map::spec_set(old(pool.shares), shareholder, new_shares); + self.shares == simple_map::spec_set(old(self.shares), shareholder, new_shares); ensures (!key_exists && new_shares > 0) ==> - vector::eq_push_back(pool.shareholders, old(pool.shareholders), shareholder); + vector::eq_push_back(self.shareholders, old(self.shareholders), shareholder); } spec fun spec_amount_to_shares_with_total_coins(pool: Pool, coins_amount: u64, total_coins: u64): u64 { @@ -111,19 +111,19 @@ spec aptos_std::pool_u64 { } } - spec amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u64 { - aborts_if pool.total_coins > 0 && pool.total_shares > 0 - && (coins_amount * pool.total_shares) / total_coins > MAX_U64; - aborts_if (pool.total_coins == 0 || pool.total_shares == 0) - && coins_amount * pool.scaling_factor > MAX_U64; - aborts_if pool.total_coins > 0 && pool.total_shares > 0 && total_coins == 0; - ensures result == spec_amount_to_shares_with_total_coins(pool, coins_amount, total_coins); + spec amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u64 { + aborts_if self.total_coins > 0 && self.total_shares > 0 + && (coins_amount * self.total_shares) / total_coins > MAX_U64; + aborts_if (self.total_coins == 0 || self.total_shares == 0) + && coins_amount * self.scaling_factor > MAX_U64; + aborts_if self.total_coins > 0 && self.total_shares > 0 && total_coins == 0; + ensures result == spec_amount_to_shares_with_total_coins(self, coins_amount, total_coins); } - spec shares_to_amount_with_total_coins(pool: &Pool, shares: u64, total_coins: u64): u64 { - aborts_if pool.total_coins > 0 && pool.total_shares > 0 - && (shares * total_coins) / pool.total_shares > MAX_U64; - ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins); + spec shares_to_amount_with_total_coins(self: &Pool, shares: u64, total_coins: u64): u64 { + aborts_if self.total_coins > 0 && self.total_shares > 0 + && (shares * total_coins) / self.total_shares > MAX_U64; + ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins); } spec fun spec_shares_to_amount_with_total_coins(pool: Pool, shares: u64, total_coins: u64): u64 { @@ -135,52 +135,54 @@ spec aptos_std::pool_u64 { } } - spec multiply_then_divide(_pool: &Pool, x: u64, y: u64, z: u64): u64 { + spec multiply_then_divide(self: &Pool, x: u64, y: u64, z: u64): u64 { aborts_if z == 0; aborts_if (x * y) / z > MAX_U64; ensures result == (x * y) / z; } - spec redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 { - let redeemed_coins = spec_shares_to_amount_with_total_coins(pool, shares_to_redeem, pool.total_coins); - aborts_if !spec_contains(pool, shareholder); - aborts_if spec_shares(pool, shareholder) < shares_to_redeem; - aborts_if pool.total_coins < redeemed_coins; - aborts_if pool.total_shares < shares_to_redeem; - ensures pool.total_coins == old(pool.total_coins) - redeemed_coins; - ensures pool.total_shares == old(pool.total_shares) - shares_to_redeem; - include shares_to_redeem > 0 ==> DeductSharesEnsures { num_shares: shares_to_redeem }; + spec redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u64): u64 { + let redeemed_coins = spec_shares_to_amount_with_total_coins(self, shares_to_redeem, self.total_coins); + aborts_if !spec_contains(self, shareholder); + aborts_if spec_shares(self, shareholder) < shares_to_redeem; + aborts_if self.total_coins < redeemed_coins; + aborts_if self.total_shares < shares_to_redeem; + ensures self.total_coins == old(self.total_coins) - redeemed_coins; + ensures self.total_shares == old(self.total_shares) - shares_to_redeem; + include shares_to_redeem > 0 ==> DeductSharesEnsures { + num_shares: shares_to_redeem + }; ensures result == redeemed_coins; } spec transfer_shares( - pool: &mut Pool, + self: &mut Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u64 ) { pragma aborts_if_is_partial; - aborts_if !spec_contains(pool, shareholder_1); - aborts_if spec_shares(pool, shareholder_1) < shares_to_transfer; + aborts_if !spec_contains(self, shareholder_1); + aborts_if spec_shares(self, shareholder_1) < shares_to_transfer; // TODO: difficult to specify due to the intermediate state problem. } - spec deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u64): u64 { - aborts_if !spec_contains(pool, shareholder); - aborts_if spec_shares(pool, shareholder) < num_shares; + spec deduct_shares(self: &mut Pool, shareholder: address, num_shares: u64): u64 { + aborts_if !spec_contains(self, shareholder); + aborts_if spec_shares(self, shareholder) < num_shares; include DeductSharesEnsures; - let remaining_shares = simple_map::spec_get(pool.shares, shareholder) - num_shares; - ensures remaining_shares > 0 ==> result == simple_map::spec_get(pool.shares, shareholder); + let remaining_shares = simple_map::spec_get(self.shares, shareholder) - num_shares; + ensures remaining_shares > 0 ==> result == simple_map::spec_get(self.shares, shareholder); ensures remaining_shares == 0 ==> result == 0; } spec schema DeductSharesEnsures { - pool: Pool; + self: Pool; shareholder: address; num_shares: u64; - let remaining_shares = simple_map::spec_get(pool.shares, shareholder) - num_shares; - ensures remaining_shares > 0 ==> simple_map::spec_get(pool.shares, shareholder) == remaining_shares; - ensures remaining_shares == 0 ==> !simple_map::spec_contains_key(pool.shares, shareholder); - ensures remaining_shares == 0 ==> !vector::spec_contains(pool.shareholders, shareholder); + let remaining_shares = simple_map::spec_get(self.shares, shareholder) - num_shares; + ensures remaining_shares > 0 ==> simple_map::spec_get(self.shares, shareholder) == remaining_shares; + ensures remaining_shares == 0 ==> !simple_map::spec_contains_key(self.shares, shareholder); + ensures remaining_shares == 0 ==> !vector::spec_contains(self.shareholders, shareholder); } } diff --git a/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.move b/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.move index c9ab78e3b52a8..d2b1827315194 100644 --- a/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.move +++ b/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.move @@ -69,193 +69,193 @@ module aptos_std::pool_u64_unbound { } /// Destroy an empty pool. This will fail if the pool has any balance of coins. - public fun destroy_empty(pool: Pool) { - assert!(pool.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY)); + public fun destroy_empty(self: Pool) { + assert!(self.total_coins == 0, error::invalid_state(EPOOL_IS_NOT_EMPTY)); let Pool { total_coins: _, total_shares: _, shares, scaling_factor: _, - } = pool; + } = self; table::destroy_empty(shares); } - /// Return `pool`'s total balance of coins. - public fun total_coins(pool: &Pool): u64 { - pool.total_coins + /// Return `self`'s total balance of coins. + public fun total_coins(self: &Pool): u64 { + self.total_coins } - /// Return the total number of shares across all shareholders in `pool`. - public fun total_shares(pool: &Pool): u128 { - pool.total_shares + /// Return the total number of shares across all shareholders in `self`. + public fun total_shares(self: &Pool): u128 { + self.total_shares } - /// Return true if `shareholder` is in `pool`. - public fun contains(pool: &Pool, shareholder: address): bool { - table::contains(&pool.shares, shareholder) + /// Return true if `shareholder` is in `self`. + public fun contains(self: &Pool, shareholder: address): bool { + table::contains(&self.shares, shareholder) } - /// Return the number of shares of `stakeholder` in `pool`. - public fun shares(pool: &Pool, shareholder: address): u128 { - if (contains(pool, shareholder)) { - *table::borrow(&pool.shares, shareholder) + /// Return the number of shares of `stakeholder` in `self`. + public fun shares(self: &Pool, shareholder: address): u128 { + if (contains(self, shareholder)) { + *table::borrow(&self.shares, shareholder) } else { 0 } } - /// Return the balance in coins of `shareholder` in `pool.` - public fun balance(pool: &Pool, shareholder: address): u64 { - let num_shares = shares(pool, shareholder); - shares_to_amount(pool, num_shares) + /// Return the balance in coins of `shareholder` in `self`. + public fun balance(self: &Pool, shareholder: address): u64 { + let num_shares = shares(self, shareholder); + shares_to_amount(self, num_shares) } - /// Return the number of shareholders in `pool`. - public fun shareholders_count(pool: &Pool): u64 { - table::length(&pool.shares) + /// Return the number of shareholders in `self`. + public fun shareholders_count(self: &Pool): u64 { + table::length(&self.shares) } - /// Update `pool`'s total balance of coins. - public fun update_total_coins(pool: &mut Pool, new_total_coins: u64) { - pool.total_coins = new_total_coins; + /// Update `self`'s total balance of coins. + public fun update_total_coins(self: &mut Pool, new_total_coins: u64) { + self.total_coins = new_total_coins; } /// Allow an existing or new shareholder to add their coins to the pool in exchange for new shares. - public fun buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u128 { + public fun buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u128 { if (coins_amount == 0) return 0; - let new_shares = amount_to_shares(pool, coins_amount); - assert!(MAX_U64 - pool.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); - assert!(MAX_U128 - pool.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_SHARES_OVERFLOW)); + let new_shares = amount_to_shares(self, coins_amount); + assert!(MAX_U64 - self.total_coins >= coins_amount, error::invalid_argument(EPOOL_TOTAL_COINS_OVERFLOW)); + assert!(MAX_U128 - self.total_shares >= new_shares, error::invalid_argument(EPOOL_TOTAL_SHARES_OVERFLOW)); - pool.total_coins = pool.total_coins + coins_amount; - pool.total_shares = pool.total_shares + new_shares; - add_shares(pool, shareholder, new_shares); + self.total_coins = self.total_coins + coins_amount; + self.total_shares = self.total_shares + new_shares; + add_shares(self, shareholder, new_shares); new_shares } - /// Add the number of shares directly for `shareholder` in `pool`. + /// Add the number of shares directly for `shareholder` in `self`. /// This would dilute other shareholders if the pool's balance of coins didn't change. - fun add_shares(pool: &mut Pool, shareholder: address, new_shares: u128): u128 { - if (contains(pool, shareholder)) { - let existing_shares = table::borrow_mut(&mut pool.shares, shareholder); + fun add_shares(self: &mut Pool, shareholder: address, new_shares: u128): u128 { + if (contains(self, shareholder)) { + let existing_shares = table::borrow_mut(&mut self.shares, shareholder); let current_shares = *existing_shares; assert!(MAX_U128 - current_shares >= new_shares, error::invalid_argument(ESHAREHOLDER_SHARES_OVERFLOW)); *existing_shares = current_shares + new_shares; *existing_shares } else if (new_shares > 0) { - table::add(&mut pool.shares, shareholder, new_shares); + table::add(&mut self.shares, shareholder, new_shares); new_shares } else { new_shares } } - /// Allow `shareholder` to redeem their shares in `pool` for coins. - public fun redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 { - assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES)); + /// Allow `shareholder` to redeem their shares in `self` for coins. + public fun redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 { + assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder) >= shares_to_redeem, error::invalid_argument(EINSUFFICIENT_SHARES)); if (shares_to_redeem == 0) return 0; - let redeemed_coins = shares_to_amount(pool, shares_to_redeem); - pool.total_coins = pool.total_coins - redeemed_coins; - pool.total_shares = pool.total_shares - shares_to_redeem; - deduct_shares(pool, shareholder, shares_to_redeem); + let redeemed_coins = shares_to_amount(self, shares_to_redeem); + self.total_coins = self.total_coins - redeemed_coins; + self.total_shares = self.total_shares - shares_to_redeem; + deduct_shares(self, shareholder, shares_to_redeem); redeemed_coins } /// Transfer shares from `shareholder_1` to `shareholder_2`. public fun transfer_shares( - pool: &mut Pool, + self: &mut Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128, ) { - assert!(contains(pool, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES)); + assert!(contains(self, shareholder_1), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder_1) >= shares_to_transfer, error::invalid_argument(EINSUFFICIENT_SHARES)); if (shares_to_transfer == 0) return; - deduct_shares(pool, shareholder_1, shares_to_transfer); - add_shares(pool, shareholder_2, shares_to_transfer); + deduct_shares(self, shareholder_1, shares_to_transfer); + add_shares(self, shareholder_2, shares_to_transfer); } - /// Directly deduct `shareholder`'s number of shares in `pool` and return the number of remaining shares. - fun deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u128): u128 { - assert!(contains(pool, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); - assert!(shares(pool, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES)); + /// Directly deduct `shareholder`'s number of shares in `self` and return the number of remaining shares. + fun deduct_shares(self: &mut Pool, shareholder: address, num_shares: u128): u128 { + assert!(contains(self, shareholder), error::invalid_argument(ESHAREHOLDER_NOT_FOUND)); + assert!(shares(self, shareholder) >= num_shares, error::invalid_argument(EINSUFFICIENT_SHARES)); - let existing_shares = table::borrow_mut(&mut pool.shares, shareholder); + let existing_shares = table::borrow_mut(&mut self.shares, shareholder); *existing_shares = *existing_shares - num_shares; // Remove the shareholder completely if they have no shares left. let remaining_shares = *existing_shares; if (remaining_shares == 0) { - table::remove(&mut pool.shares, shareholder); + table::remove(&mut self.shares, shareholder); }; remaining_shares } - /// Return the number of new shares `coins_amount` can buy in `pool`. + /// Return the number of new shares `coins_amount` can buy in `self`. /// `amount` needs to big enough to avoid rounding number. - public fun amount_to_shares(pool: &Pool, coins_amount: u64): u128 { - amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins) + public fun amount_to_shares(self: &Pool, coins_amount: u64): u128 { + amount_to_shares_with_total_coins(self, coins_amount, self.total_coins) } - /// Return the number of new shares `coins_amount` can buy in `pool` with a custom total coins number. + /// Return the number of new shares `coins_amount` can buy in `self` with a custom total coins number. /// `amount` needs to big enough to avoid rounding number. - public fun amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u128 { + public fun amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u128 { // No shares yet so amount is worth the same number of shares. - if (pool.total_coins == 0 || pool.total_shares == 0) { + if (self.total_coins == 0 || self.total_shares == 0) { // Multiply by scaling factor to minimize rounding errors during internal calculations for buy ins/redeems. // This can overflow but scaling factor is expected to be chosen carefully so this would not overflow. - to_u128(coins_amount) * to_u128(pool.scaling_factor) + to_u128(coins_amount) * to_u128(self.scaling_factor) } else { // Shares price = total_coins / total existing shares. // New number of shares = new_amount / shares_price = new_amount * existing_shares / total_amount. // We rearrange the calc and do multiplication first to avoid rounding errors. - multiply_then_divide(pool, to_u128(coins_amount), pool.total_shares, to_u128(total_coins)) + multiply_then_divide(self, to_u128(coins_amount), self.total_shares, to_u128(total_coins)) } } - /// Return the number of coins `shares` are worth in `pool`. + /// Return the number of coins `shares` are worth in `self`. /// `shares` needs to big enough to avoid rounding number. - public fun shares_to_amount(pool: &Pool, shares: u128): u64 { - shares_to_amount_with_total_coins(pool, shares, pool.total_coins) + public fun shares_to_amount(self: &Pool, shares: u128): u64 { + shares_to_amount_with_total_coins(self, shares, self.total_coins) } - /// Return the number of coins `shares` are worth in `pool` with a custom total coins number. + /// Return the number of coins `shares` are worth in `self` with a custom total coins number. /// `shares` needs to big enough to avoid rounding number. - public fun shares_to_amount_with_total_coins(pool: &Pool, shares: u128, total_coins: u64): u64 { + public fun shares_to_amount_with_total_coins(self: &Pool, shares: u128, total_coins: u64): u64 { // No shares or coins yet so shares are worthless. - if (pool.total_coins == 0 || pool.total_shares == 0) { + if (self.total_coins == 0 || self.total_shares == 0) { 0 } else { // Shares price = total_coins / total existing shares. // Shares worth = shares * shares price = shares * total_coins / total existing shares. // We rearrange the calc and do multiplication first to avoid rounding errors. - (multiply_then_divide(pool, shares, to_u128(total_coins), pool.total_shares) as u64) + (multiply_then_divide(self, shares, to_u128(total_coins), self.total_shares) as u64) } } /// Return the number of coins `shares` are worth in `pool` with custom total coins and shares numbers. public fun shares_to_amount_with_total_stats( - pool: &Pool, + self: &Pool, shares: u128, total_coins: u64, total_shares: u128, ): u64 { - if (pool.total_coins == 0 || total_shares == 0) { + if (self.total_coins == 0 || total_shares == 0) { 0 } else { - (multiply_then_divide(pool, shares, to_u128(total_coins), total_shares) as u64) + (multiply_then_divide(self, shares, to_u128(total_coins), total_shares) as u64) } } - public fun multiply_then_divide(_pool: &Pool, x: u128, y: u128, z: u128): u128 { + public fun multiply_then_divide(self: &Pool, x: u128, y: u128, z: u128): u128 { let result = (to_u256(x) * to_u256(y)) / to_u256(z); (result as u128) } diff --git a/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.spec.move b/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.spec.move index 2a8570883ebea..c51b8464c6a94 100644 --- a/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/pool_u64_unbound.spec.move @@ -15,9 +15,9 @@ spec aptos_std::pool_u64_unbound { table::spec_contains(pool.shares, shareholder) } - spec contains(pool: &Pool, shareholder: address): bool { + spec contains(self: &Pool, shareholder: address): bool { aborts_if false; - ensures result == spec_contains(pool, shareholder); + ensures result == spec_contains(self, shareholder); } spec fun spec_shares(pool: Pool, shareholder: address): u64 { @@ -29,59 +29,59 @@ spec aptos_std::pool_u64_unbound { } } - spec shares(pool: &Pool, shareholder: address): u128 { + spec shares(self: &Pool, shareholder: address): u128 { aborts_if false; - ensures result == spec_shares(pool, shareholder); + ensures result == spec_shares(self, shareholder); } - spec balance(pool: &Pool, shareholder: address): u64 { - let shares = spec_shares(pool, shareholder); - let total_coins = pool.total_coins; - aborts_if pool.total_coins > 0 && pool.total_shares > 0 && (shares * total_coins) / pool.total_shares > MAX_U64; - ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins); + spec balance(self: &Pool, shareholder: address): u64 { + let shares = spec_shares(self, shareholder); + let total_coins = self.total_coins; + aborts_if self.total_coins > 0 && self.total_shares > 0 && (shares * total_coins) / self.total_shares > MAX_U64; + ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins); } - spec buy_in(pool: &mut Pool, shareholder: address, coins_amount: u64): u128 { - let new_shares = spec_amount_to_shares_with_total_coins(pool, coins_amount, pool.total_coins); - aborts_if pool.total_coins + coins_amount > MAX_U64; - aborts_if pool.total_shares + new_shares > MAX_U128; + spec buy_in(self: &mut Pool, shareholder: address, coins_amount: u64): u128 { + let new_shares = spec_amount_to_shares_with_total_coins(self, coins_amount, self.total_coins); + aborts_if self.total_coins + coins_amount > MAX_U64; + aborts_if self.total_shares + new_shares > MAX_U128; include coins_amount > 0 ==> AddSharesAbortsIf { new_shares: new_shares }; include coins_amount > 0 ==> AddSharesEnsures { new_shares: new_shares }; - ensures pool.total_coins == old(pool.total_coins) + coins_amount; - ensures pool.total_shares == old(pool.total_shares) + new_shares; + ensures self.total_coins == old(self.total_coins) + coins_amount; + ensures self.total_shares == old(self.total_shares) + new_shares; ensures result == new_shares; } - spec add_shares(pool: &mut Pool, shareholder: address, new_shares: u128): u128 { + spec add_shares(self: &mut Pool, shareholder: address, new_shares: u128): u128 { include AddSharesAbortsIf; include AddSharesEnsures; - let key_exists = table::spec_contains(pool.shares, shareholder); - ensures result == if (key_exists) { table::spec_get(pool.shares, shareholder) } + let key_exists = table::spec_contains(self.shares, shareholder); + ensures result == if (key_exists) { table::spec_get(self.shares, shareholder) } else { new_shares }; } spec schema AddSharesAbortsIf { - pool: Pool; + self: Pool; shareholder: address; new_shares: u64; - let key_exists = table::spec_contains(pool.shares, shareholder); - let current_shares = table::spec_get(pool.shares, shareholder); + let key_exists = table::spec_contains(self.shares, shareholder); + let current_shares = table::spec_get(self.shares, shareholder); aborts_if key_exists && current_shares + new_shares > MAX_U128; } spec schema AddSharesEnsures { - pool: Pool; + self: Pool; shareholder: address; new_shares: u64; - let key_exists = table::spec_contains(pool.shares, shareholder); - let current_shares = table::spec_get(pool.shares, shareholder); + let key_exists = table::spec_contains(self.shares, shareholder); + let current_shares = table::spec_get(self.shares, shareholder); ensures key_exists ==> - pool.shares == table::spec_set(old(pool.shares), shareholder, current_shares + new_shares); + self.shares == table::spec_set(old(self.shares), shareholder, current_shares + new_shares); ensures (!key_exists && new_shares > 0) ==> - pool.shares == table::spec_set(old(pool.shares), shareholder, new_shares); + self.shares == table::spec_set(old(self.shares), shareholder, new_shares); } spec fun spec_amount_to_shares_with_total_coins(pool: Pool, coins_amount: u64, total_coins: u64): u128 { @@ -93,19 +93,19 @@ spec aptos_std::pool_u64_unbound { } } - spec amount_to_shares_with_total_coins(pool: &Pool, coins_amount: u64, total_coins: u64): u128 { - aborts_if pool.total_coins > 0 && pool.total_shares > 0 - && (coins_amount * pool.total_shares) / total_coins > MAX_U128; - aborts_if (pool.total_coins == 0 || pool.total_shares == 0) - && coins_amount * pool.scaling_factor > MAX_U128; - aborts_if pool.total_coins > 0 && pool.total_shares > 0 && total_coins == 0; - ensures result == spec_amount_to_shares_with_total_coins(pool, coins_amount, total_coins); + spec amount_to_shares_with_total_coins(self: &Pool, coins_amount: u64, total_coins: u64): u128 { + aborts_if self.total_coins > 0 && self.total_shares > 0 + && (coins_amount * self.total_shares) / total_coins > MAX_U128; + aborts_if (self.total_coins == 0 || self.total_shares == 0) + && coins_amount * self.scaling_factor > MAX_U128; + aborts_if self.total_coins > 0 && self.total_shares > 0 && total_coins == 0; + ensures result == spec_amount_to_shares_with_total_coins(self, coins_amount, total_coins); } - spec shares_to_amount_with_total_coins(pool: &Pool, shares: u128, total_coins: u64): u64 { - aborts_if pool.total_coins > 0 && pool.total_shares > 0 - && (shares * total_coins) / pool.total_shares > MAX_U64; - ensures result == spec_shares_to_amount_with_total_coins(pool, shares, total_coins); + spec shares_to_amount_with_total_coins(self: &Pool, shares: u128, total_coins: u64): u64 { + aborts_if self.total_coins > 0 && self.total_shares > 0 + && (shares * total_coins) / self.total_shares > MAX_U64; + ensures result == spec_shares_to_amount_with_total_coins(self, shares, total_coins); } spec fun spec_shares_to_amount_with_total_coins(pool: Pool, shares: u128, total_coins: u64): u64 { @@ -117,63 +117,66 @@ spec aptos_std::pool_u64_unbound { } } - spec multiply_then_divide(_pool: &Pool, x: u128, y: u128, z: u128): u128 { + spec multiply_then_divide(self: &Pool, x: u128, y: u128, z: u128): u128 { aborts_if z == 0; aborts_if (x * y) / z > MAX_U128; ensures result == (x * y) / z; } - spec redeem_shares(pool: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 { - let redeemed_coins = spec_shares_to_amount_with_total_coins(pool, shares_to_redeem, pool.total_coins); - aborts_if !spec_contains(pool, shareholder); - aborts_if spec_shares(pool, shareholder) < shares_to_redeem; - aborts_if pool.total_coins < redeemed_coins; - aborts_if pool.total_shares < shares_to_redeem; - ensures pool.total_coins == old(pool.total_coins) - redeemed_coins; - ensures pool.total_shares == old(pool.total_shares) - shares_to_redeem; - include shares_to_redeem > 0 ==> DeductSharesEnsures { num_shares: shares_to_redeem }; + spec redeem_shares(self: &mut Pool, shareholder: address, shares_to_redeem: u128): u64 { + let redeemed_coins = spec_shares_to_amount_with_total_coins(self, shares_to_redeem, self.total_coins); + aborts_if !spec_contains(self, shareholder); + aborts_if spec_shares(self, shareholder) < shares_to_redeem; + aborts_if self.total_coins < redeemed_coins; + aborts_if self.total_shares < shares_to_redeem; + ensures self.total_coins == old(self.total_coins) - redeemed_coins; + ensures self.total_shares == old(self.total_shares) - shares_to_redeem; + include shares_to_redeem > 0 ==> DeductSharesEnsures { + num_shares: shares_to_redeem + }; ensures result == redeemed_coins; } spec transfer_shares( - pool: &mut Pool, + self: &mut Pool, shareholder_1: address, shareholder_2: address, shares_to_transfer: u128 ) { - aborts_if (shareholder_1 != shareholder_2) && shares_to_transfer > 0 && spec_contains(pool, shareholder_2) && - (spec_shares(pool, shareholder_2) + shares_to_transfer > MAX_U128); - aborts_if !spec_contains(pool, shareholder_1); - aborts_if spec_shares(pool, shareholder_1) < shares_to_transfer; - ensures shareholder_1 == shareholder_2 ==> spec_shares(old(pool), shareholder_1) == spec_shares(pool, shareholder_1); - ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(pool), shareholder_1) == shares_to_transfer)) ==> - !spec_contains(pool, shareholder_1); + aborts_if (shareholder_1 != shareholder_2) && shares_to_transfer > 0 && spec_contains(self, shareholder_2) && + (spec_shares(self, shareholder_2) + shares_to_transfer > MAX_U128); + aborts_if !spec_contains(self, shareholder_1); + aborts_if spec_shares(self, shareholder_1) < shares_to_transfer; + ensures shareholder_1 == shareholder_2 ==> spec_shares(old(self), shareholder_1) == spec_shares( + self, shareholder_1); + ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(self), shareholder_1) == shares_to_transfer)) ==> + !spec_contains(self, shareholder_1); ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0) ==> - (spec_contains(pool, shareholder_2)); - ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && !spec_contains(old(pool), shareholder_2)) ==> - (spec_contains(pool, shareholder_2) && spec_shares(pool, shareholder_2) == shares_to_transfer); - ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && spec_contains(old(pool), shareholder_2)) ==> - (spec_contains(pool, shareholder_2) && spec_shares(pool, shareholder_2) == spec_shares(old(pool), shareholder_2) + shares_to_transfer); - ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(pool), shareholder_1) > shares_to_transfer)) ==> - (spec_contains(pool, shareholder_1) && (spec_shares(pool, shareholder_1) == spec_shares(old(pool), shareholder_1) - shares_to_transfer)); + (spec_contains(self, shareholder_2)); + ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && !spec_contains(old(self), shareholder_2)) ==> + (spec_contains(self, shareholder_2) && spec_shares(self, shareholder_2) == shares_to_transfer); + ensures (shareholder_1 != shareholder_2 && shares_to_transfer > 0 && spec_contains(old(self), shareholder_2)) ==> + (spec_contains(self, shareholder_2) && spec_shares(self, shareholder_2) == spec_shares(old(self), shareholder_2) + shares_to_transfer); + ensures ((shareholder_1 != shareholder_2) && (spec_shares(old(self), shareholder_1) > shares_to_transfer)) ==> + (spec_contains(self, shareholder_1) && (spec_shares(self, shareholder_1) == spec_shares(old(self), shareholder_1) - shares_to_transfer)); } - spec deduct_shares(pool: &mut Pool, shareholder: address, num_shares: u128): u128 { - aborts_if !spec_contains(pool, shareholder); - aborts_if spec_shares(pool, shareholder) < num_shares; + spec deduct_shares(self: &mut Pool, shareholder: address, num_shares: u128): u128 { + aborts_if !spec_contains(self, shareholder); + aborts_if spec_shares(self, shareholder) < num_shares; include DeductSharesEnsures; - let remaining_shares = table::spec_get(pool.shares, shareholder) - num_shares; - ensures remaining_shares > 0 ==> result == table::spec_get(pool.shares, shareholder); + let remaining_shares = table::spec_get(self.shares, shareholder) - num_shares; + ensures remaining_shares > 0 ==> result == table::spec_get(self.shares, shareholder); ensures remaining_shares == 0 ==> result == 0; } spec schema DeductSharesEnsures { - pool: Pool; + self: Pool; shareholder: address; num_shares: u64; - let remaining_shares = table::spec_get(pool.shares, shareholder) - num_shares; - ensures remaining_shares > 0 ==> table::spec_get(pool.shares, shareholder) == remaining_shares; - ensures remaining_shares == 0 ==> !table::spec_contains(pool.shares, shareholder); + let remaining_shares = table::spec_get(self.shares, shareholder) - num_shares; + ensures remaining_shares > 0 ==> table::spec_get(self.shares, shareholder) == remaining_shares; + ensures remaining_shares == 0 ==> !table::spec_contains(self.shares, shareholder); } spec to_u128(num: u64): u128 { diff --git a/aptos-move/framework/aptos-stdlib/sources/simple_map.move b/aptos-move/framework/aptos-stdlib/sources/simple_map.move index 98ae46cf6b30d..d672065b1467d 100644 --- a/aptos-move/framework/aptos-stdlib/sources/simple_map.move +++ b/aptos-move/framework/aptos-stdlib/sources/simple_map.move @@ -23,8 +23,8 @@ module aptos_std::simple_map { value: Value, } - public fun length(map: &SimpleMap): u64 { - vector::length(&map.data) + public fun length(self: &SimpleMap): u64 { + vector::length(&self.data) } /// Create an empty SimpleMap. @@ -52,68 +52,68 @@ module aptos_std::simple_map { } public fun borrow( - map: &SimpleMap, + self: &SimpleMap, key: &Key, ): &Value { - let maybe_idx = find(map, key); + let maybe_idx = find(self, key); assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND)); let idx = option::extract(&mut maybe_idx); - &vector::borrow(&map.data, idx).value + &vector::borrow(&self.data, idx).value } public fun borrow_mut( - map: &mut SimpleMap, + self: &mut SimpleMap, key: &Key, ): &mut Value { - let maybe_idx = find(map, key); + let maybe_idx = find(self, key); assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND)); let idx = option::extract(&mut maybe_idx); - &mut vector::borrow_mut(&mut map.data, idx).value + &mut vector::borrow_mut(&mut self.data, idx).value } public fun contains_key( - map: &SimpleMap, + self: &SimpleMap, key: &Key, ): bool { - let maybe_idx = find(map, key); + let maybe_idx = find(self, key); option::is_some(&maybe_idx) } - public fun destroy_empty(map: SimpleMap) { - let SimpleMap { data } = map; + public fun destroy_empty(self: SimpleMap) { + let SimpleMap { data } = self; vector::destroy_empty(data); } /// Add a key/value pair to the map. The key must not already exist. public fun add( - map: &mut SimpleMap, + self: &mut SimpleMap, key: Key, value: Value, ) { - let maybe_idx = find(map, &key); + let maybe_idx = find(self, &key); assert!(option::is_none(&maybe_idx), error::invalid_argument(EKEY_ALREADY_EXISTS)); - vector::push_back(&mut map.data, Element { key, value }); + vector::push_back(&mut self.data, Element { key, value }); } /// Add multiple key/value pairs to the map. The keys must not already exist. public fun add_all( - map: &mut SimpleMap, + self: &mut SimpleMap, keys: vector, values: vector, ) { vector::zip(keys, values, |key, value| { - add(map, key, value); + add(self, key, value); }); } /// Insert key/value pair or update an existing key to a new value public fun upsert( - map: &mut SimpleMap, + self: &mut SimpleMap, key: Key, value: Value ): (std::option::Option, std::option::Option) { - let data = &mut map.data; + let data = &mut self.data; let len = vector::length(data); let i = 0; while (i < len) { @@ -126,21 +126,21 @@ module aptos_std::simple_map { }; i = i + 1; }; - vector::push_back(&mut map.data, Element { key, value }); + vector::push_back(&mut self.data, Element { key, value }); (std::option::none(), std::option::none()) } /// Return all keys in the map. This requires keys to be copyable. - public fun keys(map: &SimpleMap): vector { - vector::map_ref(&map.data, |e| { + public fun keys(self: &SimpleMap): vector { + vector::map_ref(&self.data, |e| { let e: &Element = e; e.key }) } /// Return all values in the map. This requires values to be copyable. - public fun values(map: &SimpleMap): vector { - vector::map_ref(&map.data, |e| { + public fun values(self: &SimpleMap): vector { + vector::map_ref(&self.data, |e| { let e: &Element = e; e.value }) @@ -149,10 +149,10 @@ module aptos_std::simple_map { /// Transform the map into two vectors with the keys and values respectively /// Primarily used to destroy a map public fun to_vec_pair( - map: SimpleMap): (vector, vector) { + self: SimpleMap): (vector, vector) { let keys: vector = vector::empty(); let values: vector = vector::empty(); - let SimpleMap { data } = map; + let SimpleMap { data } = self; vector::for_each(data, |e| { let Element { key, value } = e; vector::push_back(&mut keys, key); @@ -164,35 +164,35 @@ module aptos_std::simple_map { /// For maps that cannot be dropped this is a utility to destroy them /// using lambdas to destroy the individual keys and values. public inline fun destroy( - map: SimpleMap, + self: SimpleMap, dk: |Key|, dv: |Value| ) { - let (keys, values) = to_vec_pair(map); + let (keys, values) = to_vec_pair(self); vector::destroy(keys, |_k| dk(_k)); vector::destroy(values, |_v| dv(_v)); } /// Remove a key/value pair from the map. The key must exist. public fun remove( - map: &mut SimpleMap, + self: &mut SimpleMap, key: &Key, ): (Key, Value) { - let maybe_idx = find(map, key); + let maybe_idx = find(self, key); assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND)); let placement = option::extract(&mut maybe_idx); - let Element { key, value } = vector::swap_remove(&mut map.data, placement); + let Element { key, value } = vector::swap_remove(&mut self.data, placement); (key, value) } fun find( - map: &SimpleMap, + self: &SimpleMap, key: &Key, ): option::Option { - let leng = vector::length(&map.data); + let leng = vector::length(&self.data); let i = 0; while (i < leng) { - let element = vector::borrow(&map.data, i); + let element = vector::borrow(&self.data, i); if (&element.key == key) { return option::some(i) }; diff --git a/aptos-move/framework/aptos-stdlib/sources/simple_map.spec.move b/aptos-move/framework/aptos-stdlib/sources/simple_map.spec.move index 35258eb37532d..f0bcd5a759fc8 100644 --- a/aptos-move/framework/aptos-stdlib/sources/simple_map.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/simple_map.spec.move @@ -89,30 +89,32 @@ spec aptos_std::simple_map { spec_get(result, vector::borrow(keys, i)) == vector::borrow(values, i); } - spec to_vec_pair(map: SimpleMap): (vector, vector) { + spec to_vec_pair(self: SimpleMap): (vector, vector) { pragma intrinsic; pragma opaque; ensures [abstract] forall k: Key: vector::spec_contains(result_1, k) <==> - spec_contains_key(map, k); + spec_contains_key(self, k); ensures [abstract] forall i in 0..len(result_1): - spec_get(map, vector::borrow(result_1, i)) == vector::borrow(result_2, i); + spec_get(self, vector::borrow(result_1, i)) == vector::borrow(result_2, i); } spec upsert( - map: &mut SimpleMap, + self: &mut SimpleMap, key: Key, value: Value ): (std::option::Option, std::option::Option) { pragma intrinsic; pragma opaque; aborts_if [abstract] false; - ensures [abstract] !spec_contains_key(old(map), key) ==> option::is_none(result_1); - ensures [abstract] !spec_contains_key(old(map), key) ==> option::is_none(result_2); - ensures [abstract] spec_contains_key(map, key); - ensures [abstract] spec_get(map, key) == value; - ensures [abstract] spec_contains_key(old(map), key) ==> ((option::is_some(result_1)) && (option::spec_borrow(result_1) == key)); - ensures [abstract] spec_contains_key(old(map), key) ==> ((option::is_some(result_2)) && (option::spec_borrow(result_2) == spec_get(old(map), key))); + ensures [abstract] !spec_contains_key(old(self), key) ==> option::is_none(result_1); + ensures [abstract] !spec_contains_key(old(self), key) ==> option::is_none(result_2); + ensures [abstract] spec_contains_key(self, key); + ensures [abstract] spec_get(self, key) == value; + ensures [abstract] spec_contains_key(old(self), key) ==> ((option::is_some(result_1)) && (option::spec_borrow(result_1) == key)); + ensures [abstract] spec_contains_key(old(self), key) ==> ((option::is_some(result_2)) && (option::spec_borrow(result_2) == spec_get(old( + self + ), key))); } // Specification functions for tables diff --git a/aptos-move/framework/aptos-stdlib/sources/table.move b/aptos-move/framework/aptos-stdlib/sources/table.move index dbc85209dd8e0..f96936f29f6f9 100644 --- a/aptos-move/framework/aptos-stdlib/sources/table.move +++ b/aptos-move/framework/aptos-stdlib/sources/table.move @@ -23,73 +23,73 @@ module aptos_std::table { /// Add a new entry to the table. Aborts if an entry for this /// key already exists. The entry itself is not stored in the /// table, and cannot be discovered from it. - public fun add(table: &mut Table, key: K, val: V) { - add_box>(table, key, Box { val }) + public fun add(self: &mut Table, key: K, val: V) { + add_box>(self, key, Box { val }) } /// Acquire an immutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow(table: &Table, key: K): &V { - &borrow_box>(table, key).val + public fun borrow(self: &Table, key: K): &V { + &borrow_box>(self, key).val } /// Acquire an immutable reference to the value which `key` maps to. /// Returns specified default value if there is no entry for `key`. - public fun borrow_with_default(table: &Table, key: K, default: &V): &V { - if (!contains(table, copy key)) { + public fun borrow_with_default(self: &Table, key: K, default: &V): &V { + if (!contains(self, copy key)) { default } else { - borrow(table, copy key) + borrow(self, copy key) } } /// Acquire a mutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow_mut(table: &mut Table, key: K): &mut V { - &mut borrow_box_mut>(table, key).val + public fun borrow_mut(self: &mut Table, key: K): &mut V { + &mut borrow_box_mut>(self, key).val } /// Acquire a mutable reference to the value which `key` maps to. /// Insert the pair (`key`, `default`) first if there is no entry for `key`. - public fun borrow_mut_with_default(table: &mut Table, key: K, default: V): &mut V { - if (!contains(table, copy key)) { - add(table, copy key, default) + public fun borrow_mut_with_default(self: &mut Table, key: K, default: V): &mut V { + if (!contains(self, copy key)) { + add(self, copy key, default) }; - borrow_mut(table, key) + borrow_mut(self, key) } /// Insert the pair (`key`, `value`) if there is no entry for `key`. /// update the value of the entry for `key` to `value` otherwise - public fun upsert(table: &mut Table, key: K, value: V) { - if (!contains(table, copy key)) { - add(table, copy key, value) + public fun upsert(self: &mut Table, key: K, value: V) { + if (!contains(self, copy key)) { + add(self, copy key, value) } else { - let ref = borrow_mut(table, key); + let ref = borrow_mut(self, key); *ref = value; }; } - /// Remove from `table` and return the value which `key` maps to. + /// Remove from `self` and return the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun remove(table: &mut Table, key: K): V { - let Box { val } = remove_box>(table, key); + public fun remove(self: &mut Table, key: K): V { + let Box { val } = remove_box>(self, key); val } - /// Returns true iff `table` contains an entry for `key`. - public fun contains(table: &Table, key: K): bool { - contains_box>(table, key) + /// Returns true iff `self` contains an entry for `key`. + public fun contains(self: &Table, key: K): bool { + contains_box>(self, key) } #[test_only] /// Testing only: allows to drop a table even if it is not empty. - public fun drop_unchecked(table: Table) { - drop_unchecked_box>(table) + public fun drop_unchecked(self: Table) { + drop_unchecked_box>(self) } - public(friend) fun destroy(table: Table) { - destroy_empty_box>(&table); - drop_unchecked_box>(table) + public(friend) fun destroy(self: Table) { + destroy_empty_box>(&self); + drop_unchecked_box>(self) } #[test_only] diff --git a/aptos-move/framework/aptos-stdlib/sources/table_with_length.move b/aptos-move/framework/aptos-stdlib/sources/table_with_length.move index c56ff2b4224fc..e4ca2415bc939 100644 --- a/aptos-move/framework/aptos-stdlib/sources/table_with_length.move +++ b/aptos-move/framework/aptos-stdlib/sources/table_with_length.move @@ -25,84 +25,84 @@ module aptos_std::table_with_length { } /// Destroy a table. The table must be empty to succeed. - public fun destroy_empty(table: TableWithLength) { - assert!(table.length == 0, error::invalid_state(ENOT_EMPTY)); - let TableWithLength { inner, length: _ } = table; + public fun destroy_empty(self: TableWithLength) { + assert!(self.length == 0, error::invalid_state(ENOT_EMPTY)); + let TableWithLength { inner, length: _ } = self; table::destroy(inner) } /// Add a new entry to the table. Aborts if an entry for this /// key already exists. The entry itself is not stored in the /// table, and cannot be discovered from it. - public fun add(table: &mut TableWithLength, key: K, val: V) { - table::add(&mut table.inner, key, val); - table.length = table.length + 1; + public fun add(self: &mut TableWithLength, key: K, val: V) { + table::add(&mut self.inner, key, val); + self.length = self.length + 1; } /// Acquire an immutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow(table: &TableWithLength, key: K): &V { - table::borrow(&table.inner, key) + public fun borrow(self: &TableWithLength, key: K): &V { + table::borrow(&self.inner, key) } /// Acquire a mutable reference to the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun borrow_mut(table: &mut TableWithLength, key: K): &mut V { - table::borrow_mut(&mut table.inner, key) + public fun borrow_mut(self: &mut TableWithLength, key: K): &mut V { + table::borrow_mut(&mut self.inner, key) } /// Returns the length of the table, i.e. the number of entries. - public fun length(table: &TableWithLength): u64 { - table.length + public fun length(self: &TableWithLength): u64 { + self.length } /// Returns true if this table is empty. - public fun empty(table: &TableWithLength): bool { - table.length == 0 + public fun empty(self: &TableWithLength): bool { + self.length == 0 } /// Acquire a mutable reference to the value which `key` maps to. /// Insert the pair (`key`, `default`) first if there is no entry for `key`. - public fun borrow_mut_with_default(table: &mut TableWithLength, key: K, default: V): &mut V { - if (table::contains(&table.inner, key)) { - table::borrow_mut(&mut table.inner, key) + public fun borrow_mut_with_default(self: &mut TableWithLength, key: K, default: V): &mut V { + if (table::contains(&self.inner, key)) { + table::borrow_mut(&mut self.inner, key) } else { - table::add(&mut table.inner, key, default); - table.length = table.length + 1; - table::borrow_mut(&mut table.inner, key) + table::add(&mut self.inner, key, default); + self.length = self.length + 1; + table::borrow_mut(&mut self.inner, key) } } /// Insert the pair (`key`, `value`) if there is no entry for `key`. /// update the value of the entry for `key` to `value` otherwise - public fun upsert(table: &mut TableWithLength, key: K, value: V) { - if (!table::contains(&table.inner, key)) { - add(table, copy key, value) + public fun upsert(self: &mut TableWithLength, key: K, value: V) { + if (!table::contains(&self.inner, key)) { + add(self, copy key, value) } else { - let ref = table::borrow_mut(&mut table.inner, key); + let ref = table::borrow_mut(&mut self.inner, key); *ref = value; }; } /// Remove from `table` and return the value which `key` maps to. /// Aborts if there is no entry for `key`. - public fun remove(table: &mut TableWithLength, key: K): V { - let val = table::remove(&mut table.inner, key); - table.length = table.length - 1; + public fun remove(self: &mut TableWithLength, key: K): V { + let val = table::remove(&mut self.inner, key); + self.length = self.length - 1; val } /// Returns true iff `table` contains an entry for `key`. - public fun contains(table: &TableWithLength, key: K): bool { - table::contains(&table.inner, key) + public fun contains(self: &TableWithLength, key: K): bool { + table::contains(&self.inner, key) } #[test_only] /// Drop table even if not empty, only when testing. - public fun drop_unchecked(table: TableWithLength) { + public fun drop_unchecked(self: TableWithLength) { // Unpack table with length, dropping length count but not // inner table. - let TableWithLength{inner, length: _} = table; + let TableWithLength{inner, length: _} = self; table::drop_unchecked(inner); // Drop inner table. } diff --git a/aptos-move/framework/aptos-stdlib/sources/type_info.move b/aptos-move/framework/aptos-stdlib/sources/type_info.move index 2ad3bba4041cc..62265f7a77096 100644 --- a/aptos-move/framework/aptos-stdlib/sources/type_info.move +++ b/aptos-move/framework/aptos-stdlib/sources/type_info.move @@ -3,7 +3,6 @@ module aptos_std::type_info { use std::features; use std::string::{Self, String}; use std::vector; - // // Error codes // @@ -24,16 +23,16 @@ module aptos_std::type_info { // Public functions // - public fun account_address(type_info: &TypeInfo): address { - type_info.account_address + public fun account_address(self: &TypeInfo): address { + self.account_address } - public fun module_name(type_info: &TypeInfo): vector { - type_info.module_name + public fun module_name(self: &TypeInfo): vector { + self.module_name } - public fun struct_name(type_info: &TypeInfo): vector { - type_info.struct_name + public fun struct_name(self: &TypeInfo): vector { + self.struct_name } /// Returns the current chain ID, mirroring what `aptos_framework::chain_id::get()` would return, except in `#[test]` @@ -65,13 +64,15 @@ module aptos_std::type_info { /// nesting patterns, as well as `test_size_of_val_vectors()` for an /// analysis of vector size dynamism. public fun size_of_val(val_ref: &T): u64 { - // Return vector length of vectorized BCS representation. vector::length(&bcs::to_bytes(val_ref)) } #[test_only] use aptos_std::table::Table; + #[test_only] + use std::vector; + #[test] fun test_type_of() { let type_info = type_of(); diff --git a/aptos-move/framework/aptos-stdlib/sources/type_info.spec.move b/aptos-move/framework/aptos-stdlib/sources/type_info.spec.move index ed3ed481c8008..26fe9b6163c6a 100644 --- a/aptos-move/framework/aptos-stdlib/sources/type_info.spec.move +++ b/aptos-move/framework/aptos-stdlib/sources/type_info.spec.move @@ -30,7 +30,6 @@ spec aptos_std::type_info { } spec size_of_val(val_ref: &T): u64 { - aborts_if false; ensures result == spec_size_of_val(val_ref); } } diff --git a/aptos-move/framework/move-stdlib/doc/option.md b/aptos-move/framework/move-stdlib/doc/option.md index 914a948f5c978..5da326c846261 100644 --- a/aptos-move/framework/move-stdlib/doc/option.md +++ b/aptos-move/framework/move-stdlib/doc/option.md @@ -208,10 +208,10 @@ Return an Option containi ## Function `is_none` -Return true if t does not hold a value +Return true if self does not hold a value -
public fun is_none<Element>(t: &option::Option<Element>): bool
+
public fun is_none<Element>(self: &option::Option<Element>): bool
 
@@ -220,8 +220,8 @@ Return true if t does not hold a value Implementation -
public fun is_none<Element>(t: &Option<Element>): bool {
-    vector::is_empty(&t.vec)
+
public fun is_none<Element>(self: &Option<Element>): bool {
+    vector::is_empty(&self.vec)
 }
 
@@ -233,10 +233,10 @@ Return true if t does not hold a value ## Function `is_some` -Return true if t holds a value +Return true if self holds a value -
public fun is_some<Element>(t: &option::Option<Element>): bool
+
public fun is_some<Element>(self: &option::Option<Element>): bool
 
@@ -245,8 +245,8 @@ Return true if t holds a value Implementation -
public fun is_some<Element>(t: &Option<Element>): bool {
-    !vector::is_empty(&t.vec)
+
public fun is_some<Element>(self: &Option<Element>): bool {
+    !vector::is_empty(&self.vec)
 }
 
@@ -258,11 +258,11 @@ Return true if t holds a value ## Function `contains` -Return true if the value in t is equal to e_ref -Always returns false if t does not hold a value +Return true if the value in self is equal to e_ref +Always returns false if self does not hold a value -
public fun contains<Element>(t: &option::Option<Element>, e_ref: &Element): bool
+
public fun contains<Element>(self: &option::Option<Element>, e_ref: &Element): bool
 
@@ -271,8 +271,8 @@ Always returns false if t does not hold a value Implementation -
public fun contains<Element>(t: &Option<Element>, e_ref: &Element): bool {
-    vector::contains(&t.vec, e_ref)
+
public fun contains<Element>(self: &Option<Element>, e_ref: &Element): bool {
+    vector::contains(&self.vec, e_ref)
 }
 
@@ -284,11 +284,11 @@ Always returns false if t does not hold a value ## Function `borrow` -Return an immutable reference to the value inside t -Aborts if t does not hold a value +Return an immutable reference to the value inside self +Aborts if self does not hold a value -
public fun borrow<Element>(t: &option::Option<Element>): &Element
+
public fun borrow<Element>(self: &option::Option<Element>): &Element
 
@@ -297,9 +297,9 @@ Aborts if t does not hold a value Implementation -
public fun borrow<Element>(t: &Option<Element>): &Element {
-    assert!(is_some(t), EOPTION_NOT_SET);
-    vector::borrow(&t.vec, 0)
+
public fun borrow<Element>(self: &Option<Element>): &Element {
+    assert!(is_some(self), EOPTION_NOT_SET);
+    vector::borrow(&self.vec, 0)
 }
 
@@ -311,11 +311,11 @@ Aborts if t does not hold a value ## Function `borrow_with_default` -Return a reference to the value inside t if it holds one -Return default_ref if t does not hold a value +Return a reference to the value inside self if it holds one +Return default_ref if self does not hold a value -
public fun borrow_with_default<Element>(t: &option::Option<Element>, default_ref: &Element): &Element
+
public fun borrow_with_default<Element>(self: &option::Option<Element>, default_ref: &Element): &Element
 
@@ -324,8 +324,8 @@ Return default_ref if t does not hold a value Implementation -
public fun borrow_with_default<Element>(t: &Option<Element>, default_ref: &Element): &Element {
-    let vec_ref = &t.vec;
+
public fun borrow_with_default<Element>(self: &Option<Element>, default_ref: &Element): &Element {
+    let vec_ref = &self.vec;
     if (vector::is_empty(vec_ref)) default_ref
     else vector::borrow(vec_ref, 0)
 }
@@ -339,11 +339,11 @@ Return default_ref if t does not hold a value
 
 ## Function `get_with_default`
 
-Return the value inside t if it holds one
-Return default if t does not hold a value
+Return the value inside self if it holds one
+Return default if self does not hold a value
 
 
-
public fun get_with_default<Element: copy, drop>(t: &option::Option<Element>, default: Element): Element
+
public fun get_with_default<Element: copy, drop>(self: &option::Option<Element>, default: Element): Element
 
@@ -353,10 +353,10 @@ Return default if t does not hold a value
public fun get_with_default<Element: copy + drop>(
-    t: &Option<Element>,
+    self: &Option<Element>,
     default: Element,
 ): Element {
-    let vec_ref = &t.vec;
+    let vec_ref = &self.vec;
     if (vector::is_empty(vec_ref)) default
     else *vector::borrow(vec_ref, 0)
 }
@@ -370,11 +370,11 @@ Return default if t does not hold a value
 
 ## Function `fill`
 
-Convert the none option t to a some option by adding e.
-Aborts if t already holds a value
+Convert the none option self to a some option by adding e.
+Aborts if self already holds a value
 
 
-
public fun fill<Element>(t: &mut option::Option<Element>, e: Element)
+
public fun fill<Element>(self: &mut option::Option<Element>, e: Element)
 
@@ -383,8 +383,8 @@ Aborts if t already holds a value Implementation -
public fun fill<Element>(t: &mut Option<Element>, e: Element) {
-    let vec_ref = &mut t.vec;
+
public fun fill<Element>(self: &mut Option<Element>, e: Element) {
+    let vec_ref = &mut self.vec;
     if (vector::is_empty(vec_ref)) vector::push_back(vec_ref, e)
     else abort EOPTION_IS_SET
 }
@@ -398,11 +398,11 @@ Aborts if t already holds a value
 
 ## Function `extract`
 
-Convert a some option to a none by removing and returning the value stored inside t
-Aborts if t does not hold a value
+Convert a some option to a none by removing and returning the value stored inside self
+Aborts if self does not hold a value
 
 
-
public fun extract<Element>(t: &mut option::Option<Element>): Element
+
public fun extract<Element>(self: &mut option::Option<Element>): Element
 
@@ -411,9 +411,9 @@ Aborts if t does not hold a value Implementation -
public fun extract<Element>(t: &mut Option<Element>): Element {
-    assert!(is_some(t), EOPTION_NOT_SET);
-    vector::pop_back(&mut t.vec)
+
public fun extract<Element>(self: &mut Option<Element>): Element {
+    assert!(is_some(self), EOPTION_NOT_SET);
+    vector::pop_back(&mut self.vec)
 }
 
@@ -425,11 +425,11 @@ Aborts if t does not hold a value ## Function `borrow_mut` -Return a mutable reference to the value inside t -Aborts if t does not hold a value +Return a mutable reference to the value inside self +Aborts if self does not hold a value -
public fun borrow_mut<Element>(t: &mut option::Option<Element>): &mut Element
+
public fun borrow_mut<Element>(self: &mut option::Option<Element>): &mut Element
 
@@ -438,9 +438,9 @@ Aborts if t does not hold a value Implementation -
public fun borrow_mut<Element>(t: &mut Option<Element>): &mut Element {
-    assert!(is_some(t), EOPTION_NOT_SET);
-    vector::borrow_mut(&mut t.vec, 0)
+
public fun borrow_mut<Element>(self: &mut Option<Element>): &mut Element {
+    assert!(is_some(self), EOPTION_NOT_SET);
+    vector::borrow_mut(&mut self.vec, 0)
 }
 
@@ -452,11 +452,11 @@ Aborts if t does not hold a value ## Function `swap` -Swap the old value inside t with e and return the old value -Aborts if t does not hold a value +Swap the old value inside self with e and return the old value +Aborts if self does not hold a value -
public fun swap<Element>(t: &mut option::Option<Element>, e: Element): Element
+
public fun swap<Element>(self: &mut option::Option<Element>, e: Element): Element
 
@@ -465,9 +465,9 @@ Aborts if t does not hold a value Implementation -
public fun swap<Element>(t: &mut Option<Element>, e: Element): Element {
-    assert!(is_some(t), EOPTION_NOT_SET);
-    let vec_ref = &mut t.vec;
+
public fun swap<Element>(self: &mut Option<Element>, e: Element): Element {
+    assert!(is_some(self), EOPTION_NOT_SET);
+    let vec_ref = &mut self.vec;
     let old_value = vector::pop_back(vec_ref);
     vector::push_back(vec_ref, e);
     old_value
@@ -482,12 +482,12 @@ Aborts if t does not hold a value
 
 ## Function `swap_or_fill`
 
-Swap the old value inside t with e and return the old value;
+Swap the old value inside self with e and return the old value;
 or if there is no old value, fill it with e.
-Different from swap(), swap_or_fill() allows for t not holding a value.
+Different from swap(), swap_or_fill() allows for self not holding a value.
 
 
-
public fun swap_or_fill<Element>(t: &mut option::Option<Element>, e: Element): option::Option<Element>
+
public fun swap_or_fill<Element>(self: &mut option::Option<Element>, e: Element): option::Option<Element>
 
@@ -496,8 +496,8 @@ Different from swap(), swap_or_fill() allows for t not holding a va Implementation -
public fun swap_or_fill<Element>(t: &mut Option<Element>, e: Element): Option<Element> {
-    let vec_ref = &mut t.vec;
+
public fun swap_or_fill<Element>(self: &mut Option<Element>, e: Element): Option<Element> {
+    let vec_ref = &mut self.vec;
     let old_value = if (vector::is_empty(vec_ref)) none()
         else some(vector::pop_back(vec_ref));
     vector::push_back(vec_ref, e);
@@ -513,10 +513,10 @@ Different from swap(), swap_or_fill() allows for t not holding a va
 
 ## Function `destroy_with_default`
 
-Destroys t. If t holds a value, return it. Returns default otherwise
+Destroys self. If self holds a value, return it. Returns default otherwise
 
 
-
public fun destroy_with_default<Element: drop>(t: option::Option<Element>, default: Element): Element
+
public fun destroy_with_default<Element: drop>(self: option::Option<Element>, default: Element): Element
 
@@ -525,8 +525,8 @@ Destroys t. If t holds a value, return it. Returns Implementation -
public fun destroy_with_default<Element: drop>(t: Option<Element>, default: Element): Element {
-    let Option { vec } = t;
+
public fun destroy_with_default<Element: drop>(self: Option<Element>, default: Element): Element {
+    let Option { vec } = self;
     if (vector::is_empty(&mut vec)) default
     else vector::pop_back(&mut vec)
 }
@@ -540,11 +540,11 @@ Destroys t. If t holds a value, return it. Returns t and return its contents
-Aborts if t does not hold a value
+Unpack self and return its contents
+Aborts if self does not hold a value
 
 
-
public fun destroy_some<Element>(t: option::Option<Element>): Element
+
public fun destroy_some<Element>(self: option::Option<Element>): Element
 
@@ -553,9 +553,9 @@ Aborts if t does not hold a value Implementation -
public fun destroy_some<Element>(t: Option<Element>): Element {
-    assert!(is_some(&t), EOPTION_NOT_SET);
-    let Option { vec } = t;
+
public fun destroy_some<Element>(self: Option<Element>): Element {
+    assert!(is_some(&self), EOPTION_NOT_SET);
+    let Option { vec } = self;
     let elem = vector::pop_back(&mut vec);
     vector::destroy_empty(vec);
     elem
@@ -570,11 +570,11 @@ Aborts if t does not hold a value
 
 ## Function `destroy_none`
 
-Unpack t
-Aborts if t holds a value
+Unpack self
+Aborts if self holds a value
 
 
-
public fun destroy_none<Element>(t: option::Option<Element>)
+
public fun destroy_none<Element>(self: option::Option<Element>)
 
@@ -583,9 +583,9 @@ Aborts if t holds a value Implementation -
public fun destroy_none<Element>(t: Option<Element>) {
-    assert!(is_none(&t), EOPTION_IS_SET);
-    let Option { vec } = t;
+
public fun destroy_none<Element>(self: Option<Element>) {
+    assert!(is_none(&self), EOPTION_IS_SET);
+    let Option { vec } = self;
     vector::destroy_empty(vec)
 }
 
@@ -598,11 +598,11 @@ Aborts if t holds a value ## Function `to_vec` -Convert t into a vector of length 1 if it is Some, +Convert self into a vector of length 1 if it is Some, and an empty vector otherwise -
public fun to_vec<Element>(t: option::Option<Element>): vector<Element>
+
public fun to_vec<Element>(self: option::Option<Element>): vector<Element>
 
@@ -611,8 +611,8 @@ and an empty vector otherwise Implementation -
public fun to_vec<Element>(t: Option<Element>): vector<Element> {
-    let Option { vec } = t;
+
public fun to_vec<Element>(self: Option<Element>): vector<Element> {
+    let Option { vec } = self;
     vec
 }
 
@@ -628,7 +628,7 @@ and an empty vector otherwise Apply the function to the optional element, consuming it. Does nothing if no value present. -
public fun for_each<Element>(o: option::Option<Element>, f: |Element|)
+
public fun for_each<Element>(self: option::Option<Element>, f: |Element|)
 
@@ -637,11 +637,11 @@ Apply the function to the optional element, consuming it. Does nothing if no val Implementation -
public inline fun for_each<Element>(o: Option<Element>, f: |Element|) {
-    if (is_some(&o)) {
-        f(destroy_some(o))
+
public inline fun for_each<Element>(self: Option<Element>, f: |Element|) {
+    if (is_some(&self)) {
+        f(destroy_some(self))
     } else {
-        destroy_none(o)
+        destroy_none(self)
     }
 }
 
@@ -657,7 +657,7 @@ Apply the function to the optional element, consuming it. Does nothing if no val Apply the function to the optional element reference. Does nothing if no value present. -
public fun for_each_ref<Element>(o: &option::Option<Element>, f: |&Element|)
+
public fun for_each_ref<Element>(self: &option::Option<Element>, f: |&Element|)
 
@@ -666,9 +666,9 @@ Apply the function to the optional element reference. Does nothing if no value p Implementation -
public inline fun for_each_ref<Element>(o: &Option<Element>, f: |&Element|) {
-    if (is_some(o)) {
-        f(borrow(o))
+
public inline fun for_each_ref<Element>(self: &Option<Element>, f: |&Element|) {
+    if (is_some(self)) {
+        f(borrow(self))
     }
 }
 
@@ -684,7 +684,7 @@ Apply the function to the optional element reference. Does nothing if no value p Apply the function to the optional element reference. Does nothing if no value present. -
public fun for_each_mut<Element>(o: &mut option::Option<Element>, f: |&mut Element|)
+
public fun for_each_mut<Element>(self: &mut option::Option<Element>, f: |&mut Element|)
 
@@ -693,9 +693,9 @@ Apply the function to the optional element reference. Does nothing if no value p Implementation -
public inline fun for_each_mut<Element>(o: &mut Option<Element>, f: |&mut Element|) {
-    if (is_some(o)) {
-        f(borrow_mut(o))
+
public inline fun for_each_mut<Element>(self: &mut Option<Element>, f: |&mut Element|) {
+    if (is_some(self)) {
+        f(borrow_mut(self))
     }
 }
 
@@ -711,7 +711,7 @@ Apply the function to the optional element reference. Does nothing if no value p Folds the function over the optional element. -
public fun fold<Accumulator, Element>(o: option::Option<Element>, init: Accumulator, f: |(Accumulator, Element)|Accumulator): Accumulator
+
public fun fold<Accumulator, Element>(self: option::Option<Element>, init: Accumulator, f: |(Accumulator, Element)|Accumulator): Accumulator
 
@@ -721,14 +721,14 @@ Folds the function over the optional element.
public inline fun fold<Accumulator, Element>(
-    o: Option<Element>,
+    self: Option<Element>,
     init: Accumulator,
     f: |Accumulator,Element|Accumulator
 ): Accumulator {
-    if (is_some(&o)) {
-        f(init, destroy_some(o))
+    if (is_some(&self)) {
+        f(init, destroy_some(self))
     } else {
-        destroy_none(o);
+        destroy_none(self);
         init
     }
 }
@@ -745,7 +745,7 @@ Folds the function over the optional element.
 Maps the content of an option.
 
 
-
public fun map<Element, OtherElement>(o: option::Option<Element>, f: |Element|OtherElement): option::Option<OtherElement>
+
public fun map<Element, OtherElement>(self: option::Option<Element>, f: |Element|OtherElement): option::Option<OtherElement>
 
@@ -754,11 +754,11 @@ Maps the content of an option. Implementation -
public inline fun map<Element, OtherElement>(o: Option<Element>, f: |Element|OtherElement): Option<OtherElement> {
-    if (is_some(&o)) {
-        some(f(destroy_some(o)))
+
public inline fun map<Element, OtherElement>(self: Option<Element>, f: |Element|OtherElement): Option<OtherElement> {
+    if (is_some(&self)) {
+        some(f(destroy_some(self)))
     } else {
-        destroy_none(o);
+        destroy_none(self);
         none()
     }
 }
@@ -775,7 +775,7 @@ Maps the content of an option.
 Maps the content of an option without destroying the original option.
 
 
-
public fun map_ref<Element, OtherElement>(o: &option::Option<Element>, f: |&Element|OtherElement): option::Option<OtherElement>
+
public fun map_ref<Element, OtherElement>(self: &option::Option<Element>, f: |&Element|OtherElement): option::Option<OtherElement>
 
@@ -785,9 +785,9 @@ Maps the content of an option without destroying the original option.
public inline fun map_ref<Element, OtherElement>(
-    o: &Option<Element>, f: |&Element|OtherElement): Option<OtherElement> {
-    if (is_some(o)) {
-        some(f(borrow(o)))
+    self: &Option<Element>, f: |&Element|OtherElement): Option<OtherElement> {
+    if (is_some(self)) {
+        some(f(borrow(self)))
     } else {
         none()
     }
@@ -805,7 +805,7 @@ Maps the content of an option without destroying the original option.
 Filters the content of an option
 
 
-
public fun filter<Element: drop>(o: option::Option<Element>, f: |&Element|bool): option::Option<Element>
+
public fun filter<Element: drop>(self: option::Option<Element>, f: |&Element|bool): option::Option<Element>
 
@@ -814,9 +814,9 @@ Filters the content of an option Implementation -
public inline fun filter<Element:drop>(o: Option<Element>, f: |&Element|bool): Option<Element> {
-    if (is_some(&o) && f(borrow(&o))) {
-        o
+
public inline fun filter<Element:drop>(self: Option<Element>, f: |&Element|bool): Option<Element> {
+    if (is_some(&self) && f(borrow(&self))) {
+        self
     } else {
         none()
     }
@@ -834,7 +834,7 @@ Filters the content of an option
 Returns true if the option contains an element which satisfies predicate.
 
 
-
public fun any<Element>(o: &option::Option<Element>, p: |&Element|bool): bool
+
public fun any<Element>(self: &option::Option<Element>, p: |&Element|bool): bool
 
@@ -843,8 +843,8 @@ Returns true if the option contains an element which satisfies predicate. Implementation -
public inline fun any<Element>(o: &Option<Element>, p: |&Element|bool): bool {
-    is_some(o) && p(borrow(o))
+
public inline fun any<Element>(self: &Option<Element>, p: |&Element|bool): bool {
+    is_some(self) && p(borrow(self))
 }
 
@@ -859,7 +859,7 @@ Returns true if the option contains an element which satisfies predicate. Utility function to destroy an option that is not droppable. -
public fun destroy<Element>(o: option::Option<Element>, d: |Element|)
+
public fun destroy<Element>(self: option::Option<Element>, d: |Element|)
 
@@ -868,8 +868,8 @@ Utility function to destroy an option that is not droppable. Implementation -
public inline fun destroy<Element>(o: Option<Element>, d: |Element|) {
-    let vec = to_vec(o);
+
public inline fun destroy<Element>(self: Option<Element>, d: |Element|) {
+    let vec = to_vec(self);
     vector::destroy(vec, |e| d(e));
 }
 
@@ -900,8 +900,8 @@ Utility function to destroy an option that is not droppable.
schema AbortsIfNone<Element> {
-    t: Option<Element>;
-    aborts_if spec_is_none(t) with EOPTION_NOT_SET;
+    self: Option<Element>;
+    aborts_if spec_is_none(self) with EOPTION_NOT_SET;
 }
 
@@ -1015,7 +1015,7 @@ because it's 0 for "none" or 1 for "some". ### Function `is_none` -
public fun is_none<Element>(t: &option::Option<Element>): bool
+
public fun is_none<Element>(self: &option::Option<Element>): bool
 
@@ -1023,7 +1023,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == spec_is_none(t);
+ensures result == spec_is_none(self);
 
@@ -1032,8 +1032,8 @@ because it's 0 for "none" or 1 for "some". -
fun spec_is_none<Element>(t: Option<Element>): bool {
-   vector::is_empty(t.vec)
+
fun spec_is_none<Element>(self: Option<Element>): bool {
+   vector::is_empty(self.vec)
 }
 
@@ -1044,7 +1044,7 @@ because it's 0 for "none" or 1 for "some". ### Function `is_some` -
public fun is_some<Element>(t: &option::Option<Element>): bool
+
public fun is_some<Element>(self: &option::Option<Element>): bool
 
@@ -1052,7 +1052,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == spec_is_some(t);
+ensures result == spec_is_some(self);
 
@@ -1061,8 +1061,8 @@ because it's 0 for "none" or 1 for "some". -
fun spec_is_some<Element>(t: Option<Element>): bool {
-   !vector::is_empty(t.vec)
+
fun spec_is_some<Element>(self: Option<Element>): bool {
+   !vector::is_empty(self.vec)
 }
 
@@ -1073,7 +1073,7 @@ because it's 0 for "none" or 1 for "some". ### Function `contains` -
public fun contains<Element>(t: &option::Option<Element>, e_ref: &Element): bool
+
public fun contains<Element>(self: &option::Option<Element>, e_ref: &Element): bool
 
@@ -1081,7 +1081,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == spec_contains(t, e_ref);
+ensures result == spec_contains(self, e_ref);
 
@@ -1090,8 +1090,8 @@ because it's 0 for "none" or 1 for "some". -
fun spec_contains<Element>(t: Option<Element>, e: Element): bool {
-   is_some(t) && borrow(t) == e
+
fun spec_contains<Element>(self: Option<Element>, e: Element): bool {
+   is_some(self) && borrow(self) == e
 }
 
@@ -1102,7 +1102,7 @@ because it's 0 for "none" or 1 for "some". ### Function `borrow` -
public fun borrow<Element>(t: &option::Option<Element>): &Element
+
public fun borrow<Element>(self: &option::Option<Element>): &Element
 
@@ -1110,7 +1110,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 include AbortsIfNone<Element>;
-ensures result == spec_borrow(t);
+ensures result == spec_borrow(self);
 
@@ -1119,8 +1119,8 @@ because it's 0 for "none" or 1 for "some". -
fun spec_borrow<Element>(t: Option<Element>): Element {
-   t.vec[0]
+
fun spec_borrow<Element>(self: Option<Element>): Element {
+   self.vec[0]
 }
 
@@ -1131,7 +1131,7 @@ because it's 0 for "none" or 1 for "some". ### Function `borrow_with_default` -
public fun borrow_with_default<Element>(t: &option::Option<Element>, default_ref: &Element): &Element
+
public fun borrow_with_default<Element>(self: &option::Option<Element>, default_ref: &Element): &Element
 
@@ -1139,7 +1139,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == (if (spec_is_some(t)) spec_borrow(t) else default_ref);
+ensures result == (if (spec_is_some(self)) spec_borrow(self) else default_ref);
 
@@ -1149,7 +1149,7 @@ because it's 0 for "none" or 1 for "some". ### Function `get_with_default` -
public fun get_with_default<Element: copy, drop>(t: &option::Option<Element>, default: Element): Element
+
public fun get_with_default<Element: copy, drop>(self: &option::Option<Element>, default: Element): Element
 
@@ -1157,7 +1157,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == (if (spec_is_some(t)) spec_borrow(t) else default);
+ensures result == (if (spec_is_some(self)) spec_borrow(self) else default);
 
@@ -1167,16 +1167,16 @@ because it's 0 for "none" or 1 for "some". ### Function `fill` -
public fun fill<Element>(t: &mut option::Option<Element>, e: Element)
+
public fun fill<Element>(self: &mut option::Option<Element>, e: Element)
 
pragma opaque;
-aborts_if spec_is_some(t) with EOPTION_IS_SET;
-ensures spec_is_some(t);
-ensures spec_borrow(t) == e;
+aborts_if spec_is_some(self) with EOPTION_IS_SET;
+ensures spec_is_some(self);
+ensures spec_borrow(self) == e;
 
@@ -1186,7 +1186,7 @@ because it's 0 for "none" or 1 for "some". ### Function `extract` -
public fun extract<Element>(t: &mut option::Option<Element>): Element
+
public fun extract<Element>(self: &mut option::Option<Element>): Element
 
@@ -1194,8 +1194,8 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 include AbortsIfNone<Element>;
-ensures result == spec_borrow(old(t));
-ensures spec_is_none(t);
+ensures result == spec_borrow(old(self));
+ensures spec_is_none(self);
 
@@ -1205,15 +1205,15 @@ because it's 0 for "none" or 1 for "some". ### Function `borrow_mut` -
public fun borrow_mut<Element>(t: &mut option::Option<Element>): &mut Element
+
public fun borrow_mut<Element>(self: &mut option::Option<Element>): &mut Element
 
include AbortsIfNone<Element>;
-ensures result == spec_borrow(t);
-ensures t == old(t);
+ensures result == spec_borrow(self);
+ensures self == old(self);
 
@@ -1223,7 +1223,7 @@ because it's 0 for "none" or 1 for "some". ### Function `swap` -
public fun swap<Element>(t: &mut option::Option<Element>, e: Element): Element
+
public fun swap<Element>(self: &mut option::Option<Element>, e: Element): Element
 
@@ -1231,9 +1231,9 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 include AbortsIfNone<Element>;
-ensures result == spec_borrow(old(t));
-ensures spec_is_some(t);
-ensures spec_borrow(t) == e;
+ensures result == spec_borrow(old(self));
+ensures spec_is_some(self);
+ensures spec_borrow(self) == e;
 
@@ -1243,7 +1243,7 @@ because it's 0 for "none" or 1 for "some". ### Function `swap_or_fill` -
public fun swap_or_fill<Element>(t: &mut option::Option<Element>, e: Element): option::Option<Element>
+
public fun swap_or_fill<Element>(self: &mut option::Option<Element>, e: Element): option::Option<Element>
 
@@ -1251,8 +1251,8 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == old(t);
-ensures spec_borrow(t) == e;
+ensures result == old(self);
+ensures spec_borrow(self) == e;
 
@@ -1262,7 +1262,7 @@ because it's 0 for "none" or 1 for "some". ### Function `destroy_with_default` -
public fun destroy_with_default<Element: drop>(t: option::Option<Element>, default: Element): Element
+
public fun destroy_with_default<Element: drop>(self: option::Option<Element>, default: Element): Element
 
@@ -1270,7 +1270,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == (if (spec_is_some(t)) spec_borrow(t) else default);
+ensures result == (if (spec_is_some(self)) spec_borrow(self) else default);
 
@@ -1280,7 +1280,7 @@ because it's 0 for "none" or 1 for "some". ### Function `destroy_some` -
public fun destroy_some<Element>(t: option::Option<Element>): Element
+
public fun destroy_some<Element>(self: option::Option<Element>): Element
 
@@ -1288,7 +1288,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 include AbortsIfNone<Element>;
-ensures result == spec_borrow(t);
+ensures result == spec_borrow(self);
 
@@ -1298,14 +1298,14 @@ because it's 0 for "none" or 1 for "some". ### Function `destroy_none` -
public fun destroy_none<Element>(t: option::Option<Element>)
+
public fun destroy_none<Element>(self: option::Option<Element>)
 
pragma opaque;
-aborts_if spec_is_some(t) with EOPTION_IS_SET;
+aborts_if spec_is_some(self) with EOPTION_IS_SET;
 
@@ -1315,7 +1315,7 @@ because it's 0 for "none" or 1 for "some". ### Function `to_vec` -
public fun to_vec<Element>(t: option::Option<Element>): vector<Element>
+
public fun to_vec<Element>(self: option::Option<Element>): vector<Element>
 
@@ -1323,7 +1323,7 @@ because it's 0 for "none" or 1 for "some".
pragma opaque;
 aborts_if false;
-ensures result == t.vec;
+ensures result == self.vec;
 
diff --git a/aptos-move/framework/move-stdlib/doc/string.md b/aptos-move/framework/move-stdlib/doc/string.md index b45c55afe9902..319b862698c08 100644 --- a/aptos-move/framework/move-stdlib/doc/string.md +++ b/aptos-move/framework/move-stdlib/doc/string.md @@ -150,7 +150,7 @@ Tries to create a new string from a sequence of bytes. Returns a reference to the underlying byte vector. -
public fun bytes(s: &string::String): &vector<u8>
+
public fun bytes(self: &string::String): &vector<u8>
 
@@ -159,8 +159,8 @@ Returns a reference to the underlying byte vector. Implementation -
public fun bytes(s: &String): &vector<u8> {
-    &s.bytes
+
public fun bytes(self: &String): &vector<u8> {
+    &self.bytes
 }
 
@@ -175,7 +175,7 @@ Returns a reference to the underlying byte vector. Checks whether this string is empty. -
public fun is_empty(s: &string::String): bool
+
public fun is_empty(self: &string::String): bool
 
@@ -184,8 +184,8 @@ Checks whether this string is empty. Implementation -
public fun is_empty(s: &String): bool {
-    vector::is_empty(&s.bytes)
+
public fun is_empty(self: &String): bool {
+    vector::is_empty(&self.bytes)
 }
 
@@ -200,7 +200,7 @@ Checks whether this string is empty. Returns the length of this string, in bytes. -
public fun length(s: &string::String): u64
+
public fun length(self: &string::String): u64
 
@@ -209,8 +209,8 @@ Returns the length of this string, in bytes. Implementation -
public fun length(s: &String): u64 {
-    vector::length(&s.bytes)
+
public fun length(self: &String): u64 {
+    vector::length(&self.bytes)
 }
 
@@ -225,7 +225,7 @@ Returns the length of this string, in bytes. Appends a string. -
public fun append(s: &mut string::String, r: string::String)
+
public fun append(self: &mut string::String, r: string::String)
 
@@ -234,8 +234,8 @@ Appends a string. Implementation -
public fun append(s: &mut String, r: String) {
-    vector::append(&mut s.bytes, r.bytes)
+
public fun append(self: &mut String, r: String) {
+    vector::append(&mut self.bytes, r.bytes)
 }
 
@@ -250,7 +250,7 @@ Appends a string. Appends bytes which must be in valid utf8 format. -
public fun append_utf8(s: &mut string::String, bytes: vector<u8>)
+
public fun append_utf8(self: &mut string::String, bytes: vector<u8>)
 
@@ -259,8 +259,8 @@ Appends bytes which must be in valid utf8 format. Implementation -
public fun append_utf8(s: &mut String, bytes: vector<u8>) {
-    append(s, utf8(bytes))
+
public fun append_utf8(self: &mut String, bytes: vector<u8>) {
+    append(self, utf8(bytes))
 }
 
@@ -276,7 +276,7 @@ Insert the other string at the byte index in given string. The index must be at boundary. -
public fun insert(s: &mut string::String, at: u64, o: string::String)
+
public fun insert(self: &mut string::String, at: u64, o: string::String)
 
@@ -285,15 +285,15 @@ boundary. Implementation -
public fun insert(s: &mut String, at: u64, o: String) {
-    let bytes = &s.bytes;
+
public fun insert(self: &mut String, at: u64, o: String) {
+    let bytes = &self.bytes;
     assert!(at <= vector::length(bytes) && internal_is_char_boundary(bytes, at), EINVALID_INDEX);
-    let l = length(s);
-    let front = sub_string(s, 0, at);
-    let end = sub_string(s, at, l);
+    let l = length(self);
+    let front = sub_string(self, 0, at);
+    let end = sub_string(self, at, l);
     append(&mut front, o);
     append(&mut front, end);
-    *s = front;
+    *self = front;
 }
 
@@ -310,7 +310,7 @@ of the first byte not included (or the length of the string). The indices must b guaranteeing that the result is valid utf8. -
public fun sub_string(s: &string::String, i: u64, j: u64): string::String
+
public fun sub_string(self: &string::String, i: u64, j: u64): string::String
 
@@ -319,8 +319,8 @@ guaranteeing that the result is valid utf8. Implementation -
public fun sub_string(s: &String, i: u64, j: u64): String {
-    let bytes = &s.bytes;
+
public fun sub_string(self: &String, i: u64, j: u64): String {
+    let bytes = &self.bytes;
     let l = vector::length(bytes);
     assert!(
         j <= l && i <= j && internal_is_char_boundary(bytes, i) && internal_is_char_boundary(bytes, j),
@@ -341,7 +341,7 @@ guaranteeing that the result is valid utf8.
 Computes the index of the first occurrence of a string. Returns length(s) if no occurrence found.
 
 
-
public fun index_of(s: &string::String, r: &string::String): u64
+
public fun index_of(self: &string::String, r: &string::String): u64
 
@@ -350,8 +350,8 @@ Computes the index of the first occurrence of a string. Returns index_of(s: &String, r: &String): u64 { - internal_index_of(&s.bytes, &r.bytes) +
public fun index_of(self: &String, r: &String): u64 {
+    internal_index_of(&self.bytes, &r.bytes)
 }
 
diff --git a/aptos-move/framework/move-stdlib/sources/option.move b/aptos-move/framework/move-stdlib/sources/option.move index 1793abfe9bc20..50aefbe12d008 100644 --- a/aptos-move/framework/move-stdlib/sources/option.move +++ b/aptos-move/framework/move-stdlib/sources/option.move @@ -57,134 +57,134 @@ module std::option { aborts_if vector::length(vec) > 1; } - /// Return true if `t` does not hold a value - public fun is_none(t: &Option): bool { - vector::is_empty(&t.vec) + /// Return true if `self` does not hold a value + public fun is_none(self: &Option): bool { + vector::is_empty(&self.vec) } spec is_none { pragma opaque; aborts_if false; - ensures result == spec_is_none(t); + ensures result == spec_is_none(self); } - spec fun spec_is_none(t: Option): bool { - vector::is_empty(t.vec) + spec fun spec_is_none(self: Option): bool { + vector::is_empty(self.vec) } - /// Return true if `t` holds a value - public fun is_some(t: &Option): bool { - !vector::is_empty(&t.vec) + /// Return true if `self` holds a value + public fun is_some(self: &Option): bool { + !vector::is_empty(&self.vec) } spec is_some { pragma opaque; aborts_if false; - ensures result == spec_is_some(t); + ensures result == spec_is_some(self); } - spec fun spec_is_some(t: Option): bool { - !vector::is_empty(t.vec) + spec fun spec_is_some(self: Option): bool { + !vector::is_empty(self.vec) } - /// Return true if the value in `t` is equal to `e_ref` - /// Always returns `false` if `t` does not hold a value - public fun contains(t: &Option, e_ref: &Element): bool { - vector::contains(&t.vec, e_ref) + /// Return true if the value in `self` is equal to `e_ref` + /// Always returns `false` if `self` does not hold a value + public fun contains(self: &Option, e_ref: &Element): bool { + vector::contains(&self.vec, e_ref) } spec contains { pragma opaque; aborts_if false; - ensures result == spec_contains(t, e_ref); + ensures result == spec_contains(self, e_ref); } - spec fun spec_contains(t: Option, e: Element): bool { - is_some(t) && borrow(t) == e + spec fun spec_contains(self: Option, e: Element): bool { + is_some(self) && borrow(self) == e } - /// Return an immutable reference to the value inside `t` - /// Aborts if `t` does not hold a value - public fun borrow(t: &Option): &Element { - assert!(is_some(t), EOPTION_NOT_SET); - vector::borrow(&t.vec, 0) + /// Return an immutable reference to the value inside `self` + /// Aborts if `self` does not hold a value + public fun borrow(self: &Option): &Element { + assert!(is_some(self), EOPTION_NOT_SET); + vector::borrow(&self.vec, 0) } spec borrow { pragma opaque; include AbortsIfNone; - ensures result == spec_borrow(t); + ensures result == spec_borrow(self); } - spec fun spec_borrow(t: Option): Element { - t.vec[0] + spec fun spec_borrow(self: Option): Element { + self.vec[0] } - /// Return a reference to the value inside `t` if it holds one - /// Return `default_ref` if `t` does not hold a value - public fun borrow_with_default(t: &Option, default_ref: &Element): &Element { - let vec_ref = &t.vec; + /// Return a reference to the value inside `self` if it holds one + /// Return `default_ref` if `self` does not hold a value + public fun borrow_with_default(self: &Option, default_ref: &Element): &Element { + let vec_ref = &self.vec; if (vector::is_empty(vec_ref)) default_ref else vector::borrow(vec_ref, 0) } spec borrow_with_default { pragma opaque; aborts_if false; - ensures result == (if (spec_is_some(t)) spec_borrow(t) else default_ref); + ensures result == (if (spec_is_some(self)) spec_borrow(self) else default_ref); } - /// Return the value inside `t` if it holds one - /// Return `default` if `t` does not hold a value + /// Return the value inside `self` if it holds one + /// Return `default` if `self` does not hold a value public fun get_with_default( - t: &Option, + self: &Option, default: Element, ): Element { - let vec_ref = &t.vec; + let vec_ref = &self.vec; if (vector::is_empty(vec_ref)) default else *vector::borrow(vec_ref, 0) } spec get_with_default { pragma opaque; aborts_if false; - ensures result == (if (spec_is_some(t)) spec_borrow(t) else default); + ensures result == (if (spec_is_some(self)) spec_borrow(self) else default); } - /// Convert the none option `t` to a some option by adding `e`. - /// Aborts if `t` already holds a value - public fun fill(t: &mut Option, e: Element) { - let vec_ref = &mut t.vec; + /// Convert the none option `self` to a some option by adding `e`. + /// Aborts if `self` already holds a value + public fun fill(self: &mut Option, e: Element) { + let vec_ref = &mut self.vec; if (vector::is_empty(vec_ref)) vector::push_back(vec_ref, e) else abort EOPTION_IS_SET } spec fill { pragma opaque; - aborts_if spec_is_some(t) with EOPTION_IS_SET; - ensures spec_is_some(t); - ensures spec_borrow(t) == e; + aborts_if spec_is_some(self) with EOPTION_IS_SET; + ensures spec_is_some(self); + ensures spec_borrow(self) == e; } - /// Convert a `some` option to a `none` by removing and returning the value stored inside `t` - /// Aborts if `t` does not hold a value - public fun extract(t: &mut Option): Element { - assert!(is_some(t), EOPTION_NOT_SET); - vector::pop_back(&mut t.vec) + /// Convert a `some` option to a `none` by removing and returning the value stored inside `self` + /// Aborts if `self` does not hold a value + public fun extract(self: &mut Option): Element { + assert!(is_some(self), EOPTION_NOT_SET); + vector::pop_back(&mut self.vec) } spec extract { pragma opaque; include AbortsIfNone; - ensures result == spec_borrow(old(t)); - ensures spec_is_none(t); + ensures result == spec_borrow(old(self)); + ensures spec_is_none(self); } - /// Return a mutable reference to the value inside `t` - /// Aborts if `t` does not hold a value - public fun borrow_mut(t: &mut Option): &mut Element { - assert!(is_some(t), EOPTION_NOT_SET); - vector::borrow_mut(&mut t.vec, 0) + /// Return a mutable reference to the value inside `self` + /// Aborts if `self` does not hold a value + public fun borrow_mut(self: &mut Option): &mut Element { + assert!(is_some(self), EOPTION_NOT_SET); + vector::borrow_mut(&mut self.vec, 0) } spec borrow_mut { include AbortsIfNone; - ensures result == spec_borrow(t); - ensures t == old(t); + ensures result == spec_borrow(self); + ensures self == old(self); } - /// Swap the old value inside `t` with `e` and return the old value - /// Aborts if `t` does not hold a value - public fun swap(t: &mut Option, e: Element): Element { - assert!(is_some(t), EOPTION_NOT_SET); - let vec_ref = &mut t.vec; + /// Swap the old value inside `self` with `e` and return the old value + /// Aborts if `self` does not hold a value + public fun swap(self: &mut Option, e: Element): Element { + assert!(is_some(self), EOPTION_NOT_SET); + let vec_ref = &mut self.vec; let old_value = vector::pop_back(vec_ref); vector::push_back(vec_ref, e); old_value @@ -192,16 +192,16 @@ module std::option { spec swap { pragma opaque; include AbortsIfNone; - ensures result == spec_borrow(old(t)); - ensures spec_is_some(t); - ensures spec_borrow(t) == e; + ensures result == spec_borrow(old(self)); + ensures spec_is_some(self); + ensures spec_borrow(self) == e; } - /// Swap the old value inside `t` with `e` and return the old value; + /// Swap the old value inside `self` with `e` and return the old value; /// or if there is no old value, fill it with `e`. - /// Different from swap(), swap_or_fill() allows for `t` not holding a value. - public fun swap_or_fill(t: &mut Option, e: Element): Option { - let vec_ref = &mut t.vec; + /// Different from swap(), swap_or_fill() allows for `self` not holding a value. + public fun swap_or_fill(self: &mut Option, e: Element): Option { + let vec_ref = &mut self.vec; let old_value = if (vector::is_empty(vec_ref)) none() else some(vector::pop_back(vec_ref)); vector::push_back(vec_ref, e); @@ -210,27 +210,27 @@ module std::option { spec swap_or_fill { pragma opaque; aborts_if false; - ensures result == old(t); - ensures spec_borrow(t) == e; + ensures result == old(self); + ensures spec_borrow(self) == e; } - /// Destroys `t.` If `t` holds a value, return it. Returns `default` otherwise - public fun destroy_with_default(t: Option, default: Element): Element { - let Option { vec } = t; + /// Destroys `self.` If `self` holds a value, return it. Returns `default` otherwise + public fun destroy_with_default(self: Option, default: Element): Element { + let Option { vec } = self; if (vector::is_empty(&mut vec)) default else vector::pop_back(&mut vec) } spec destroy_with_default { pragma opaque; aborts_if false; - ensures result == (if (spec_is_some(t)) spec_borrow(t) else default); + ensures result == (if (spec_is_some(self)) spec_borrow(self) else default); } - /// Unpack `t` and return its contents - /// Aborts if `t` does not hold a value - public fun destroy_some(t: Option): Element { - assert!(is_some(&t), EOPTION_NOT_SET); - let Option { vec } = t; + /// Unpack `self` and return its contents + /// Aborts if `self` does not hold a value + public fun destroy_some(self: Option): Element { + assert!(is_some(&self), EOPTION_NOT_SET); + let Option { vec } = self; let elem = vector::pop_back(&mut vec); vector::destroy_empty(vec); elem @@ -238,106 +238,106 @@ module std::option { spec destroy_some { pragma opaque; include AbortsIfNone; - ensures result == spec_borrow(t); + ensures result == spec_borrow(self); } - /// Unpack `t` - /// Aborts if `t` holds a value - public fun destroy_none(t: Option) { - assert!(is_none(&t), EOPTION_IS_SET); - let Option { vec } = t; + /// Unpack `self` + /// Aborts if `self` holds a value + public fun destroy_none(self: Option) { + assert!(is_none(&self), EOPTION_IS_SET); + let Option { vec } = self; vector::destroy_empty(vec) } spec destroy_none { pragma opaque; - aborts_if spec_is_some(t) with EOPTION_IS_SET; + aborts_if spec_is_some(self) with EOPTION_IS_SET; } - /// Convert `t` into a vector of length 1 if it is `Some`, + /// Convert `self` into a vector of length 1 if it is `Some`, /// and an empty vector otherwise - public fun to_vec(t: Option): vector { - let Option { vec } = t; + public fun to_vec(self: Option): vector { + let Option { vec } = self; vec } spec to_vec { pragma opaque; aborts_if false; - ensures result == t.vec; + ensures result == self.vec; } /// Apply the function to the optional element, consuming it. Does nothing if no value present. - public inline fun for_each(o: Option, f: |Element|) { - if (is_some(&o)) { - f(destroy_some(o)) + public inline fun for_each(self: Option, f: |Element|) { + if (is_some(&self)) { + f(destroy_some(self)) } else { - destroy_none(o) + destroy_none(self) } } /// Apply the function to the optional element reference. Does nothing if no value present. - public inline fun for_each_ref(o: &Option, f: |&Element|) { - if (is_some(o)) { - f(borrow(o)) + public inline fun for_each_ref(self: &Option, f: |&Element|) { + if (is_some(self)) { + f(borrow(self)) } } /// Apply the function to the optional element reference. Does nothing if no value present. - public inline fun for_each_mut(o: &mut Option, f: |&mut Element|) { - if (is_some(o)) { - f(borrow_mut(o)) + public inline fun for_each_mut(self: &mut Option, f: |&mut Element|) { + if (is_some(self)) { + f(borrow_mut(self)) } } /// Folds the function over the optional element. public inline fun fold( - o: Option, + self: Option, init: Accumulator, f: |Accumulator,Element|Accumulator ): Accumulator { - if (is_some(&o)) { - f(init, destroy_some(o)) + if (is_some(&self)) { + f(init, destroy_some(self)) } else { - destroy_none(o); + destroy_none(self); init } } /// Maps the content of an option. - public inline fun map(o: Option, f: |Element|OtherElement): Option { - if (is_some(&o)) { - some(f(destroy_some(o))) + public inline fun map(self: Option, f: |Element|OtherElement): Option { + if (is_some(&self)) { + some(f(destroy_some(self))) } else { - destroy_none(o); + destroy_none(self); none() } } /// Maps the content of an option without destroying the original option. public inline fun map_ref( - o: &Option, f: |&Element|OtherElement): Option { - if (is_some(o)) { - some(f(borrow(o))) + self: &Option, f: |&Element|OtherElement): Option { + if (is_some(self)) { + some(f(borrow(self))) } else { none() } } /// Filters the content of an option - public inline fun filter(o: Option, f: |&Element|bool): Option { - if (is_some(&o) && f(borrow(&o))) { - o + public inline fun filter(self: Option, f: |&Element|bool): Option { + if (is_some(&self) && f(borrow(&self))) { + self } else { none() } } /// Returns true if the option contains an element which satisfies predicate. - public inline fun any(o: &Option, p: |&Element|bool): bool { - is_some(o) && p(borrow(o)) + public inline fun any(self: &Option, p: |&Element|bool): bool { + is_some(self) && p(borrow(self)) } /// Utility function to destroy an option that is not droppable. - public inline fun destroy(o: Option, d: |Element|) { - let vec = to_vec(o); + public inline fun destroy(self: Option, d: |Element|) { + let vec = to_vec(self); vector::destroy(vec, |e| d(e)); } @@ -350,7 +350,7 @@ module std::option { /// # Helper Schema spec schema AbortsIfNone { - t: Option; - aborts_if spec_is_none(t) with EOPTION_NOT_SET; + self: Option; + aborts_if spec_is_none(self) with EOPTION_NOT_SET; } } diff --git a/aptos-move/framework/move-stdlib/sources/string.move b/aptos-move/framework/move-stdlib/sources/string.move index 6a2ca69d00ec7..549aa2380e36a 100644 --- a/aptos-move/framework/move-stdlib/sources/string.move +++ b/aptos-move/framework/move-stdlib/sources/string.move @@ -30,48 +30,48 @@ module std::string { } /// Returns a reference to the underlying byte vector. - public fun bytes(s: &String): &vector { - &s.bytes + public fun bytes(self: &String): &vector { + &self.bytes } /// Checks whether this string is empty. - public fun is_empty(s: &String): bool { - vector::is_empty(&s.bytes) + public fun is_empty(self: &String): bool { + vector::is_empty(&self.bytes) } /// Returns the length of this string, in bytes. - public fun length(s: &String): u64 { - vector::length(&s.bytes) + public fun length(self: &String): u64 { + vector::length(&self.bytes) } /// Appends a string. - public fun append(s: &mut String, r: String) { - vector::append(&mut s.bytes, r.bytes) + public fun append(self: &mut String, r: String) { + vector::append(&mut self.bytes, r.bytes) } /// Appends bytes which must be in valid utf8 format. - public fun append_utf8(s: &mut String, bytes: vector) { - append(s, utf8(bytes)) + public fun append_utf8(self: &mut String, bytes: vector) { + append(self, utf8(bytes)) } /// Insert the other string at the byte index in given string. The index must be at a valid utf8 char /// boundary. - public fun insert(s: &mut String, at: u64, o: String) { - let bytes = &s.bytes; + public fun insert(self: &mut String, at: u64, o: String) { + let bytes = &self.bytes; assert!(at <= vector::length(bytes) && internal_is_char_boundary(bytes, at), EINVALID_INDEX); - let l = length(s); - let front = sub_string(s, 0, at); - let end = sub_string(s, at, l); + let l = length(self); + let front = sub_string(self, 0, at); + let end = sub_string(self, at, l); append(&mut front, o); append(&mut front, end); - *s = front; + *self = front; } /// Returns a sub-string using the given byte indices, where `i` is the first byte position and `j` is the start /// of the first byte not included (or the length of the string). The indices must be at valid utf8 char boundaries, /// guaranteeing that the result is valid utf8. - public fun sub_string(s: &String, i: u64, j: u64): String { - let bytes = &s.bytes; + public fun sub_string(self: &String, i: u64, j: u64): String { + let bytes = &self.bytes; let l = vector::length(bytes); assert!( j <= l && i <= j && internal_is_char_boundary(bytes, i) && internal_is_char_boundary(bytes, j), @@ -81,8 +81,8 @@ module std::string { } /// Computes the index of the first occurrence of a string. Returns `length(s)` if no occurrence found. - public fun index_of(s: &String, r: &String): u64 { - internal_index_of(&s.bytes, &r.bytes) + public fun index_of(self: &String, r: &String): u64 { + internal_index_of(&self.bytes, &r.bytes) } // Native API diff --git a/aptos-move/framework/supra-framework/doc/config_buffer.md b/aptos-move/framework/supra-framework/doc/config_buffer.md index b235b5f7f5f06..214505cd9b849 100644 --- a/aptos-move/framework/supra-framework/doc/config_buffer.md +++ b/aptos-move/framework/supra-framework/doc/config_buffer.md @@ -287,7 +287,7 @@ Typically used in X::on_new_epoch() where X is an on-chaon config. let key = type_info::type_name<T>(); aborts_if !simple_map::spec_contains_key(configs.configs, key); include any::UnpackAbortsIf<T> { - x: simple_map::spec_get(configs.configs, key) + self: simple_map::spec_get(configs.configs, key) }; }
@@ -318,7 +318,7 @@ Typically used in X::on_new_epoch() where X is an on-chaon config. let type_name = type_info::type_name<T>(); let configs = global<PendingConfigs>(@supra_framework); include spec_fun_does_exist<T>(type_name) ==> any::UnpackAbortsIf<T> { - x: simple_map::spec_get(configs.configs, type_name) + self: simple_map::spec_get(configs.configs, type_name) }; }
@@ -333,7 +333,7 @@ Typically used in X::on_new_epoch() where X is an on-chaon config. let type_name = type_info::type_name<T>(); let configs = global<PendingConfigs>(@supra_framework); include spec_fun_does_exist<T>(type_name) ==> any::UnpackRequirement<T> { - x: simple_map::spec_get(configs.configs, type_name) + self: simple_map::spec_get(configs.configs, type_name) }; }
diff --git a/aptos-move/framework/supra-framework/doc/reconfiguration_state.md b/aptos-move/framework/supra-framework/doc/reconfiguration_state.md index 9740a88d4f798..b2ad293c1c840 100644 --- a/aptos-move/framework/supra-framework/doc/reconfiguration_state.md +++ b/aptos-move/framework/supra-framework/doc/reconfiguration_state.md @@ -562,7 +562,7 @@ Abort if the current state is not "in progress". include copyable_any::type_name(global<State>(@supra_framework).variant).bytes == b"0x1::reconfiguration_state::StateActive" ==> copyable_any::UnpackAbortsIf<StateActive> { - x: global<State>(@supra_framework).variant + self: global<State>(@supra_framework).variant }; aborts_if copyable_any::type_name(global<State>(@supra_framework).variant).bytes != b"0x1::reconfiguration_state::StateActive"; diff --git a/aptos-move/framework/supra-framework/sources/configs/config_buffer.spec.move b/aptos-move/framework/supra-framework/sources/configs/config_buffer.spec.move index e68efb0ee693a..03f5b60fddf73 100644 --- a/aptos-move/framework/supra-framework/sources/configs/config_buffer.spec.move +++ b/aptos-move/framework/supra-framework/sources/configs/config_buffer.spec.move @@ -32,7 +32,7 @@ spec supra_framework::config_buffer { let key = type_info::type_name(); aborts_if !simple_map::spec_contains_key(configs.configs, key); include any::UnpackAbortsIf { - x: simple_map::spec_get(configs.configs, key) + self: simple_map::spec_get(configs.configs, key) }; } @@ -51,7 +51,7 @@ spec supra_framework::config_buffer { let configs = global(@supra_framework); // TODO(#12015) include spec_fun_does_exist(type_name) ==> any::UnpackAbortsIf { - x: simple_map::spec_get(configs.configs, type_name) + self: simple_map::spec_get(configs.configs, type_name) }; } @@ -61,7 +61,7 @@ spec supra_framework::config_buffer { let configs = global(@supra_framework); // TODO(#12015) include spec_fun_does_exist(type_name) ==> any::UnpackRequirement { - x: simple_map::spec_get(configs.configs, type_name) + self: simple_map::spec_get(configs.configs, type_name) }; } diff --git a/aptos-move/framework/supra-framework/sources/reconfiguration_state.spec.move b/aptos-move/framework/supra-framework/sources/reconfiguration_state.spec.move index 8b6d175e2c23f..687faaa4cd0fc 100644 --- a/aptos-move/framework/supra-framework/sources/reconfiguration_state.spec.move +++ b/aptos-move/framework/supra-framework/sources/reconfiguration_state.spec.move @@ -100,7 +100,7 @@ spec supra_framework::reconfiguration_state { include copyable_any::type_name(global(@supra_framework).variant).bytes == b"0x1::reconfiguration_state::StateActive" ==> copyable_any::UnpackAbortsIf { - x: global(@supra_framework).variant + self: global(@supra_framework).variant }; aborts_if copyable_any::type_name(global(@supra_framework).variant).bytes != b"0x1::reconfiguration_state::StateActive";