Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
fixup! Add interface example
Browse files Browse the repository at this point in the history
  • Loading branch information
runtian-zhou committed Mar 22, 2023
1 parent 571e493 commit 1c522a4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
processed 5 tasks
processed 7 tasks
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ module 0x42.CoinInterface {
label b0:
return *&move(self).T<T>::merge;
}

public value<T>(self: &Self.T<T>, coin: &T): u64 {
let value: |&T| (u64);
let ret: u64;
label b0:
value = *&move(self).T<T>::value;
ret = call_function_pointer<|&T| (u64)>(move(coin), move(value));
return move(ret);
}
}

//# publish
Expand Down Expand Up @@ -80,6 +89,59 @@ module 0x42.BasicCoin1 {
}
}


//# publish
module 0x42.BasicCoin2 {
import 0x42.CoinInterface;

struct Coin has store, drop { value: u64 }

public zero(): Self.Coin {
label b0:
return Coin { value: 0 };
}

public mint(value: u64): Self.Coin {
label b0:
return Coin { value: move(value) };
}

public value(c: &Self.Coin): u64 {
label b0:
return *&move(c).Coin::value;
}

public merge(c: &mut Self.Coin, other: Self.Coin) {
let value: u64;
label b0:
Coin { value } = move(other);
*&mut move(c).Coin::value = (*&copy(c).Coin::value) + 1;
return;
}


public split(c: &mut Self.Coin, value: u64): Self.Coin {
let coin_value: u64;
label b0:
coin_value = *&copy(c).Coin::value;
assert(copy(coin_value) >= copy(value), 0);
*&mut copy(c).Coin::value = move(coin_value) - copy(value);
return Coin { value: move(value) };
}

public coin_interface(): CoinInterface.T<Self.Coin> {
let value: |&Self.Coin| (u64);
let split: |&mut Self.Coin, u64| (Self.Coin);
let merge: |&mut Self.Coin, Self.Coin| ();
let interface: CoinInterface.T<Self.Coin>;
label b0:
value = get_function_pointer(Self.value);
split = get_function_pointer(Self.split);
merge = get_function_pointer(Self.merge);
interface = CoinInterface.new_interface<Self.Coin>(move(value), move(split), move(merge));
return move(interface);
}
}
//# publish
module 0x42.CoinType {
struct Foo has drop {
Expand All @@ -91,13 +153,11 @@ module 0x42.CoinType {
module 0x42.GenericAdder {
import 0x42.CoinInterface;
public add_coins<T>(interface: &CoinInterface.T<T>, coin1: &T, coin2: &T): u64 {
let value: |&T| (u64);
let v1: u64;
let v2: u64;
label b0:
value = CoinInterface.value_func<T>(move(interface));
v1 = call_function_pointer<|&T| (u64)>(move(coin1), copy(value));
v2 = call_function_pointer<|&T| (u64)>(move(coin2), copy(value));
v1 = CoinInterface.value<T>(copy(interface), move(coin1));
v2 = CoinInterface.value<T>(move(interface), move(coin2));
return move(v1) + move(v2);
}
}
Expand All @@ -123,3 +183,23 @@ label b0:
return;
}

//# run
import 0x42.CoinInterface;
import 0x42.BasicCoin2;
import 0x42.GenericAdder;

main() {
let interface: CoinInterface.T<BasicCoin2.Coin>;
let coin1: BasicCoin2.Coin;
let coin2: BasicCoin2.Coin;
let v: u64;
label b0:
coin1 = BasicCoin2.mint(10);
coin2 = BasicCoin2.mint(20);
interface = BasicCoin2.coin_interface();

v = GenericAdder.add_coins<BasicCoin2.Coin>(&interface, &coin1, &coin2);
assert(move(v) == 30, 0);
return;
}

0 comments on commit 1c522a4

Please sign in to comment.