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

refactor: api module overhaul #540

Merged
merged 24 commits into from
Dec 19, 2024
Merged

refactor: api module overhaul #540

merged 24 commits into from
Dec 19, 2024

Conversation

lwshang
Copy link
Contributor

@lwshang lwshang commented Dec 18, 2024

Description

Overhaul the api module. It now provides a complete binding for the system API. Stable memory API are moved into src/stable.rs.

The bindings are named consistently. Please refer to the top documentation comments in src/api.rs for additional details.
Existing bindings that are inconsistently named according to the new rules are now deprecated.

How Has This Been Tested?

Added e2e-tests/tests/api.rs.

e2e-tests/bin/api.rs illustrates how to use the API bindings.

Checklist:

  • The title of this PR complies with Conventional Commits.
  • I have edited the CHANGELOG accordingly.
  • I have made corresponding changes to the documentation.

@lwshang lwshang changed the base branch from main to next December 18, 2024 20:25
@lwshang lwshang marked this pull request as ready for review December 19, 2024 00:55
@lwshang lwshang requested a review from a team as a code owner December 19, 2024 00:55
@lwshang lwshang changed the title refactor: api overhaul refactor: api module overhaul Dec 19, 2024
Copy link
Member

@ericswanson-dfinity ericswanson-dfinity left a comment

Choose a reason for hiding this comment

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

Even if all of these changes are non-breaking and deprecations, there should be some mention in the changelog

Comment on lines +132 to +135
let size = (stable_size() << 16)
.try_into()
.expect("overflow: stable memory too large to read in one go");
let mut vec = Vec::with_capacity(size);

Choose a reason for hiding this comment

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

Could we use Result rather than panic in the interface here? The .expect(...) is up to us, and we could use Vec::try_with_capacity().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The content of this src/stable.rs file was directly copied from src/api/stable/mod.rs.
I will improve the internal implementation in a later PR.


let mut vec = self.memory.lock().unwrap();
let previous_len = vec.len() as u64;
let new_len = vec.len() as u64 + new_bytes;

Choose a reason for hiding this comment

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

Suggested change
let new_len = vec.len() as u64 + new_bytes;
let new_len = previous_len + new_bytes;

/// size that was reserved.
///
/// *Note*: Pages are 64KiB in WASM.
fn stable_grow(&self, new_pages: u64) -> Result<u64, StableMemoryError>;

Choose a reason for hiding this comment

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

I want to suggest calling the parameter additional_pages, which is unambiguous, but the public spec also uses new_pages.

Suggested change
fn stable_grow(&self, new_pages: u64) -> Result<u64, StableMemoryError>;
fn stable_grow(&self, additional_pages: u64) -> Result<u64, StableMemoryError>;

ic-cdk/src/api.rs Outdated Show resolved Hide resolved
ic-cdk/src/api.rs Outdated Show resolved Hide resolved
@lwshang
Copy link
Contributor Author

lwshang commented Dec 19, 2024

I will improve thesrc/stable.rs implementation in a separate PR short after.

Providing migration guide / changelog / docs will be the last task in #521 before I make the first alpha release.

@lwshang lwshang merged commit 65443f9 into next Dec 19, 2024
8 checks passed
@lwshang lwshang deleted the lwshang/api_overhaul branch December 19, 2024 17:54
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.

2 participants