Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Per-things trait. #4904

Merged
merged 14 commits into from
Feb 13, 2020
Merged

Per-things trait. #4904

merged 14 commits into from
Feb 13, 2020

Conversation

kianenigma
Copy link
Contributor

@kianenigma kianenigma commented Feb 12, 2020

Makes Per-XXX implement a trait for their common functionality.

Used in sp-phragmen, so henceforth the caller of Phragmén can specify their desired accuracy of results. This is needed for #4517. Making a separate PR for the sake of easy review.

@kianenigma kianenigma added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes labels Feb 12, 2020
@kianenigma kianenigma requested review from expenses and gui1117 and removed request for andresilva, mxinden and Demi-Marie February 12, 2020 13:10
@kianenigma kianenigma changed the title Kiz per thing trait Per-things trait. Feb 12, 2020
primitives/arithmetic/src/per_things.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/per_things.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Show resolved Hide resolved
/// Arithmetic types do all the usual stuff you'd expect numbers to do. They are guaranteed to
/// be able to represent at least `u32` values without loss, hence the trait implies `From<u32>`
/// and smaller ints. All other conversions are fallible.
pub trait SimpleArithmetic:
Copy link
Member

Choose a reason for hiding this comment

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

get out

Copy link
Member

Choose a reason for hiding this comment

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

So, SimpleArithmetic is already somewhat not that expressive. But splitting this now into 3 traits who all are doing basically the same.

We should up with something better.

  1. Have one trait, probably just called Arithmetic.
  2. MiniArithmetic is just a subset of SimpleArithmetic or? We probably can just merge Arithmetic and MiniArithmetic.
  3. SimpleArithmetic should get some better name, IDK :D The name should give a clear indication what the difference of the trait is.

Copy link
Contributor Author

@kianenigma kianenigma Feb 12, 2020

Choose a reason for hiding this comment

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

  1. Agree that the Simple prefix is not cool.

  2. Mini Arithmetic is not a subset. it is actually a superset logically, since it has the same operations but can be any size, while Simple Arithmetic can be u32 or bigger. But this is not visible trait-wise. Neither can be composed of the other one, at least in the current format.

Hence, I created the third one:

  • BasicArithmetic is just operations with no assumption on size
  • Mini and Simple can go on an compose it + add limitation on size.

Aside from the fact that Simple is not a nice prefix, I think this is actually quite nice and clean.

Copy link
Member

Choose a reason for hiding this comment

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

  1. Yeah, I actually meant it the way you described it :D

Mini isn't a nice prefix as well :P Maybe U8Arithmetic + U32Arithmetic? To make it clear on reading?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh and the fact that the prefix Mini also doesn't make much sense anymore :D

How about this

RuntimeArithmetic for the one which is u32 and above, since we also use it for blocknumber etc.
Arithmetic for the one which is generic on size.

primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
@kianenigma kianenigma mentioned this pull request Feb 12, 2020
12 tasks
primitives/phragmen/src/lib.rs Outdated Show resolved Hide resolved
primitives/phragmen/src/lib.rs Outdated Show resolved Hide resolved
primitives/phragmen/src/lib.rs Outdated Show resolved Hide resolved
primitives/phragmen/src/lib.rs Outdated Show resolved Hide resolved
primitives/phragmen/src/lib.rs Outdated Show resolved Hide resolved
primitives/phragmen/src/lib.rs Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved

/// Same as [`RuntimeArithmetic`] for integer types of any size, without any assumption of their size
/// being at least `u32`.
pub trait AnyArithmetic:
Copy link
Member

Choose a reason for hiding this comment

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

I still don't understand the reason for this?

Why do we need this? And it puts a requirement on the size by adding From<u8> :P

All these trait bounds can be moved to the BaseArithmetic, beside of the From<u8>. Will also this could be moved, because u8 is the smallest possible type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me see if I get you right:

What you recommend is putting all the TryInto<_>, TryFrom<_>, UniqueSaturatedInto<_> and UniqueSaturatedFrom<_>s in Base for all the 6 major integer types, since we don't know the size there, right? and then we can say AtLeast32Bit: Base + From<u8> + From<u16> + From<u32>.

The only minor problem with that would be that some of the traits would be duplicate and don't really make sense. I.e. a type ends up being From<u32> + TryFrom<u32>. Is this cool?

Copy link
Member

Choose a reason for hiding this comment

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

  1. Yeah base takes all these trait bounds and I also would give it From<u8> (there is no datatype that is smaller than u8)
  2. Every type implements From<Self> and also
impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

This means that every type automatically implements TryFrom for all types it implements From for.

/// Arithmetic types do all the usual stuff you'd expect numbers to do. They are guaranteed to
/// be able to represent at least `u32` values without loss, hence the trait implies `From<u32>`
/// and smaller ints. All other conversions are fallible.
pub trait RuntimeArithmetic:
Copy link
Member

Choose a reason for hiding this comment

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

I think we should call it AtLeast32Bit. The name RuntimeArithmetic doesn't make sense here, as this is nothing we enforce in the runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

32BitArithmetic?

Copy link
Member

Choose a reason for hiding this comment

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

That is not right, because we are not doing 32bit arithmetic here :D We just add From<u32> and From<u16>. That does say anything about the arithmetic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not fully happy with AtLeast32Bit but we'll go with it for now since I cannot come up with any other name :D 🤷🏻‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

Yeah me neither :D But I also don't have any better idea :(

@kianenigma kianenigma merged commit d940c02 into master Feb 13, 2020
@kianenigma kianenigma deleted the kiz-per-thing-trait branch February 13, 2020 12:09
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Feb 18, 2020
* Give perthigns the trait it always deserved.

* Make staking and phragmen work with the new generic per_thing

* Make everything work together 🔨

* a bit of cleanup

* Clean usage

* Bump.

* Fix name

* fix grumbles

* hopefully fix the ui test

* Some grumbles

* revamp traits again

* Better naming again.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants