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

Signed type impls of Into<UInt> are now fallible #570

Merged
merged 1 commit into from
Jun 15, 2023
Merged

Conversation

zslayton
Copy link
Contributor

Fixes #569.

  • Signed integer primitives now implement TryInto<UInt> and fail if they are negative.
  • Coefficient::new now takes I: Into<Magnitude> where Magnitude is a wrapper that signed integers infallibly convert to via absolute value.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov
Copy link

codecov bot commented Jun 15, 2023

Codecov Report

Patch coverage: 64.91% and project coverage change: -0.08 ⚠️

Comparison is base (4f3724b) 83.30% compared to head (ac73f25) 83.23%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #570      +/-   ##
==========================================
- Coverage   83.30%   83.23%   -0.08%     
==========================================
  Files         104      104              
  Lines       19576    19611      +35     
  Branches    19576    19611      +35     
==========================================
+ Hits        16308    16323      +15     
- Misses       1730     1750      +20     
  Partials     1538     1538              
Impacted Files Coverage Δ
src/types/integer.rs 75.51% <22.72%> (-5.96%) ⬇️
src/types/coefficient.rs 87.14% <88.00%> (-0.15%) ⬇️
src/binary/int.rs 82.95% <100.00%> (ø)
src/binary/non_blocking/binary_buffer.rs 94.08% <100.00%> (ø)
src/binary/uint.rs 92.30% <100.00%> (ø)
src/lazy/binary/immutable_buffer.rs 87.02% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor Author

@zslayton zslayton left a comment

Choose a reason for hiding this comment

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

🗺️ PR tour

@@ -197,7 +197,7 @@ impl From<DecodedInt> for Coefficient {
} = int;
use types::Sign::{Negative, Positive};
let sign = if is_negative { Negative } else { Positive };
Coefficient::new(sign, value)
Coefficient::new(sign, value.unsigned_abs())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ All signed int primitives have an unsigned_abs() method that returns their absolute value as an unsigned integer of the closest-sized type (i8->u8, i16->u16, etc). Now the Int type has an unsigned_abs() method that returns a UInt.

@@ -690,7 +690,7 @@ mod tests {
let mut buffer = BinaryBuffer::new(&[0b1000_0000]);
let var_int = buffer.read_uint(buffer.remaining())?;
assert_eq!(var_int.size_in_bytes(), 1);
assert_eq!(var_int.value(), &UInt::from(128));
assert_eq!(var_int.value(), &UInt::from(128u64));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Signed integers can no longer be infallibly converted to a UInt. In the absence of other type information, Rust treats integers as i32s. This means that to call UInt::from(...), we need to mark our integers as an unsigned type.

///
/// Signed integers can infallibly implement `Into<Magnitude>` by using their
/// absolute value.
pub struct Magnitude(UInt);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🗺️ Previously, as described in #569, signed integers being converted to UInt would use their absolute value. This wrapper type offers the same behavior, but is narrowly used in APIs where that wouldn't be surprising.

@zslayton zslayton marked this pull request as ready for review June 15, 2023 22:44
@zslayton zslayton requested a review from popematt June 15, 2023 22:44
@zslayton zslayton merged commit c76655a into main Jun 15, 2023
20 of 21 checks passed
@zslayton zslayton deleted the try-from-signed branch June 15, 2023 23:05
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.

Converting from a signed integer to a UInt should be fallible
2 participants