Integer type that is always the same as C's "int"

#1

Hi,

so i’m interfacing with a glib based library using the cpp crate. At one point, this library returns a “gint”, which is basically glib’s renamed version of C’s int. Unfortunately, int's size depends on the platform and the compiler, and it’s either 16, 32, or 64 bits long. Now if i want to use that value in rust, what type should it be? is there some type that’s platform dependant and will always be the same size as whatever the c++ compiler used by the cpp crate uses as its int type?

kind regards :slight_smile:

0 Likes

#2

https://docs.rs/libc/0.2.48/libc/type.c_int.html

1 Like

#3

thanks, so gcc/clang guarantee that it’s always 32bit?

0 Likes

#4

The libc crate specializes its type definitions for each target platform. It happens that all supported platforms currently use type c_int = i32, but this is not guaranteed for all platforms everywhere.

4 Likes

#5

For a concrete example where there is a difference, see c_long:
https://docs.rs/libc/0.2.48/x86_64-unknown-linux-gnu/libc/type.c_long.html
https://docs.rs/libc/0.2.48/i686-unknown-linux-gnu/libc/type.c_long.html

4 Likes

#6

BTW, you don’t need the libc crate. std::os::raw::* has these type definitions.

3 Likes