{"id":15297003,"url":"https://github.com/mqnfred/ffishim","last_synced_at":"2025-04-13T22:12:09.949Z","repository":{"id":57628705,"uuid":"256278754","full_name":"mqnfred/ffishim","owner":"mqnfred","description":"Generates ffi-compatible layer for your rust code","archived":false,"fork":false,"pushed_at":"2020-07-04T04:11:26.000Z","size":110,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-06T09:07:59.469Z","etag":null,"topics":["ffi","proc-macro","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mqnfred.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-16T17:05:46.000Z","updated_at":"2024-06-24T19:27:18.000Z","dependencies_parsed_at":"2022-09-26T20:12:12.949Z","dependency_job_id":null,"html_url":"https://github.com/mqnfred/ffishim","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mqnfred%2Fffishim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mqnfred%2Fffishim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mqnfred%2Fffishim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mqnfred%2Fffishim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mqnfred","download_url":"https://codeload.github.com/mqnfred/ffishim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248788916,"owners_count":21161728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ffi","proc-macro","rust"],"created_at":"2024-09-30T19:14:01.718Z","updated_at":"2025-04-13T22:12:09.920Z","avatar_url":"https://github.com/mqnfred.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FFIShim\n\n[![crates.io](https://img.shields.io/crates/v/ffishim_derive)](https://crates.io/ffishim_derive)\n[![badge](https://docs.rs/ffishim_derive/badge.svg)](https://docs.rs/ffishim_derive/)\n\nMany common rust types (like `String` for example) cannot be sent over FFI\nbecause their layouts in memory do not match the C ABI (`String` does not have\na null byte at its end.) It makes it hard to use native rust types and call\nthat code from FFI.\n\nThis crate provides a shim which will expose FFI-compatible data structures and\nfunction wrappers for native rust types and functions.\n\nHere is a quick example of rust code using a native `String` and calling that\nfrom C:\n\n```rust\n#[ffishim_function]\nfn hello(s: String) -\u003e String {\n    format!(\"Hello, {}!\", s)\n}\n```\n\nYou should be able to call this from C as follows:\n\n```c\n#include \"ffishim/header.h\"\nextern result_t *ffi_hello(char *s);\nint main() {\n\tchar *ffi = malloc(sizeof(char) * 4);\n\tffi[0] = \"f\";\n\tffi[1] = \"f\";\n\tffi[2] = \"i\";\n\tffi[3] = \"\\0\";\n\n\tresult_t *res = ffi_hello(ffi)\n\tif (res-\u003emessage != NULL) {\n\t\tprintf(\"error: %s\\n\", res-\u003emessage);\n\t\tfree(res-\u003emessage);\n\t} else {\n\t\tprintf(\"%s\\n\", res-\u003epayload);\n\t\tfree(res-\u003epayload);\n\t}\n\tfree(res);\n}\n```\n\nThis will print:\n\n```\nHello, ffi!\n```\n\nSince some type transformations might fail, the functions always return a\n`result_t` type, which contains a pointer to an error message and a payload. In\ncase of an error, the payload is nil and the message contains the error string.\n\n## More examples\n\nYou can find more examples of the shim's behavior by looking at the\n[`tests`][1] folder. The structure of the tests is as follow:\n\n - `src/lib.rs`: the rust library to expose\n - `Cargo.toml`: manifest of the rust library\n - `main.c`: the C code that uses this rust library\n - `expected_output`: contains the output expected from running the C program\n\nEvery test crate is a stand-alone app leveraging the ffishim library. They will\nshed light on how to setup the library and use it.\n\n## C ABI Disclaimer\n\nThis crate does not currently generate C ABI-compatible bindings. This is\nbecause it has been designed to be used together with [dustr][2], which\ngenerates Dart bindings on top of this ffi shim.\n\nBecause dart ffi support is still in alpha, it cannot quite consume the C ABI\njust yet. For example, [it does not support nested structs][3], and [structures\ncannot be passed by value to functions][4].\n\n## TODO/Limitations\n\nThis crate is still in beta. It is not fit for production use yet.\n\n### Bugs\n\n - The scalars type to libc types should either refer only libc types or none\n - Name conflicts arise across crates in .so: for example the function name\n   `free_config` if there is a `Config` struct in 2 crates. Need name mangling\n\n### Features\n\n - `from` should become `try_from`? (invalid rust string -\u003e CString unsafe)\n - Replace functions if `feature(ffishim)` instead of `ffi_` prefix\n - Reconsider dependence on `::anyhow::Error` explicitly\n - Type behavior for chrono dates\n\n### Testing\n\n - Add a \"complete\" test/example situation\n - Design benchmarking strategy and framework\n - Add test of enums\n\n### Documentation\n\n - Write README.md introduction on what this crate does\n - Write `ffishim_derive` documentation on how to use macros\n\n[1]: tests/\n[2]: https://github.com/mqnfred/dustr\n[3]: https://github.com/dart-lang/sdk/issues/37271\n[4]: https://github.com/dart-lang/sdk/issues/41062\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmqnfred%2Fffishim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmqnfred%2Fffishim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmqnfred%2Fffishim/lists"}