-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: impl details of #[callback_unwrap]
#1319
Changes from all commits
72006ec
17deb8c
4c3b6c2
e5c1126
ebc93de
b28864a
40196cb
022c9af
7fe8625
bd7a8c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -447,11 +447,48 @@ extern crate quickcheck; | |
/// | ||
/// ### Other examples within repo: | ||
/// | ||
/// - `Cross-Contract Factorial` again [examples/cross-contract-calls](https://github.com/near/near-sdk-rs/blob/9596835369467cac6198e8de9a4b72a38deee4a5/examples/cross-contract-calls/high-level/src/lib.rs?plain=1#L26) | ||
/// - `Cross-Contract Factorial` again [examples/cross-contract-calls](https://github.com/near/near-sdk-rs/tree/master/examples/cross-contract-calls/high-level/src/lib.rs?plain=1#L26) | ||
/// - same example as [above](near#example-with-cross-contract-factorial), but uses [`Promise::then`] instead of [`env`](mod@env) host functions calls to set up a callback of `factorial_mult` | ||
/// - [examples/adder](https://github.com/near/near-sdk-rs/blob/9596835369467cac6198e8de9a4b72a38deee4a5/examples/adder/src/lib.rs?plain=1#L30) | ||
/// - [examples/adder](https://github.com/near/near-sdk-rs/blob/9596835369467cac6198e8de9a4b72a38deee4a5/examples/adder/src/lib.rs?plain=1#L31) | ||
/// - [examples/callback-results](https://github.com/near/near-sdk-rs/blob/9596835369467cac6198e8de9a4b72a38deee4a5/examples/callback-results/src/lib.rs?plain=1#L51) | ||
/// - [examples/callback-results](https://github.com/near/near-sdk-rs/tree/master/examples/callback-results/src/lib.rs?plain=1#L51) | ||
/// | ||
/// ### Implementation details of `#[callback_unwrap]` macro and **host functions** calls used | ||
/// | ||
/// ```rust | ||
/// # use near_sdk::near; | ||
/// # #[near(contract_state)] | ||
/// # pub struct Contract { /* .. */ } | ||
/// #[near] | ||
/// impl Contract { | ||
/// pub fn method( | ||
/// &mut self, | ||
/// regular: String, | ||
/// #[callback_unwrap] one: String, | ||
/// #[callback_unwrap] two: String | ||
/// ) { /* .. */ } | ||
/// } | ||
/// ``` | ||
/// | ||
/// For above `method` using the attribute on arguments, changes the body of function generated in [`#[near]` on mutating method](near#for-above-mutating-method-near-macro-defines-the-following-function) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this pr depends on #1307, as the doc anchor is first added there. RUSTDOCFLAGS: -D warnings
run: |
cargo doc -p near-sdk --features unstable,legacy,unit-testing,__macro-docs,__abi-generate
dj8yfo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// ```rust,no_run | ||
/// #[no_mangle] | ||
/// pub extern "C" fn method() { /* .. */ } | ||
/// ``` | ||
/// | ||
/// in the following way: | ||
/// | ||
/// 1. arguments, annotated with `#[callback_unwrap]`, are no longer expected to be included into `input`, | ||
/// deserialized in (step **3**, [`#[near]` on mutating method](near#for-above-mutating-method-near-macro-defines-the-following-function)). | ||
/// 2. for each argument, annotated with `#[callback_unwrap]`: | ||
/// 1. [`env::promise_result`] host function is called with corresponding index, starting from 0 | ||
/// (`0u64` for argument `one`, `1u64` for argument `two` above), and saved into `promise_result` variable | ||
/// 2. if the `promise_result` is a [`PromiseResult::Failed`] error, then [`env::panic_str`] host function is called to signal callback computation error | ||
/// 3. otherwise, if the `promise_result` is a [`PromiseResult::Successful`], it's unwrapped and saved to a `data` variable | ||
/// 4. `data` is deserialized similar to that as usual (step **3**, [`#[near]` on mutating method](near#for-above-mutating-method-near-macro-defines-the-following-function)), | ||
/// and saved to `deserialized_n_promise` variable | ||
/// 3. counterpart of (step **7**, [`#[near]` on mutating method](near#for-above-mutating-method-near-macro-defines-the-following-function)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make it more compact:
2.2
2.3 can be removed 2.4 otherwise, promise_result is deserialized ... But your variant is also lgtm |
||
/// original method is called `Contract::method(&mut state, deserialized_input_success.regular, deserialized_0_promise, deserialized_1_promise)`, | ||
/// as defined in `#[near]` annotated impl block | ||
/// | ||
/// ## `#[near(event_json(...))]` (annotates enums) | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[callback_vec]
isn't really compatible with#[callback_unwrap]
to be used in this manner, due to a feature??/bug??, because it revisits the same promise indices, already visited bycallback_unwrap
,so
others
vector in this example containeda
andb
as first elementsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created #1320 to describe with an example + assertion of current behaviour, because it looks like it's not obvious to get it with
cargo expand
example