rust


Conditional const definition based on a function call


I would like to write this C code in Rust:
#include <limits.h>
#if ((INT_MAX >> 15) >> 15) >= 1
#define LUAI_BITSINT 32
#else
/* 'int' always must have at least 16 bits */
#define LUAI_BITSINT 16
#endif
I could create a function returning my BITSINT but I want to avoid runtime cost. I don't know how to express isize::max_value() inside a macro in Rust, is it possible?
This would be my runtime code:
fn bitsint() -> usize {
if ((isize::max_value() >> 15) >> 15) >= 1 {
32
} else {
16
}
}
Currently, you cannot define a const or static value based on a function call. That requires the const functions feature to be completed.
That being said...
but I want to avoid runtime cost
tl;dr There is no runtime cost here, this is fine.
To avoid runtime cost, you should first ensure that you have runtime cost. To check, I used this code:
#[inline(never)]
fn bitsint() -> usize {
if ((isize::max_value() >> 15) >> 15) >= 1 {
12345
} else {
67890
}
}
fn main() {
println!("{}", bitsint());
}
I switched the values to make them easier to find in the assembly. The generated assembly does not have a function bitsint, even though I asked for it to not be inlined. It's hard to stop an optimizing compiler!
In the rest of the assembly, the value 67890 does not even occur, even though 12345 does.
An alternate solution may be to use conditional compilation. As an untested example:
#[cfg(target_pointer_width = "64")]
const LUAI_BITSINT: usize = 32;
#[cfg(not(target_pointer_width = "64"))]
const LUAI_BITSINT: usize = 16;
You could also use a build script to generate Rust code based on whatever conditions you want. This would create a constant that you could then include! into your code.

Related Links

How to reverse map of objects to vector of tuples by using an iterator?
How do I create an empty byte array?
How to return a struct with a generic type that implements the `Read` and `Write` traits?
Unused type parameters when binding a generic type to a trait that takes lifetime
Rust borrow checker prevents reuse after end of scope [duplicate]
Why does removing println! cause a Windows API function called through FFI to fail?
How to increase lifetime of the bindings [duplicate]
Is Rust's lexical grammar regular, context-free or context-sensitive?
How do I change where `cargo install` places binaries?
How to convert hex string to a float in Rust?
How do I include the end value in a range?
What files in a Cargo project should be in my .gitignore?
Repeating a Rust task with tokio_timer
error: failed to run custom build command for `onig_sys v61.1.0`
Can't use lazy static integer constant for the size of an array
Read reference from Option<&mut T> multiple times

Categories

HOME
eclipse-plugin
plpgsql
openlayers-3
informatica
google-play-services
yql
web2py
greasemonkey
flurry
scheduler
responsive-design
msmq
hystrix
lc3
fasm
chromecast
clish
reselect
tfsbuild
codefluent
social-tables
data-type-conversion
vuforia
crud
sales
social-networking
google-play-developer-api
universal
android-collapsingtoolbar
grails-2.5
numerics
code-rally
nanogallery
cheat-engine
android-n
assert
oauth2
distributed-caching
kitura
absolute
axis
adblock
wysihtml5
sonarqube5.2
webalizer
android-5.0-lollipop
android-webservice
exc-bad-access
connect-direct
zend-server
cognos-tm1
oid
pbx
yui3
google-identity-toolkit
xcode6.4
s-function
codecharge
apple-configurator
timestamping
utf-16
gravatar
flask-security
python-cffi
highest
supercomputers
swagger-maven-plugin
karma-coverage
artisan
shoes
kotlin-android-extensions
azure-xplat-cli
signed
nanomsg
directorysearcher
dos2unix
thoughtworks-go
oracle-adf-mobile
lumia-imaging-sdk
stripe.net
radius-protocol
assetic
eclim
revmob
coalesce
html-frames
tfs-sdk
examine
viewpagerindicator
firebird1.5
kext
cewolf
mongrel
activestate
eventaggregator
browser-based
mysqli-multi-query
service-factory

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App