-
Notifications
You must be signed in to change notification settings - Fork 19
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
Support for casts in macros with smaller integers #15
base: master
Are you sure you want to change the base?
Conversation
Thanks for your contribution! I won't have time to look at it for at least a week, but maybe @emilio can |
It looks reasonable, but I don't have enough nom expertise to properly review. Thanks a lot for working on this! |
7e60551
to
2f087fd
Compare
@jethrogb Is there any chance to get this pull request merged? I am trying to upload my first crate to crates.io and this feature is the only blocker. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I like the general direction.
#define Int_n128 (int8_t)128 | ||
#define Int_n121 (int8_t)1234567 | ||
#define Int_111 (unsigned int)111.111 | ||
#define Int_4294967295 (unsigned int)-1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline
impl<'a> PRef<'a> { | ||
method!(cast<PRef<'a>,&[Token],TypeCast,::Error>, mut self, | ||
delimited!(p!("("), alt!( | ||
do_parse!(k!("unsigned") >> k!("int") >> (TypeCast::UnsignedInt(size_of::<c_uint>()))) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will choose the size based on the platform that bindgen runs on (generally the compilation host), that seems incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we handle cross compilation? unsigned int may be different size on different architectures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we should, but I don't have a good idea for how.
do_parse!(i!("uint32_t") >> (TypeCast::UnsignedInt(4))) | | ||
do_parse!(i!("int8_t") >> (TypeCast::SignedInt(1))) | | ||
do_parse!(i!("int16_t") >> (TypeCast::SignedInt(2))) | | ||
do_parse!(i!("int32_t") >> (TypeCast::SignedInt(4))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about long
, long long
, and 64-bit types? In general, it may be useful to handle unknown types here in some graceful manner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any integer type longer than 32 bits(size_t, uint64_t, long) is not supported since EvalResult::Int can not hold it. We need change the API to support 64-bit types.
impl EvalResult { | ||
fn cast(self, ctype: TypeCast) -> Self { | ||
use self::TypeCast::*; | ||
let round = |bits| 2i64.pow((bits) as u32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary parentheses around bits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove it.
impl EvalResult { | ||
fn cast(self, ctype: TypeCast) -> Self { | ||
use self::TypeCast::*; | ||
let round = |bits| 2i64.pow((bits) as u32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this doesn't implement rounding, maybe give it another name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is pow2
a proper name?
@crab2313, any update on this? |
Partially fix #4 in the most common cases.
Any integer type longer than 32 bits(size_t, uint64_t, long) is not supported since
EvalResult::Int
can not hold it. For example:Please give me some ideas to simplify the implementation. I know it is ugly.