# FFI Function Signatures Under this section we discuss the signature requirements of the exported FFI functions. ## C-style Name Export FFI functions must be exported under C names, that is, they must not go through the C++ name mangling mechanism. In Rust, you need to decorate the function with the `#[unsafe(no_mangle)]` attribute together with `extern "C"`, for example: ```rust #[unsafe(no_mangle)] pub extern "C" fn WFStartup() -> CError { // Function body omitted... } // More FFI functions omitted... ``` ## Export Name Style In this subsection, we discuss the naming style of exported functions. In this design, there are exactly two styles to choose from. We call them the Windows style and the POSIX style. Either style can be chosen freely, but within a single library the two styles must not be mixed. We recommend the following selection rule: if your library is Windows-only, choose the Windows style; otherwise, choose the POSIX style. ### Windows Style The Windows style format is `` for ordinary functions, and `` for opaque struct functions. In the format, `` is the name of the opaque struct and `` is the name of the function. Both of them must use PascalCase naming. For ``, it is a prefix identifier that is specific to this library. As we all know, C has no namespace concept, and all functions are exported into the same space. Therefore, avoiding name collisions between functions is an important task. By prepending `` to the function name, name duplication can be avoided as much as possible. For this reason, you need to choose `` carefully and try to pick a prefix that is unlikely to collide. In the Windows style, there are two spellings for ``. One is the all-uppercase style and the other is the style in which only the first letter is uppercase and all remaining letters are lowercase. Choose according to your own needs. In addition, the length of `` must not exceed 5 characters, and it must not contain anything other than letters and digits (that is, underscores are not allowed). The following examples show some legal and illegal Windows style function names: | Name | Verdict | Reason | |------|---------|--------| | `WFStartup` | Legal | An ordinary (non opaque struct) function. | | `WFIconGetHandle` | Legal | An opaque struct function whose `` is `Icon`. | | `WFWordHandleGetHandle` | Legal | An opaque struct function whose `` is `WordHandle`. | | `FlGetMessage` | Legal | Uses a ``, which is `Fl`, that capitalizes only its first letter. | | `MyRustGetMessage` | Illegal | ``, which is `MyRust`, does not keep all remaining letters lowercase. | | `My_RustGetMessage` | Illegal | `` contains an underscore. | | `IconGetHandle` | Illegal | Missing ``. | | `QwertyuiopGetData` | Illegal | ``, which is `Qwertyuiop`, is too long. | | `WFget_message` | Illegal | ``, which is `get_message`, is not PascalCase. | | `WFicon_handleGetMessage` | Illegal | ``, which is `icon_handle`, is not PascalCase. | ### POSIX Style The POSIX style format is `_` for ordinary functions, and `__` for opaque struct functions. The ``, `` and `` components in the format have the same meaning as in the Windows style, but the format is different. `` and `` must use snake_case naming. `` must be all lowercase, and it must not contain anything other than letters and digits (that is, underscores are not allowed). Its length and selection requirements are the same as in the Windows style. The following examples show some legal and illegal POSIX style function names: | Name | Verdict | Reason | |------|---------|--------| | `wf_startup` | Legal | An ordinary (non opaque struct) function. | | `wf_icon_get_handle` | Legal | An opaque struct function whose `` is `icon`. | | `wf_word_handle_get_handle` | Legal | An opaque struct function whose `` is `word_handle`. | | `Fl_get_message` | Illegal | ``, which is `Fl`, is not all lowercase. | | `my_rust_get_message` | Illegal | ``, which is `my_rust`, contains an underscore. | | `icon_get_handle` | Illegal | Missing ``. | | `qwertyuiop_get_data` | Illegal | ``, which is `qwertyuiop`, is too long. | | `wf_GetMessage` | Illegal | ``, which is `GetMessage`, is not snake_case. | | `wf_Icon_get_message` | Illegal | ``, which is `Icon`, is not snake_case. |