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

Recursive types and arrays
Can I determine the zero value of generic types?
How to slice a large Vec<i32> as &[u8]?
How to parse i64 from a string? [duplicate]
Why is variable scope dependent on the definition order?
Warning on “unstable” directive
How can I pass a socket as an argument to a function being called within a thread?
error: cannot move out of borrowed content on &mut self
Why does a reference not live long enough in case of “as_slice”?
Getting the error “error: the trait `core::marker::Sized` is not implemented” when trying to return the value from a vector
Traits with associated type in templates
Error handling best-practices
How to call count on an iterator and still use the iterator's items?
Caught between a lifetime and an FFI place
How to swap elements of array?
error: cannot assign to immutable indexed content `i[..]`

Categories

HOME
magento2
class
scroll
key
windows-10
nsview
whmcs
autocomplete
tibco
mithril.js
stack-overflow
fasm
android-externalstorage
jquery-select2-4
mpu6050
twisted
synthesis
reselect
nest-api
web-deployment
pickle
jaspersoft-studio
scheduled-tasks
multiple-tables
raml
bluemix-mobile-services
data-type-conversion
channel
pyopencl
raima
fusetools
plyr
high-availability
spreadsheetgear
jackrabbit-oak
history.js
macromedia
audit
steam-web-api
buildforge
grails-2.5
dreamfactory
ms-solver-foundation
aurelia-cli
imgur
slideshow
robust
dagger
doctrine-extensions
adblock
password-hash
gnome-terminal
polymaps
mechanicalturk
annotatorjs
haraka
nunit-console
flask-security
json-spirit
msgpack
decidable
opencyc
lowercase
enaml
knife
replicaset
algebraixlib
artisan
flow-js
ocmockito
azure-xplat-cli
signed
mutators
directorysearcher
operands
famo.us
configurable-product
dc
relationships
mobile-country-code
stripe.net
forceclose
incognito-mode
zend-search-lucene
sql-function
property-injection
xalan
popup-blocker
msinfo32
coordinate
thejit
kobold2d
monocross
series-40
fitch-proofs
wiktionary
azure-acs
directshow.net
bll
active-record-query
bass
aggregator
icicles
powergui
projectpier
accumulator
index.dat
suggestbox
comment-conventions
account-management

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