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 FFI for FixedDecimalFormat #680

Merged
merged 25 commits into from
May 4, 2021
Merged

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Apr 26, 2021

Todo:

  • Actual FFI for FixedDecimalFormat::format and returning strings across the boundary (blocked on discussion)
  • Tests
  • Cargo features to separate decimal/plurals (Potentially for a separate PR)

Fixes #663

@Manishearth Manishearth requested a review from sffc April 26, 2021 22:39
@Manishearth
Copy link
Member Author

Manishearth commented Apr 26, 2021

The lifetimes were not much of a problem, but we'll need to document them diligently.

A nice thing is that the icu_capi crate has rustdoc output that serves as FFI API documentation, so the lifetimes are still present.

@@ -0,0 +1,49 @@
macro_rules! c_enum {
Copy link
Member Author

Choose a reason for hiding this comment

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

I added one of these for enums but not structs because structs have the additional complexity that each field can need Into, which we can solve by either:

  • call into() on everything (allows for changes in the field type to get missed)
  • allow an is syntax in the field level, which does make the macro trickier to write (but still doable)

@Manishearth Manishearth marked this pull request as ready for review April 29, 2021 19:07
@Manishearth Manishearth requested a review from a team as a code owner April 29, 2021 19:07
@Manishearth Manishearth requested a review from zbraniecki April 29, 2021 19:07
@Manishearth
Copy link
Member Author

This is now ready for review!

@jira-pull-request-webhook
Copy link

Notice: the branch changed across the force-push!

  • Cargo.lock is different
  • components/capi/Cargo.toml is different
  • components/capi/examples/fixeddecimal/.gitignore is now changed in the branch
  • components/capi/examples/fixeddecimal/Makefile is now changed in the branch
  • components/capi/examples/fixeddecimal/test.c is now changed in the branch
  • components/capi/examples/pluralrules/Makefile is now changed in the branch
  • components/capi/include/custom_writeable.h is now changed in the branch
  • components/capi/include/decimal.h is now changed in the branch
  • components/capi/include/fixed_decimal.h is now changed in the branch
  • components/capi/src/custom_writeable.rs is now changed in the branch
  • components/capi/src/decimal.rs is different
  • components/capi/src/fixed_decimal.rs is different
  • components/capi/src/lib.rs is different
  • Makefile.toml is now changed in the branch

View Diff Across Force-Push

~ Your Friendly Jira-GitHub PR Checker Bot

@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2021

Codecov Report

Merging #680 (ec9e250) into main (6030ec1) will decrease coverage by 0.48%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #680      +/-   ##
==========================================
- Coverage   74.24%   73.75%   -0.49%     
==========================================
  Files         171      175       +4     
  Lines       10824    10865      +41     
==========================================
- Hits         8036     8014      -22     
- Misses       2788     2851      +63     
Impacted Files Coverage Δ
components/capi/src/custom_writeable.rs 0.00% <0.00%> (ø)
components/capi/src/decimal.rs 0.00% <0.00%> (ø)
components/capi/src/fixed_decimal.rs 0.00% <0.00%> (ø)
components/capi/src/lib.rs 100.00% <ø> (ø)
components/capi/src/macros.rs 0.00% <0.00%> (ø)
components/capi/src/pluralrules.rs 0.00% <ø> (ø)
components/datetime/src/skeleton.rs 80.29% <0.00%> (-1.06%) ⬇️
components/uniset/src/provider.rs 55.55% <0.00%> (ø)
components/plurals/src/provider.rs 12.50% <0.00%> (ø)
components/locale_canonicalizer/src/provider.rs 0.00% <0.00%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6030ec1...ec9e250. Read the comment docs.

@coveralls
Copy link

coveralls commented Apr 29, 2021

Pull Request Test Coverage Report for Build 1de26b34cda15b42b52d6143e01183ed0e35f7f8-PR-680

  • 0 of 77 (0.0%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 73.033%

Changes Missing Coverage Covered Lines Changed/Added Lines %
components/capi/src/fixed_decimal.rs 0 13 0.0%
components/capi/src/macros.rs 0 17 0.0%
components/capi/src/decimal.rs 0 22 0.0%
components/capi/src/custom_writeable.rs 0 25 0.0%
Totals Coverage Status
Change from base Build 8e93c88897cdd086c49524ef05c7f383a885d804: -0.4%
Covered Lines: 8141
Relevant Lines: 11147

💛 - Coveralls

components/capi/examples/fixeddecimal/test.c Outdated Show resolved Hide resolved
components/capi/src/fixed_decimal.rs Outdated Show resolved Hide resolved
components/capi/src/decimal.rs Outdated Show resolved Hide resolved
components/capi/include/custom_writeable.h Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/macros.rs Show resolved Hide resolved
/// - `buf` must be `cap` bytes long
/// - `grow()` must either return null or a valid buffer of at least the requested buffer size
/// - Rust code must call `ICU4XCustomWriteable::flush()` before releasing to C
pub struct ICU4XCustomWriteable {
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Let's bikeshed the name of this struct. Why did you put "Custom" in the name?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I copied the name from the doc, I'm not particularly attached to it

Copy link
Member

Choose a reason for hiding this comment

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

Why not just ICU4XWriteable?

Copy link
Member Author

Choose a reason for hiding this comment

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

works for me!

Manishearth and others added 2 commits April 29, 2021 14:47
@Manishearth Manishearth requested a review from sffc April 29, 2021 22:22
Comment on lines 18 to 19
/// - `flush()` and `grow()` will be passed `context` and the value should be valid for that
/// `context` may be null, however `flush()` and `grow()` must then be ready to receive it
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This comment is not relevant for safety of the struct, because:

  1. The value in context is only read on the C side.
  2. In Rust, it is already unsafe to reference a pointer.

I would delete this line and explain it further in the docs for context below.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's still important to document what flush and grow expect.

components/capi/src/custom_writeable.rs Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
Manishearth and others added 2 commits April 29, 2021 16:47
Co-authored-by: Shane F. Carr <shane@unicode.org>
@Manishearth Manishearth requested a review from sffc April 29, 2021 23:53
Copy link
Contributor

@dminor dminor left a comment

Choose a reason for hiding this comment

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

Looking good so far!

components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
buf,
len: 0,
// keep an extra byte in our pocket for the null terminator
cap: len - 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: To me, this feels more like example or test code, rather than something that is a core part of CustomWriteable. I'd prefer to see an implementation in C that lives as part of the example code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's a common enough use case that it's good to ship a basic CustomWriteable so that it's possible to experiment with ICU4X without too much boilerplate.

components/capi/src/custom_writeable.rs Outdated Show resolved Hide resolved
@Manishearth Manishearth requested a review from dminor April 30, 2021 18:55
Copy link
Member

@sffc sffc left a comment

Choose a reason for hiding this comment

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

(setting review bit)

@Manishearth Manishearth requested a review from sffc May 4, 2021 06:07
dminor
dminor previously approved these changes May 4, 2021
Copy link
Contributor

@dminor dminor left a comment

Choose a reason for hiding this comment

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

Thank you for spending the extra time on ICU4XCustomWriteable!

/// - `buf` must be `cap` bytes long
/// - `grow()` must either return null or a valid buffer of at least the requested buffer size
/// - Rust code must call `ICU4XCustomWriteable::flush()` before releasing to C
pub struct ICU4XCustomWriteable {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just ICU4XWriteable?

/// FFI version of [`FixedDecimal::multiply_pow10()`]. See its docs for more details.ICU4XFixedDecimal
///
/// Returns `true` if the multiplication was successful.
pub extern "C" fn icu4x_fixed_decimal_multiply_pow10(
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Add negate() here as well, since it is the only way to get -0. Consider optionally adding .signum() as well. We should strive to have full API coverage.

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel like methods reading from FixedDecimal aren't really that necessary in our public API for now.

My attitude here is more go go gradually and add stuff as necessary. But I added negate()

@Manishearth Manishearth requested a review from sffc May 4, 2021 21:33
sffc
sffc previously approved these changes May 4, 2021
@Manishearth Manishearth merged commit 2ff1a07 into unicode-org:main May 4, 2021
@Manishearth Manishearth deleted the fd-capi branch May 4, 2021 22:14
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.

CAPI for FixedDecimalFormat
5 participants