std::ffi - Rust (original) (raw)

Expand description

Utilities related to FFI bindings.

This module provides utilities to handle data across non-Rust interfaces, like other programming languages and the underlying operating system. It is mainly of use for FFI (Foreign Function Interface) bindings and code that needs to exchange C-like strings with other languages.

§Overview

Rust represents owned strings with the String type, and borrowed slices of strings with the str primitive. Both are always in UTF-8 encoding, and may contain nul bytes in the middle, i.e., if you look at the bytes that make up the string, there may be a \0 among them. Both String and str store their length explicitly; there are no nul terminators at the end of strings like in C.

C strings are different from Rust strings:

§Representations of non-Rust strings

CString and CStr are useful when you need to transfer UTF-8 strings to and from languages with a C ABI, like Python.

OsString and OsStr are useful when you need to transfer strings to and from the operating system itself, or when capturing the output of external commands. Conversions between OsString,OsStr and Rust strings work similarly to those for CStringand CStr.

§Conversions

§On Unix

On Unix, OsStr implements thestd::os::unix::ffi::[OsStrExt](../os/unix/ffi/trait.OsStrExt.html "os::unix::ffi::OsStrExt") trait, which augments it with two methods, from_bytes and as_bytes. These do inexpensive conversions from and to byte slices.

Additionally, on Unix OsString implements thestd::os::unix::ffi::[OsStringExt](../os/unix/ffi/trait.OsStringExt.html "os::unix::ffi::OsStringExt") trait, which provides from_vec and into_vec methods that consume their arguments, and take or produce vectors of u8.

§On Windows

An OsStr can be losslessly converted to a native Windows string. And a native Windows string can be losslessly converted to an OsString.

On Windows, OsStr implements thestd::os::windows::ffi::[OsStrExt](../os/windows/ffi/trait.OsStrExt.html "os::windows::ffi::OsStrExt") trait, which provides an encode_wide method. This provides an iterator that can be collected into a vector of u16. After a nul characters is appended, this is the same as a native Windows string.

Additionally, on Windows OsString implements thestd::os::windows:ffi::[OsStringExt](../os/windows/ffi/trait.OsStringExt.html "os::windows::ffi::OsStringExt")trait, which provides a from_wide method to convert a native Windows string (without the terminating nul character) to an OsString.

§Other platforms

Many other platforms provide their own extension traits in astd::os::*::ffi module.

§On all platforms

On all platforms, OsStr consists of a sequence of bytes that is encoded as a superset of UTF-8; see OsString for more details on its encoding on different platforms.

For limited, inexpensive conversions from and to bytes, see OsStr::as_encoded_bytes andOsStr::from_encoded_bytes_unchecked.

For basic string processing, see OsStr::slice_encoded_bytes.

c_strExperimental

CStr, CString, and related types.

os_strExperimental

The OsStr and OsString types and associated utilities.

CStr

Representation of a borrowed C string.

CString

A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the middle.

FromBytesUntilNulError

An error indicating that no nul byte was present.

FromVecWithNulError

An error indicating that a nul byte was not in the expected position.

IntoStringError

An error indicating invalid UTF-8 when converting a CString into a String.

NulError

An error indicating that an interior nul byte was found.

OsStr

Borrowed reference to an OS string (see OsString).

OsString

A type that can represent owned, mutable platform-native strings, but is cheaply inter-convertible with Rust strings.

VaListExperimental

A wrapper for a va_list

VaListImplExperimental

x86_64 ABI implementation of a va_list.

FromBytesWithNulError

An error indicating that a nul byte was not in the expected position.

c_void

Equivalent to C’s void type when used as a pointer.

c_char

Equivalent to C’s char type.

c_double

Equivalent to C’s double type.

c_float

Equivalent to C’s float type.

c_int

Equivalent to C’s signed int (int) type.

c_long

Equivalent to C’s signed long (long) type.

c_longlong

Equivalent to C’s signed long long (long long) type.

c_schar

Equivalent to C’s signed char type.

c_short

Equivalent to C’s signed short (short) type.

c_uchar

Equivalent to C’s unsigned char type.

c_uint

Equivalent to C’s unsigned int type.

c_ulong

Equivalent to C’s unsigned long type.

c_ulonglong

Equivalent to C’s unsigned long long type.

c_ushort

Equivalent to C’s unsigned short type.