Add documentation of compile-time width function alternative.#147
Open
dcsommer wants to merge 1 commit intorust-num:masterfrom
Open
Add documentation of compile-time width function alternative.#147dcsommer wants to merge 1 commit intorust-num:masterfrom
dcsommer wants to merge 1 commit intorust-num:masterfrom
Conversation
cuviper
reviewed
Jan 5, 2020
| /// All `PrimInt` types are expected to be fixed-width binary integers. The width can be queried | ||
| /// via `T::zero().count_zeros()`. The trait currently lacks a way to query the width at | ||
| /// compile-time. | ||
| /// compile-time, but `core::mem::size_of::<T>()` can be used in the meantime. |
Member
There was a problem hiding this comment.
Should we say 8 * size_of() so it counts bits?
Author
|
Yeah, I can qualify counting bytes vs bits in the text. Typically the size
of integers is specified by bytes, not bits, however. Do you think all
instances of the pattern `T::zero().count_zeros()` should be rewritten in
terms of `core::mem::size_of::<T>()` instead? I could modify the text to
that effect if you like.
…On Sat, Jan 4, 2020 at 9:51 PM Josh Stone ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/int.rs
<#147 (comment)>:
> @@ -22,7 +22,7 @@ use {Num, NumCast};
///
/// All `PrimInt` types are expected to be fixed-width binary integers. The width can be queried
/// via `T::zero().count_zeros()`. The trait currently lacks a way to query the width at
-/// compile-time.
+/// compile-time, but `core::mem::size_of::<T>()` can be used in the meantime.
Should we say 8 * size_of() so it counts bits?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#147?email_source=notifications&email_token=AAIIKIFWDUD6NOK6B3HXQZDQ4FYNRA5CNFSM4KB5PIX2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCQVUZOY#pullrequestreview-338382011>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIIKIBZLXCYL44UOP5YLMDQ4FYNRANCNFSM4KB5PIXQ>
.
|
Member
You think so? The bit width is right in the name:
AFAICS "all" is just this one instance, but sure, I'd be OK with preferring |
Author
|
I just remembered that in the case of `dyn T` we can't use
`core::mem::size_of<T>`, so there is definitely a use case for both runtime
and compile-time width calculation. I'll do it in terms of bits too.
…On Tue, Jan 7, 2020 at 12:02 PM Josh Stone ***@***.***> wrote:
Typically the size of integers is specified by bytes, not bits, however.
You think so? The bit width is right in the name: i32, u8, etc., and C's
stdint.h is similar.
Do you think all instances of the pattern T::zero().count_zeros() should
be rewritten in terms of core::mem::size_of::<T>() instead?
AFAICS "all" is just this one instance, but sure, I'd be OK with
preferring size_of since it's const.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#147?email_source=notifications&email_token=AAIIKIGSIKUGNGXG2F5GNV3Q4TNVFA5CNFSM4KB5PIX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIKC5EQ#issuecomment-571747986>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIIKIE4ZMMSJQS2A4GOCHDQ4TNVFANCNFSM4KB5PIXQ>
.
|
Member
|
How does |
Author
|
I mean when dealing with a `PrimInt` trait object, we can't know the size
of the type at compile time. For instance, in code dealing with a `Box<dyn
PrimInt>`, there isn't a way to use the `const fn`
`core::mem::size_of<T>()` to get the concrete type's width at compile time.
We need to dynamic dispatch on the trait object to calculate it.
…On Tue, Jan 7, 2020 at 12:19 PM Josh Stone ***@***.***> wrote:
How does dyn T apply in this context though? We already require PrimInt:
Sized.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#147?email_source=notifications&email_token=AAIIKIDT6L5UK4DUHAEHJVDQ4TPVNA5CNFSM4KB5PIX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIKEMEQ#issuecomment-571754002>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIIKIGXTOPD7J6UUMZJL53Q4TPVNANCNFSM4KB5PIXQ>
.
|
Member
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While the
PrimInttrait itself doesn't have compile time width functions, there are other ways to find the width of the underlying type. It is useful to present the alternative.