Module sui::borrow
hot-potato-locked borrow 메커니즘을 제공하는 간단한 라이브러리이다.
Programmable transactions에서는 transaction 내에서 값을 빌려 사용하고 마지막에 되돌려 놓을 수 있다. hot-potato
Borrow는
object가 반환되었고 다른 object로 바꿔치기되지 않았음을 보장한다.
- Struct
Referent - Struct
Borrow - Constants
- Function
new - Function
borrow - Function
put_back - Function
destroy
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::hex;
use sui::object;
use sui::tx_context;
Struct Referent
T를 감싸고 borrow API를 제공하는 object이다.
public struct Referent<T: key, store> has store
Click to open
Fields
-
id: address -
value: std::option::Option<T>
Struct Borrow
object를 빌린 뒤 반드시 되돌려 놓게 하는 hot potato이다.
public struct Borrow
Click to open
Fields
-
ref: address -
obj: sui::object::ID
Constants
Borrow가 Referent와 일치하지 않는다.
const EWrongBorrow: u64 = 0;
Referent.value를 같은 타입의 다른 object로 바꿔치기하려는 시도이다.
const EWrongValue: u64 = 1;
Function new
새
Referent struct를 생성한다.
public fun new<T: key, store>(value: T, ctx: &mut sui::tx_context::TxContext): sui::borrow::Referent<T>
Click to open
Function borrow
Referent에서 T를 빌리고, T와 Borrow
hot potato를 받는다.
public fun borrow<T: key, store>(self: &mut sui::borrow::Referent<T>): (T, sui::borrow::Borrow)
Click to open
Implementation
public fun borrow<T: key + store>(self: &mut Referent<T>): (T, Borrow) {
let value = self.value.extract();
let id = object::id(&value);
(
value,
Borrow {
ref: self.id,
obj: id,
},
)
}
Function put_back
object와
Borrow hot potato를 되돌려 놓는다.
public fun put_back<T: key, store>(self: &mut sui::borrow::Referent<T>, value: T, borrow: sui::borrow::Borrow)
Click to open
Implementation
public fun put_back<T: key + store>(self: &mut Referent<T>, value: T, borrow: Borrow) {
let Borrow { ref, obj } = borrow;
assert!(object::id(&value) == obj, EWrongValue);
assert!(self.id == ref, EWrongBorrow);
self.value.fill(value);
}
Function destroy
Referent struct를 해체하고 값을 반환한다.
public fun destroy<T: key, store>(self: sui::borrow::Referent<T>): T