Skip to content

Commit

Permalink
Make multiply_pow10 be infallible (#2285)
Browse files Browse the repository at this point in the history
Make multiply_pow10 be infallible
  • Loading branch information
younies authored Aug 2, 2022
1 parent 9dd86f9 commit 1d4c6a1
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 105 deletions.
4 changes: 1 addition & 3 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,7 @@ where
};

// We store fractional seconds as nanoseconds, convert to seconds.
fraction
.multiply_pow10(-9)
.map_err(|_| Error::FixedDecimal)?;
fraction.multiply_pow10(-9);

seconds
.concatenate_right(fraction)
Expand Down
3 changes: 1 addition & 2 deletions components/decimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ let fdf = FixedDecimalFormatter::try_new(&provider, &Locale::UND.into(), Default
.expect("Data should load successfully");

let fixed_decimal = FixedDecimal::from(200050)
.multiplied_pow10(-2)
.expect("Operation is fully in range");
.multiplied_pow10(-2);

assert_eq!("2,000.50", fdf.format(&fixed_decimal).write_to_string());
```
Expand Down
4 changes: 1 addition & 3 deletions components/decimal/src/grouper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ fn test_grouper() {
];
for cas in &cases {
for i in 0..4 {
let dec = FixedDecimal::from(1)
.multiplied_pow10((i as i16) + 3)
.unwrap();
let dec = FixedDecimal::from(1).multiplied_pow10((i as i16) + 3);
let provider = AnyPayloadProvider::new_owned::<DecimalSymbolsV1Marker>(
crate::provider::DecimalSymbolsV1 {
grouping_sizes: cas.sizes,
Expand Down
3 changes: 1 addition & 2 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
//! .expect("Data should load successfully");
//!
//! let fixed_decimal = FixedDecimal::from(200050)
//! .multiplied_pow10(-2)
//! .expect("Operation is fully in range");
//! .multiplied_pow10(-2);
//!
//! assert_eq!("2,000.50", fdf.format(&fixed_decimal).write_to_string());
//! ```
Expand Down
14 changes: 5 additions & 9 deletions components/plurals/benches/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ fn operands(c: &mut Criterion) {
.expect("Failed to parse a number into an operands.");
}
for s in &data.fixed_decimals {
let f: FixedDecimal = FixedDecimal::from(s.value)
.multiplied_pow10(s.exponent)
.unwrap();
let f: FixedDecimal = FixedDecimal::from(s.value).multiplied_pow10(s.exponent);
let _: PluralOperands = PluralOperands::from(black_box(&f));
}
})
Expand Down Expand Up @@ -108,19 +106,17 @@ fn operands(c: &mut Criterion) {
c.bench_function("plurals/operands/create/from_fixed_decimal", |b| {
b.iter(|| {
for s in &data.fixed_decimals {
let f: FixedDecimal = FixedDecimal::from(s.value)
.multiplied_pow10(s.exponent)
.unwrap();
let f: FixedDecimal = FixedDecimal::from(s.value).multiplied_pow10(s.exponent);
let _: PluralOperands = PluralOperands::from(black_box(&f));
}
});
});

{
let samples = [
FixedDecimal::from(1).multiplied_pow10(0).unwrap(),
FixedDecimal::from(123450).multiplied_pow10(-4).unwrap(),
FixedDecimal::from(2500).multiplied_pow10(-2).unwrap(),
FixedDecimal::from(1_i128).multiplied_pow10(0),
FixedDecimal::from(123450_i128).multiplied_pow10(-4),
FixedDecimal::from(2500_i128).multiplied_pow10(-2),
];
let mut group = c.benchmark_group("plurals/operands/create/from_fixed_decimal/samples");
for s in samples.iter() {
Expand Down
10 changes: 5 additions & 5 deletions components/plurals/src/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ use fixed_decimal::FixedDecimal;
/// use fixed_decimal::FixedDecimal;
/// use icu::plurals::PluralOperands;
/// assert_eq!(
/// Ok(PluralOperands {
/// PluralOperands {
/// i: 123,
/// v: 2,
/// w: 2,
/// f: 45,
/// t: 45,
/// c: 0,
/// }),
/// FixedDecimal::from(12345)
/// .multiplied_pow10(-2)
/// .map(|d| (&d).into())
/// },
/// (&FixedDecimal::from(12345)
/// .multiplied_pow10(-2))
/// .into()
/// )
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Default)]
Expand Down
4 changes: 1 addition & 3 deletions components/plurals/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ pub struct FixedDecimalInput {

impl From<&FixedDecimalInput> for FixedDecimal {
fn from(f: &FixedDecimalInput) -> Self {
FixedDecimal::from(f.from)
.multiplied_pow10(f.pow10)
.unwrap()
FixedDecimal::from(f.from).multiplied_pow10(f.pow10)
}
}

Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/c/include/ICU4XFixedDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_from_f

diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_fromstr(const char* v_data, size_t v_len);

bool ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);
void ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);

