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

Implement Ratio to hold the ratio variable for Units Conversion #4834

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 32 additions & 19 deletions components/experimental/src/units/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::prelude::*;
use num_bigint::BigInt;
use zerotrie::ZeroTrieSimpleAscii;
Expand Down Expand Up @@ -77,32 +78,44 @@ pub struct ConversionInfo<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub basic_units: ZeroVec<'data, MeasureUnitItem>,

/// Represents the numerator of the conversion factor.
#[cfg_attr(feature = "serde", serde(borrow))]
pub factor_num: ZeroVec<'data, u8>,

/// Represents the denominator of the conversion factor.
#[cfg_attr(feature = "serde", serde(borrow))]
pub factor_den: ZeroVec<'data, u8>,
/// Represents the exactness of the conversion factor.
pub exactness: Exactness,

/// Represents the sign of the conversion factor.
pub factor_sign: Sign,
/// Represents the numerator of the conversion factor.
#[zerovec::varule(RatioULE)]
pub factor: Ratio<'data>,

// TODO(#4311).
/// Represents the numerator of the offset.
#[cfg_attr(feature = "serde", serde(borrow))]
pub offset_num: ZeroVec<'data, u8>,
#[zerovec::varule(RatioULE)]
pub offset: Ratio<'data>,
Copy link
Member Author

Choose a reason for hiding this comment

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

error: field `factor` has no lifetimes to borrow
  --> components/experimental/src/units/provider.rs:81:5
   |
81 | /     /// Represents the numerator of the conversion factor.
82 | |     #[cfg_attr(feature = "serde", serde(borrow))]
83 | |     pub factor: RatioULE,
   | |________________________^

error: field `offset` has no lifetimes to borrow
  --> components/experimental/src/units/provider.rs:86:5
   |
86 | /     /// Represents the numerator of the offset.
87 | |     #[cfg_attr(feature = "serde", serde(borrow))]
88 | |     pub offset: RatioULE,
   | |________________________^

warning: unused import: `alloc::borrow::Cow`
  --> components/experimental/src/units/provider.rs:12:5
   |
12 | use alloc::borrow::Cow;
   |     ^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> components/experimental/src/units/provider.rs:83:17
   |
83 |     pub factor: RatioULE,
   |                 ^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `RatioULE`, the trait `Sized` is not implemented for `[u8]`, which is required by `RatioULE: Sized`
note: required because it appears within the type `RatioULE`
  --> components/experimental/src/units/provider.rs:94:24
   |
94 | #[zerovec::make_varule(RatioULE)]
   |                        ^^^^^^^^
   = note: only the last field of a struct may have a dynamically sized type
   = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
   |
83 |     pub factor: &RatioULE,
   |                 +
help: the `Box` type always has a statically known size and allocates its contents in the heap
   |
83 |     pub factor: Box<RatioULE>,
   |                 ++++        +

error[E0277]: the trait bound `RatioULE: AsULE` is not satisfied
  --> components/experimental/src/units/provider.rs:58:1
   |
58 | #[zerovec::make_varule(ConversionInfoULE)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsULE` is not implemented for `RatioULE`
   |
   = help: the following other types implement trait `AsULE`:
             bool
             char
             i8
             i16
             i32
             i64
             i128
             u8
           and 55 others
   = note: this error originates in the attribute macro `zerovec::make_varule` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `RatioULE: AsULE` is not satisfied
  --> components/experimental/src/units/provider.rs:83:17
   |
83 |     pub factor: RatioULE,
   |                 ^^^^^^^^ the trait `AsULE` is not implemented for `RatioULE`
   |
   = help: the following other types implement trait `AsULE`:
             bool
             char
             i8
             i16
             i32
             i64
             i128
             u8
           and 55 others

error[E0277]: the trait bound `RatioULE: AsULE` is not satisfied
  --> components/experimental/src/units/provider.rs:88:17
   |
88 |     pub offset: RatioULE,
   |                 ^^^^^^^^ the trait `AsULE` is not implemented for `RatioULE`
   |
   = help: the following other types implement trait `AsULE`:
             bool
             char
             i8
             i16
             i32
             i64
             i128
             u8
           and 55 others

For more information about this error, try `rustc --explain E0277`.
warning: `icu_experimental` (lib) generated 1 warning
error: could not compile `icu_experimental` (lib) due to 10 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
[cargo-make] ERROR - Error while executing command, exit code: 101
[cargo-make] WARN - Build Failed.

}

// TODO(#4311).
/// Represents the denominator of the offset.
// TODO(#4311).: Consider reducing the size of this struct while implementing the ULE.
#[zerovec::make_varule(RatioULE)]
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "datagen",
derive(databake::Bake),
databake(path = icu_experimental::units::provider),
)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize),
zerovec::derive(Serialize)
)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
zerovec::derive(Deserialize)
)]
pub struct Ratio<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub offset_den: ZeroVec<'data, u8>,
pub num: ZeroVec<'data, u8>,

/// Represents the sign of the offset.
pub offset_sign: Sign,
#[cfg_attr(feature = "serde", serde(borrow))]
pub den: ZeroVec<'data, u8>,

/// Represents the exactness of the conversion factor.
pub exactness: Exactness,
pub sign: Sign,
}

/// This enum is used to represent the sign of a constant value.
Expand Down