On this page:
ptr_  t
int8_  t
uint8_  t
int16_  t
uint16_  t
int32_  t
uint32_  t
int64_  t
uint64_  t
byte_  t
short_  t
ushort_  t
int_  t
uint_  t
long_  t
ulong_  t
intptr_  t
uintptr_  t
size_  t
ssize_  t
float_  t
double_  t
wchar_  t
intwchar_  t
bool_  t
boolint_  t
void_  t
string_  t
string_  utf16_  t
bytes_  t
bytes_  ptr_  t
path_  t
racket_  t
0.45+9.2.0.3

2.2 Base Foreign Types🔗ℹ

A type can only be written in specific type positions, such as in foreign.fun, foreign.struct, and mem.

Every type has a C representation and a Rhombus representation and conversion functions to get from one to the other. For example, the base type int_t is represented on the C side as a int, while it is represented in the racket side by a Int that fits into a 32-bit two’s complement representation.

foreign type

foreign.type ptr_t

The ptr_t type describes a generic pointer. On the C side, a generic pointer is represented as an address with the same representation as void*. On the Rhombus side, a generic pointer is represented as a pointer object. See also Foreign Pointers.

When an address is converted from C to Rhombus, then ptr_t produces a pointer object that references memory (assumed to be) not managed by Racket’s garbage collector. The ptr_t/gcable type implies that a pointer converted from C should be treated as (potentially) managed by Rhombus’s garbage collector. In both cases, conversion from Rhombus to C allows any pointer object.

The void_t* type is equivalent to ptr_t, and the (void_t*)/gcable type is equivalent to ptr_t/gcable.

foreign type

foreign.type int8_t

 

foreign type

foreign.type uint8_t

 

foreign type

foreign.type int16_t

 

foreign type

foreign.type uint16_t

 

foreign type

foreign.type int32_t

 

foreign type

foreign.type uint32_t

 

foreign type

foreign.type int64_t

 

foreign type

foreign.type uint64_t

Signed and unsigned integer scalar types of specific bit widths on the C side. All are represented as exact integers on the Rhombus side, constrained to a range that fits in the unsigned or two’s complement bit representation.

foreign type

foreign.type byte_t

 

foreign type

foreign.type short_t

 

foreign type

foreign.type ushort_t

 

foreign type

foreign.type int_t

 

foreign type

foreign.type uint_t

 

foreign type

foreign.type long_t

 

foreign type

foreign.type ulong_t

 

foreign type

foreign.type intptr_t

 

foreign type

foreign.type uintptr_t

 

foreign type

foreign.type size_t

 

foreign type

foreign.type ssize_t

Signed and unsigned integer scalar types of platform-specific bit widths. For consistently, a _t is added to the end of C type names like int to form a type name like int_t.

All are represented as exact integers on the Rhombus side, constrained to a range that fits in the platform-specific C representation.

foreign type

foreign.type float_t

 

foreign type

foreign.type double_t

IEEE floating-point number scalar types. On the C side, a float_t is 8 bytes, and a double_t is 16 bytes. On the Rhombus side, both are represented as flonums.

foreign type

foreign.type wchar_t

 

foreign type

foreign.type intwchar_t

On the C side, both wchar_t and intwchar_t occupy the same number of bytes. On the Rhombus side, a wchar_t is represented as a character, while a intwchar_t is a scalar type that is represented as an exact integer that fits into the platform-specific C representation.

The range of wchar_t on the C side may include integers that do not correspond to a Rhombus character, and it may omit values that do correspond to a Rhombus character. The Rhombus representation of a wchar_t is constrained to characters that fit in the C representation, and values from C that are are not representable as Rhombus characters are converted to the Unicode replacement character, Char"\uFFFD".

foreign type

foreign.type bool_t

 

foreign type

foreign.type boolint_t

Boolean scalar types. On the C side, bool_t corresponds to the C bool type from <stdbool.h>, while boolint_t corresponds to int (which is often used for a boolean representation in C-based libraries). On the Rhombus side, both are represented by boolean values when received from C, and and Rhombus value is allowed when converting to C (where #false is treated as false and all other values are treated as true).

foreign type

foreign.type void_t

A type with no representation on the C side and a #void representation on the Rhombus side. The void_t type can only be used for the result of a foreign procedure for foreign callback.

foreign type

foreign.type string_t

 

foreign type

foreign.type string_utf16_t

 

foreign type

foreign.type bytes_t

 

foreign type

foreign.type bytes_ptr_t

 

foreign type

foreign.type path_t

Types that are represented on the C side like ptr_t, but that are represented in Rhombus by conversion to and from strings, byte strings, and paths—or by #false to represent a NULL pointer.

For Rhombus to C conversion, the string_t type converts a Rhombus string to a null-terminated UTF-8 byte string and passes the address of the start of the byte string to the C side. The string_utf16_t type is similar, but conversion uses UTF-16 and a two-byte null terminator. The bytes_t type similarly copies a Rhombus byte string to add a null terminator, while bytes_ptr_t passes the start of a Rhombus byte string as-is, without adding a terminator (and where mutation of pointer content on the C side is reflected as changes to the byte string content). The path_t is like string_t, but for paths in the sense of CrossPath. All of these types convert #false on the Racket side to NULL on the C side.

When converting from C to Rhombus, the non-NULL pointer received from C is treated as a reference to a null-terminated C string, and a fresh Racket byte string is created to hold the content up to the null terminator. The string_t, string_utf16_t, and path_t types then convert that byte string to a string or path. A NULL from C is converted to #false for Rhombus.

> cast ~from (bytes_t) ~to (bytes_t) #"apple\0pie"

#"apple".copy()

> cast ~from (bytes_t) ~to (string_t) #"apple\0pie"

"apple"

> mem (cast ~from (bytes_ptr_t) ~to (byte_t*) #"apple\0pie")[6]

112

> cast ~from (string_t) ~to (path_t) "source"

Path("source")

> cast ~from (path_t) ~to (bytes_t) Path("source")

#"/home/racket/rhombus-build/plt/build/user/9.2.0.3/pkgs/rhombus-ffi/rhombus/ffi/scribblings/source"

  .copy()

> cast ~to (string_t) uintptr_to_ptr(0)

#false

foreign type

foreign.type racket_t

A type that is represented on the C side like ptr_t, but on the Rhombus side by an arbitrary value. This type can only be used for a procedure argument or result, and it will make sense only when interacting with a foreign procedure that is specifically aware of the Rhombus/Racket runtime system and cooperating with it.