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

Add example usage comments to EnvAccess methods #797

Merged
merged 23 commits into from
Jul 2, 2021

Conversation

cmichi
Copy link
Collaborator

@cmichi cmichi commented May 26, 2021

Closes #644.

@cmichi cmichi requested a review from Robbepop May 26, 2021 02:01
Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

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

I like this PR very much and would love to have examples for the important self.env() API. However, some examples are currently flawed from what I can see and shouldn't/won't compile. Especially the #[ink(message)] examples are super useful but they need a lot of hidden context around them in order to make them compile properly. Non-compiling doc tests should be avoided at all cost since they are a nightmare for maintainability.

Comment on lines 213 to 214
/// let message = ink_prelude::format!("contract's account id is {:?}", account_id);
/// ink_env::debug_println(&message);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should use the upcoming debug println macros here as soon as they are available imo.

@cmichi
Copy link
Collaborator Author

cmichi commented May 27, 2021

I updated the PR, the code in the doc comment examples is compiled now. So please give it another round of review.

I briefly investigated using a proc. macro to reduce the code duplication, but quickly ran into issues with generating only part of a doc comment.

@cmichi
Copy link
Collaborator Author

cmichi commented Jun 18, 2021

@Robbepop I updated the PR, could you take another look please?

I experimented with a macro to reduce the code duplication in f731912, but always ran into macro hygiene errors, which from my understanding are not easy to resolve.

@Robbepop
Copy link
Collaborator

I experimented with a macro to reduce the code duplication in f731912, but always ran into macro hygiene errors, which from my understanding are not easy to resolve.

Yep, this is exactly what I expected unfortunately. I think for now we cannot do much about this.

@cmichi
Copy link
Collaborator Author

cmichi commented Jun 28, 2021

I updated the doc tests with some more vivid examples.

Also I had to fix #801 as part of the recent commits. I used your suggestion from that issue:

Since Weight is not customizable in the contracts pallet we should change the return type to plain u64.

Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

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

Seeing code like

ink_env::account_id::<T>().expect("couldn't decode contract account ID")

all over the place I come to believe we need a follow-up PR that replaces those expect calls with unwrap_or_else(|error| panic!("couldn't decode contract account ID: {}", error)) that provides more information about the underlying error.

Copy link
Collaborator

@Robbepop Robbepop left a comment

Choose a reason for hiding this comment

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

LGTM

Reminder: We should create an issue for the follow-up PR for #797 (review).

@cmichi cmichi merged commit da5a124 into master Jul 2, 2021
@cmichi cmichi deleted the cmichi-add-env-access-usage-examples branch July 2, 2021 14:06
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.

Add examples how to use EnvAccess public methods in the doc comments
3 participants