void ICU4XFixedDecimal_set_sign(ICU4XFixedDecimal* self, ICU4XFixedDecimalSign sign);

Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/cpp/docs/source/fixed_decimal_ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
See the `Rust documentation <https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html>`__ for more information.


.. cpp:function:: bool multiply_pow10(int16_t power)
.. cpp:function:: void multiply_pow10(int16_t power)

Multiply the :cpp:class:`ICU4XFixedDecimal` by a given power of ten.

Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/cpp/include/ICU4XFixedDecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_from_f

diplomat_result_box_ICU4XFixedDecimal_ICU4XError ICU4XFixedDecimal_create_fromstr(const char* v_data, size_t v_len);

bool ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);
void ICU4XFixedDecimal_multiply_pow10(ICU4XFixedDecimal* self, int16_t power);

void ICU4XFixedDecimal_set_sign(ICU4XFixedDecimal* self, ICU4XFixedDecimalSign sign);

Expand Down
6 changes: 3 additions & 3 deletions ffi/diplomat/cpp/include/ICU4XFixedDecimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ICU4XFixedDecimal {
*
* See the [Rust documentation](https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.multiply_pow10) for more information.
*/
bool multiply_pow10(int16_t power);
void multiply_pow10(int16_t power);

/**
* Set the sign of the [`ICU4XFixedDecimal`].
Expand Down Expand Up @@ -171,8 +171,8 @@ inline diplomat::result<ICU4XFixedDecimal, ICU4XError> ICU4XFixedDecimal::create
}
return diplomat_result_out_value;
}
inline bool ICU4XFixedDecimal::multiply_pow10(int16_t power) {
return capi::ICU4XFixedDecimal_multiply_pow10(this->inner.get(), power);
inline void ICU4XFixedDecimal::multiply_pow10(int16_t power) {
capi::ICU4XFixedDecimal_multiply_pow10(this->inner.get(), power);
}
inline void ICU4XFixedDecimal::set_sign(ICU4XFixedDecimalSign sign) {
capi::ICU4XFixedDecimal_set_sign(this->inner.get(), static_cast<capi::ICU4XFixedDecimalSign>(sign));
Expand Down
4 changes: 2 additions & 2 deletions ffi/diplomat/src/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ pub mod ffi {

/// Multiply the [`ICU4XFixedDecimal`] by a given power of ten.
#[diplomat::rust_link(fixed_decimal::decimal::FixedDecimal::multiply_pow10, FnInStruct)]
pub fn multiply_pow10(&mut self, power: i16) -> bool {
self.0.multiply_pow10(power).is_ok()
pub fn multiply_pow10(&mut self, power: i16) {
self.0.multiply_pow10(power)
}

/// Set the sign of the [`ICU4XFixedDecimal`].
Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/wasm/icu4x/lib/ICU4XFixedDecimal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class ICU4XFixedDecimal {
* See the {@link https://unicode-org.github.io/icu4x-docs/doc/fixed_decimal/decimal/struct.FixedDecimal.html#method.multiply_pow10 Rust documentation} for more information.
*/
multiply_pow10(power: i16): boolean;
multiply_pow10(power: i16): void;

/**
Expand Down
2 changes: 1 addition & 1 deletion ffi/diplomat/wasm/icu4x/lib/ICU4XFixedDecimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ICU4XFixedDecimal {
}

multiply_pow10(arg_power) {
return wasm.ICU4XFixedDecimal_multiply_pow10(this.underlying, arg_power);
wasm.ICU4XFixedDecimal_multiply_pow10(this.underlying, arg_power);
}

set_sign(arg_sign) {
Expand Down
3 changes: 1 addition & 2 deletions utils/fixed_decimal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ the individual digits of a number.
use fixed_decimal::FixedDecimal;

let dec = FixedDecimal::from(250)
.multiplied_pow10(-2)
.expect("Bounds are small");
.multiplied_pow10(-2);
assert_eq!("2.50", format!("{}", dec));

#[derive(Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion utils/fixed_decimal/benches/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn to_string_benches(c: &mut Criterion) {
use writeable::Writeable;

let objects = [
FixedDecimal::from(2250).multiplied_pow10(-2).unwrap(),
FixedDecimal::from(2250).multiplied_pow10(-2),
FixedDecimal::from(908070605040302010u128),
];

Expand Down
4 changes: 1 addition & 3 deletions utils/fixed_decimal/examples/permyriad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use writeable::Writeable;
fn main(_argc: isize, _argv: *const *const u8) -> isize {
icu_benchmark_macros::main_setup!();
let monetary_int = 19_9500;
let fixed_decimal = FixedDecimal::from(monetary_int)
.multiplied_pow10(-4)
.expect("-4 is well in range");
let fixed_decimal = FixedDecimal::from(monetary_int).multiplied_pow10(-4);

let mut output = String::with_capacity(fixed_decimal.write_len().capacity());
fixed_decimal
Expand Down
Loading

0 comments on commit 1d4c6a1

Please sign in to comment.