types::c
types::c provides type aliases that are compatible with standard C builtin types
and typedefs, as specified ISO/IEC 9899 and POSIX, as well as convenience
functions for working with C types. This module is useful for C interop, for
instance if an external function returns a long or a ssize, or if you
need to convert between a C string and a Hare string. The types provided here
shouldn't be used for most Hare code.
Some C types aren't provided by this module, since they are provided by the Hare
language itself:
--------------------------------------------------
| Hare type | C type | C header |
|===========|==============|=====================|
| bool | _Bool, bool | stdbool.h (C99-C17) |
|-----------|--------------|---------------------|
| int | (signed) int | - |
|-----------|--------------|---------------------|
| size | size_t | stddef.h |
|-----------|--------------|---------------------|
| uint | unsigned int | - |
|-----------|--------------|---------------------|
| uintptr | uintptr_t | stdint.h |
|-----------|--------------|---------------------|
| valist | va_list | stdarg.h |
--------------------------------------------------
Some C types are mostly compatible with Hare types, with minor differences:
--------------------------------------------------------------------------------
| Hare type | C type | Differences |
|===========|===================|==============================================|
| void | void | Hare's void is a zero-size type; C's void is |
| | | an incomplete opaque type. This distinction |
| | | isn't relevant for C interop. In both |
| | | languages, void pointers are used as generic |
| | | pointers. |
|-----------|-------------------|----------------------------------------------|
| f32 | float _Imaginary | The types are equivalent in representation, |
| f64 | double _Imaginary | but behavior differs when casting between |
| [2]f32 | float _Complex | real, imaginary, and complex types. |
| [2]f64 | double _Complex | |
--------------------------------------------------------------------------------
Additional low-level or implementation-specific types may be defined in rt.
Index
Types
type char;
type char16;
type char32;
type char8;
type int_fast16;
type int_fast32;
type int_fast64;
type int_fast8;
type int_least16;
type int_least32;
type int_least64;
type int_least8;
type intmax;
type intptr;
type long;
type longlong;
type ptrdiff;
type schar;
type short;
type ssize;
type uchar;
type uint_fast16;
type uint_fast32;
type uint_fast64;
type uint_fast8;
type uint_least16;
type uint_least32;
type uint_least64;
type uint_least8;
type uintmax;
type ulong;
type ulonglong;
type ushort;
type wchar;
type wint;
Functions
fn fromstr(const str) *char;
fn fromstr_buf(const str, []char) *char;
fn strlen(*const char) size;
fn tostr(*const char) (const str | utf8::invalid);
fn tostr_unsafe(*const char) const str;
Types
type char
type char = schar;
Integer type compatible with `char`, as specified by ISO/IEC 9899.
type char16
type char16 = uint_least16;
Integer type compatible with `char16_t`, as defined in <uchar.h> and
specified by ISO/IEC 9899:2011.
type char32
type char32 = uint_least32;
Integer type compatible with `char32_t`, as defined in <uchar.h> and
specified by ISO/IEC 9899:2011.
type char8
type char8 = uchar;
Integer type compatible with `char8_t`, as defined in <uchar.h> and specified
by ISO/IEC 9899:2023.
type int_fast16
type int_fast16 = i16;
Integer type compatible with `int_fast16_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_fast32
type int_fast32 = i32;
Integer type compatible with `int_fast32_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_fast64
type int_fast64 = i64;
Integer type compatible with `int_fast64_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_fast8
type int_fast8 = i8;
Integer type compatible with `int_fast8_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_least16
type int_least16 = i16;
Integer type compatible with `int_least16_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_least32
type int_least32 = i32;
Integer type compatible with `int_least32_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_least64
type int_least64 = i64;
Integer type compatible with `int_least64_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type int_least8
type int_least8 = i8;
Integer type compatible with `int_least8_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type intmax
type intmax = i64;
Integer type compatible with `intmax_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
Note that this type isn't necessarily the actual maximum integer type. This
type should only be used when required for C interop.
type intptr
type intptr = i64;
Integer type compatible with `intptr_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899.
type long
type long = i64;
Integer type compatible with `signed long`, as specified by ISO/IEC 9899.
type longlong
type longlong = i64;
Integer type compatible with `signed long long`, as specified by ISO/IEC
9899:1999.
type ptrdiff
type ptrdiff = i64;
Integer type compatible with `ptrdiff_t`, as defined in <stddef.h> and
specified by ISO/IEC 9899.
type schar
type schar = i8;
Integer type compatible with `signed char`, as specified by ISO/IEC 9899.
type short
type short = i16;
Integer type compatible with `signed short`, as specified by ISO/IEC 9899.
type ssize
type ssize = i64;
Integer type compatible with `ssize_t`, as defined in <sys/types.h> and
specified by POSIX.
type uchar
type uchar = u8;
Integer type compatible with `unsigned char`, as specified by ISO/IEC 9899.
type uint_fast16
type uint_fast16 = u16;
Integer type compatible with `uint_fast16_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_fast32
type uint_fast32 = u32;
Integer type compatible with `uint_fast32_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_fast64
type uint_fast64 = u64;
Integer type compatible with `uint_fast64_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_fast8
type uint_fast8 = u8;
Integer type compatible with `uint_fast8_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_least16
type uint_least16 = u16;
Integer type compatible with `uint_least16_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_least32
type uint_least32 = u32;
Integer type compatible with `uint_least32_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_least64
type uint_least64 = u64;
Integer type compatible with `uint_least64_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uint_least8
type uint_least8 = u8;
Integer type compatible with `uint_least8_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
type uintmax
type uintmax = u64;
Integer type compatible with `uintmax_t`, as defined in <stdint.h> and
specified by ISO/IEC 9899:1999.
Note that this type isn't necessarily the actual maximum integer type. This
type should only be used when required for C interop.
type ulong
type ulong = u64;
Integer type compatible with `unsigned long`, as specified by ISO/IEC 9899.
type ulonglong
type ulonglong = u64;
Integer type compatible with `unsigned long long`, as specified by ISO/IEC
9899:1999.
type ushort
type ushort = u16;
Integer type compatible with `unsigned short`, as specified by ISO/IEC 9899.
type wchar
type wchar = i32;
Integer type compatible with `wchar_t`, as defined in <stddef.h> and
specified by ISO/IEC 9899.
type wint
type wint = u32;
Integer type compatible with `wint_t`, as defined in <stdint.h> and specified
by ISO/IEC 9899:1994.
Functions
fn fromstr
fn fromstr(s: const str) *char;
Converts a Hare string to a C string. The result is allocated; the caller
must free it when they're done.
fn fromstr_buf
fn fromstr_buf(s: const str, sl: []char) *char;
Converts a Hare string to a C string. The result is stored into a
user-supplied buffer.
fn strlen
fn strlen(cstr: *const char) size;
Computes the length of a NUL-terminated C string, in octets, in O(n). The
computed length does not include the NUL terminator.
fn tostr
fn tostr(cstr: *const char) (const str |
utf8::invalid);
Converts a C string to a Hare string in O(n). If the string is not valid
UTF-8, return encoding::utf8::invalid.
fn tostr_unsafe
fn tostr_unsafe(cstr: *const char) const str;
Converts a C string to a Hare string in O(n), and does not check if it's
valid UTF-8.