Skip to content
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

docs: document callback_unwrap attribute #1313

Merged
merged 9 commits into from
Feb 25, 2025

Conversation

TobieTom
Copy link
Contributor

@TobieTom TobieTom commented Feb 19, 2025

Fixes #1303

Adds documentation for the callback_unwrap attribute under the #[near] macro docs, including:

  • Description of functionality and usage
  • Integration with PromiseOrValue
  • Cross-contract factorial example
  • Rust doc annotations and references

For more details, open the Copilot Workspace session.

Fixes near#1303

Adds documentation for the `callback_unwrap` attribute under the `#[near]` macro docs, including:

- Description of functionality and usage
- Integration with `PromiseOrValue`
- Cross-contract factorial example 
- Error handling alternatives
- Rust doc annotations and references

Resolves near#1303

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/near/near-sdk-rs/issues/1303?shareId=XXXX-XXXX-XXXX-XXXX).
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.33%. Comparing base (9596835) to head (8f7cf09).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1313      +/-   ##
==========================================
+ Coverage   80.22%   80.33%   +0.10%     
==========================================
  Files         104      104              
  Lines       14844    14829      -15     
==========================================
+ Hits        11909    11913       +4     
+ Misses       2935     2916      -19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@dj8yfo dj8yfo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall this is the right direction, but all examples should at least compile (or run).

the second one isn't very clear what it's about, and it doesn't compile, which is a sign
that it's incorrect and/or incomplete.

this can be iterated quicker locally with cargo test --doc near-sdk/src/lib.rs

Comment on lines 311 to 313
/// pub fn factorial_mult(&self, n: u32, #[callback_unwrap] cur: u32) -> u32 {
/// n * cur
/// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for task #1312 (NOT in scope of current pr) it can be mentioned that the attribute works by doing the following:

#[no_mangle]
pub extern "C" fn factorial_mult() {
 ....
    let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
        ::near_sdk::PromiseResult::Successful(x) => x,
        _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
    };
    let cur: u32 = match ::near_sdk::serde_json::from_slice(&data) {
        Ok(deserialized) => deserialized,
        Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
    };
    let contract: CrossContract = ::near_sdk::env::state_read().unwrap_or_default();
    let result = CrossContract::factorial_mult(&contract, n, cur);
    
    ...

with links to corresponding [near_sdk::env] host-functions used

@dj8yfo
Copy link
Collaborator

dj8yfo commented Feb 25, 2025

it was also missed that there should've been a link from near_annotations module to this newly created anchor

@dj8yfo dj8yfo self-requested a review February 25, 2025 19:30
@dj8yfo dj8yfo merged commit 746e428 into near:master Feb 25, 2025
39 checks passed
@frol frol mentioned this pull request Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Task] document callback_unwrap
2 participants