Module 0x2::borrow
A simple library that enables hot-potato-locked borrow mechanics.
With Programmable transactions, it is possible to borrow a value within a transaction, use it and put back in the end. Hot-potato Borrow makes sure the object is returned and was not swapped for another one.
- Struct
Referent
- Struct
Borrow
- Constants
- Function
new
- Function
borrow
- Function
put_back
- Function
destroy
use 0x1::option;
use 0x2::object;
use 0x2::tx_context;
Struct Referent
An object wrapping a T and providing the borrow API.
struct Referent<T: store, key> has store
Click to open
Fields
Struct Borrow
A hot potato making sure the object is put back once borrowed.
struct Borrow
Click to open
Fields
Constants
The Borrow does not match the Referent.
const EWrongBorrow: u64 = 0;
An attempt to swap the Referent.value with another object of the same type.
const EWrongValue: u64 = 1;
Function new
Create a new Referent struct
public fun new<T: store, key>(value: T, ctx: &mut tx_context::TxContext): borrow::Referent<T>
Click to open
Implementation
Function borrow
Borrow the T from the Referent receiving the T and a Borrow hot potato.
public fun borrow<T: store, key>(self: &mut borrow::Referent<T>): (T, borrow::Borrow)
Click to open
Implementation
Function put_back
Put an object and the Borrow hot potato back.
public fun put_back<T: store, key>(self: &mut borrow::Referent<T>, value: T, borrow: borrow::Borrow)
Click to open
Implementation
Function destroy
Unpack the Referent struct and return the value.
public fun destroy<T: store, key>(self: borrow::Referent<T>): T
Click to open