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

Introduce FixedPoint::into_bits #12

Closed
wants to merge 3 commits into from

Conversation

KalitaAlexey
Copy link
Contributor

@KalitaAlexey KalitaAlexey commented Feb 10, 2021

We use FixedPoint for calculations, but at the end we want to convert it into u128.
Currently, we have to write:

let fp = ...;
(*fp.as_bits()).try_into()

or

let fp = ...;
fp.as_bits().clone().try_into()

I think it's much better to write:

let fp = ...;
fp.into_bits().try_into()

Also, made ArithmeticError and FromDecimalError Copy because they are simple and it enables Result<T, E>: Copy.

@@ -194,6 +194,10 @@ impl<I, P> FixedPoint<I, P> {
pub const fn as_bits(&self) -> &I {
&self.inner
}

pub fn into_bits(self) -> I {
Copy link
Owner

Choose a reason for hiding this comment

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

  1. I think it can be const fn.
  2. Add #[inline] in order to enable inlining across crates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. It consumes self. The compiler said no.
  2. Sure.

@@ -4,7 +4,7 @@ use core::fmt::{Display, Formatter, Result};
use derive_more::Error;

#[cfg_attr(feature = "std", derive(Error))]
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
Copy link
Owner

Choose a reason for hiding this comment

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

The Copy instance prevents us from adding a non-trivial object inside (i.e stacktraces).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the moment this error doesn't contain it.
Once you decide to add, you can remove Copy (adding a new variant is also considered to be a breaking change).

@KalitaAlexey
Copy link
Contributor Author

Travis CI failed due to some warnings from clippy that aren't related to my changes. Could you take a look?

@loyd loyd closed this Feb 16, 2021
@loyd
Copy link
Owner

loyd commented Feb 16, 2021

Closed in favor of #14

@KalitaAlexey KalitaAlexey deleted the into-bits branch February 17, 2021 15:55
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