From b6f075972ed0869440e6cccb20c5ee37d2ff7552 Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Fri, 29 May 2020 21:01:36 +0200 Subject: [PATCH] Renamed `repr_c` as `safer_ffi` --- Cargo.toml | 4 +- README.md | 28 +++---- ffi_tests/Cargo.toml | 4 +- ffi_tests/generated.h | 38 +++++----- ffi_tests/src/lib.rs | 8 +- guide/book.toml | 4 +- guide/src/SUMMARY.md | 2 +- guide/src/appendix/free-compatibility.md | 2 +- ...r_c-work.md => how-does-safer_ffi-work.md} | 6 +- guide/src/derive-reprc/_.md | 10 +-- guide/src/derive-reprc/enum.md | 4 +- guide/src/derive-reprc/struct.md | 8 +- guide/src/disclaimer.md | 2 +- guide/src/ffi-export/_.md | 7 +- guide/src/ffi-export/attributes.md | 4 +- guide/src/ffi-export/sanity-checks.md | 10 +-- guide/src/introduction/_.md | 2 +- guide/src/introduction/getting_started.md | 32 ++++---- guide/src/links.md | 30 ++++---- guide/src/motivation/_.md | 4 +- guide/src/motivation/repr-c-forall.md | 6 +- guide/src/motivation/traditional-ffi.md | 10 +-- guide/src/repr_c-types.md | 2 +- guide/src/simple-examples/max.md | 2 +- guide/src/simple-examples/string_concat.md | 2 +- guide/src/usage/_.md | 12 +-- guide/src/usage/cargo-toml.md | 22 +++--- guide/src/usage/lib-rs.md | 14 ++-- {guide/scripts => scripts}/build_docs.sh | 0 src/_lib.rs | 43 +++++++---- src/headers/_mod.rs | 74 +++++++++---------- src/layout/_mod.rs | 30 ++++---- src/layout/macros.rs | 45 ++++++----- src/proc_macro/Cargo.toml | 2 +- src/proc_macro/c_str.rs | 6 +- src/proc_macro/derives.rs | 12 +-- src/proc_macro/ffi_export.rs | 10 +-- src/utils/macros.rs | 4 +- src/utils/prelude.rs | 2 +- tests/layout_macros.rs | 8 +- 40 files changed, 264 insertions(+), 251 deletions(-) rename guide/src/appendix/{how-does-repr_c-work.md => how-does-safer_ffi-work.md} (90%) rename {guide/scripts => scripts}/build_docs.sh (100%) diff --git a/Cargo.toml b/Cargo.toml index bef2412fc4..3ffcbd98cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ path = "src/_lib.rs" [package] -name = "repr_c" +name = "safer_ffi" version = "0.1.0" authors = ["Daniel Henry-Mantilla "] edition = "2018" @@ -53,7 +53,7 @@ inventory = { version = "0.1.6", optional = true } proc-macro-hack = { version = "0.5.15", optional = true } [dependencies.proc_macro] -package = "repr_c-proc_macro" +package = "safer_ffi-proc_macro" path = "src/proc_macro" version = "0.1.0" diff --git a/README.md b/README.md index a122aa4828..1052206a11 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# `::repr_c` +# `::safer_ffi` + +See the [user guide](https://getditto.github.io/safer_ffi). # ⚠️ WIP ⚠️ @@ -25,16 +27,16 @@ edition = "2018" crate-type = ["staticlib"] [dependencies] -repr_c = { git = "https://github.com/getditto/rust-repr_c.git", features = ["proc_macros"] } +safer_ffi = { git = "https://github.com/getditto/rust-safer_ffi.git", features = ["proc_macros"] } [features] -c-headers = ["repr_c/headers"] +c-headers = ["safer_ffi/headers"] ``` ### `src/lib.rs` ```rust -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; #[derive_ReprC] #[repr(C)] @@ -63,11 +65,11 @@ fn print_point (point: &'_ Point) println!("{:?}", point); } -#[::repr_c::cfg_headers] +#[::safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - ::repr_c::headers::builder() + ::safer_ffi::headers::builder() .to_file("rust_points.h")? .generate() } @@ -87,13 +89,13 @@ cargo test --features c-headers -- generate_headers ```C /*! \file */ -/**************************************** - * * - * File auto-generated by `::repr_c`. * - * * - * Do not manually edit this file. * - * * - ****************************************/ +/******************************************* + * * + * File auto-generated by `::safer_ffi`. * + * * + * Do not manually edit this file. * + * * + *******************************************/ #ifndef __RUST_CRATE_NAME__ #define __RUST_CRATE_NAME__ diff --git a/ffi_tests/Cargo.toml b/ffi_tests/Cargo.toml index 765f65f554..983f2056c1 100644 --- a/ffi_tests/Cargo.toml +++ b/ffi_tests/Cargo.toml @@ -8,10 +8,10 @@ authors = ["Daniel Henry-Mantilla "] edition = "2018" [dependencies] -repr_c = { path = "..", features = ["proc_macros"] } +safer_ffi = { path = "..", features = ["proc_macros"] } [features] -generate-headers = ["repr_c/headers"] +generate-headers = ["safer_ffi/headers"] # [dev-dependencies] # cc = "1.0.50" diff --git a/ffi_tests/generated.h b/ffi_tests/generated.h index b4ab13f75e..3956aa5155 100644 --- a/ffi_tests/generated.h +++ b/ffi_tests/generated.h @@ -1,11 +1,11 @@ /*! \file */ -/**************************************** - * * - * File auto-generated by `::repr_c`. * - * * - * Do not manually edit this file. * - * * - ****************************************/ +/******************************************* + * * + * File auto-generated by `::safer_ffi`. * + * * + * Do not manually edit this file. * + * * + *******************************************/ #ifndef __RUST_FFI_TESTS__ #define __RUST_FFI_TESTS__ @@ -14,9 +14,19 @@ extern "C" { #endif +typedef struct foo foo_t; + +foo_t * new_foo (void); + +#include #include -#include + +int32_t read_foo ( + foo_t const * foo); + +void free_foo ( + foo_t * foo); /** \remark Has the same ABI as `uint8_t` **/ #ifdef DOXYGEN @@ -36,16 +46,6 @@ Bar_t void check_bar ( Bar_t _bar); -typedef struct foo foo_t; - -foo_t * new_foo (void); - -int32_t read_foo ( - foo_t const * foo); - -void free_foo ( - foo_t * foo); - /** \brief * Concatenate the two input strings into a new one. * @@ -87,7 +87,7 @@ void with_concat ( * typedef struct { * // Cannot be NULL * T * ptr; - * uintptr_t len; + * size_t len; * } slice_T; * ``` * diff --git a/ffi_tests/src/lib.rs b/ffi_tests/src/lib.rs index 4d616a92e0..24ce62524b 100644 --- a/ffi_tests/src/lib.rs +++ b/ffi_tests/src/lib.rs @@ -1,4 +1,4 @@ -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; /// Concatenate the two input strings into a new one. /// @@ -49,7 +49,7 @@ mod foo { use super::*; #[derive_ReprC] - #[repr_c::opaque("foo")] + #[ReprC::opaque("foo")] pub struct Foo { hidden: i32 } @@ -84,12 +84,12 @@ mod bar { {} } -#[repr_c::cfg_headers] +#[safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - let builder = ::repr_c::headers::builder(); + let builder = ::safer_ffi::headers::builder(); if ::std::env::var("HEADERS_TO_STDOUT").ok().map_or(false, |it| it == "1") { builder .to_writer(::std::io::stdout()) diff --git a/guide/book.toml b/guide/book.toml index 6bddbca435..2573b40f19 100644 --- a/guide/book.toml +++ b/guide/book.toml @@ -1,6 +1,6 @@ [book] -title = "`repr_c` User Guide" -guide = "A guide to learn how to use the `::repr_c` FFI framework" +title = "`safer_ffi` User Guide" +guide = "A guide to learn how to use the `::safer_ffi` FFI framework" authors = ["Daniel Henry-Mantilla"] language = "en" multilingual = false diff --git a/guide/src/SUMMARY.md b/guide/src/SUMMARY.md index 1f1371ada6..8f1c37023e 100644 --- a/guide/src/SUMMARY.md +++ b/guide/src/SUMMARY.md @@ -40,4 +40,4 @@ [Appendix: FFI and C compilation](appendix/c-compilation.md) -[Appendix: how does `repr_c` work](appendix/how-does-repr_c-work.md) +[Appendix: how does `safer_ffi` work](appendix/how-does-safer_ffi-work.md) diff --git a/guide/src/appendix/free-compatibility.md b/guide/src/appendix/free-compatibility.md index 2319e43c44..dd329be42d 100644 --- a/guide/src/appendix/free-compatibility.md +++ b/guide/src/appendix/free-compatibility.md @@ -1,7 +1,7 @@ # Appendix: Being compatible with `free()` ```rust,noplaypen -use ::repr_c::{prelude::*, ptr}; +use ::safer_ffi::{prelude::*, ptr}; /// A `Box`-like owned pointer type, but which can be freed using `free()`. #[derive_ReprC] diff --git a/guide/src/appendix/how-does-repr_c-work.md b/guide/src/appendix/how-does-safer_ffi-work.md similarity index 90% rename from guide/src/appendix/how-does-repr_c-work.md rename to guide/src/appendix/how-does-safer_ffi-work.md index 8f803408e4..6ed7b0aefa 100644 --- a/guide/src/appendix/how-does-repr_c-work.md +++ b/guide/src/appendix/how-does-safer_ffi-work.md @@ -1,6 +1,6 @@ {{#include ../links.md}} -# Appendix: how does `repr_c` work +# Appendix: how does `safer_ffi` work Most of the limitations of traditional FFI are related to the design of `cbindgen` and its being **implemented as a syntactic tool**: without access to @@ -22,13 +22,13 @@ course): - by encoding invariants and reflection within the type system, through a complex but stable use of helper traits. **This is the choice made by - `repr_c`**, whereby _two_ traits suffice to express the necssary semantics + `safer_ffi`**, whereby _two_ traits suffice to express the necssary semantics for FFI compatibility and integration: - the user-facing [`ReprC`] trait, implemented for types having a defined C layout: - - either directly provided by the `repr_c` crate (_c.f._ [its + - either directly provided by the `safer_ffi` crate (_c.f._ [its dedicated chapter][repr-c-forall]), - or implemented for custom types having the diff --git a/guide/src/derive-reprc/_.md b/guide/src/derive-reprc/_.md index 96cb0bc6b6..487f47d813 100644 --- a/guide/src/derive-reprc/_.md +++ b/guide/src/derive-reprc/_.md @@ -4,10 +4,10 @@ - [API Documentation][`ReprC`] -`ReprC` is the core trait around `repr_c`'s design. +`ReprC` is the core trait around `safer_ffi`'s design. - Feel free to [look at the appendix to understand why and - how][how-does-repr_c-work]. + how][how-does-safer_ffi-work]. Indeed, @@ -35,7 +35,7 @@ wrapper. A convenience macro is planned to be added to automate that step. - - when it is [a specialy-crafted type exported from the `repr_c` crate]( + - when it is [a specialy-crafted type exported from the `safer_ffi` crate]( ../motivation/repr-c-forall.md), - or **when it is a custom type that is `#[derive_ReprC]`-annotated**. @@ -64,10 +64,10 @@ Currently, the supported types for the attribute are: declaration: `typedef struct MyOpaque MyOpaque_t;` ```rust,noplaypen - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; #[derive_ReprC] - #[repr_c::opaque] + #[ReprC::opaque] struct MyOpaque { /* anything goes here */ } diff --git a/guide/src/derive-reprc/enum.md b/guide/src/derive-reprc/enum.md index 4775881722..73e0f23658 100644 --- a/guide/src/derive-reprc/enum.md +++ b/guide/src/derive-reprc/enum.md @@ -33,9 +33,9 @@ See [the reference for more info about them][rust-reference-fieldless-enums]. ### Usage ```rust,noplaypen -use ::repr_c::prelude:*; +use ::safer_ffi::prelude:*; -#[derive_ReprC] // <- `::repr_c`'s attribute +#[derive_ReprC] // <- `::safer_ffi`'s attribute #[repr(u8)] // <- explicit integer `repr` is mandatory! pub enum LogLevel { diff --git a/guide/src/derive-reprc/struct.md b/guide/src/derive-reprc/struct.md index f93a423ad4..56a741358f 100644 --- a/guide/src/derive-reprc/struct.md +++ b/guide/src/derive-reprc/struct.md @@ -6,9 +6,9 @@ ## Usage ```rust,noplaypen -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; -#[derive_ReprC] // <- `::repr_c`'s attribute +#[derive_ReprC] // <- `::safer_ffi`'s attribute #[repr(C)] // <- defined C layout is mandatory! pub struct Point { @@ -42,7 +42,7 @@ Point_t get_origin (void); `#[derive_ReprC]` supports generic structs: ```rust,noplaypen -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; /// The struct can be generic... #[derive_ReprC] @@ -100,7 +100,7 @@ Each monomorphization leads to its own C definition:
Transparent newtype wrapper ```rust,noplaypen -use ::repr_c::{prelude::*, ptr}; +use ::safer_ffi::{prelude::*, ptr}; /// A `Box`-like owned pointer type, but which can be freed using `free()`. #[derive_ReprC] diff --git a/guide/src/disclaimer.md b/guide/src/disclaimer.md index e1cc48eb43..217931107a 100644 --- a/guide/src/disclaimer.md +++ b/guide/src/disclaimer.md @@ -1,6 +1,6 @@ -Warning: `repr_c` is still in an alpha stage. +Warning: `safer_ffi` is still in an alpha stage. Some features may be missing, while others may be changed when further improving it. diff --git a/guide/src/ffi-export/_.md b/guide/src/ffi-export/_.md index dde0cf6f18..8d7fae8637 100644 --- a/guide/src/ffi-export/_.md +++ b/guide/src/ffi-export/_.md @@ -13,7 +13,7 @@ The only currently supported such "item"s are function definitions: `const` and ```rust,noplaypen -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; #[ffi_export] fn adder (x: i32, y: i32) -> i32 @@ -36,8 +36,9 @@ fn adder (x: i32, y: i32) -> i32 - That is, the following function definition is valid: ```rust,noplaypen - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; + #[ffi_export] fn max<'xs> (xs: c_slice::Ref<'xs, i32>) -> Option<&'xs i32> { @@ -50,7 +51,7 @@ fn adder (x: i32, y: i32) -> i32 - But the following one is **not**: ```rust,noplaypen,compile_fail - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; #[derive_ReprC] #[repr(C)] diff --git a/guide/src/ffi-export/attributes.md b/guide/src/ffi-export/attributes.md index 331a8f89d4..1e1b72a0f8 100644 --- a/guide/src/ffi-export/attributes.md +++ b/guide/src/ffi-export/attributes.md @@ -51,7 +51,7 @@ These are not yet implemented ```rust,noplaypen #[ffi_export] - #[repr_c(unsafe { skip_sanity_checks() })] + #[safer_ffi(unsafe { skip_sanity_checks() })] fn ... ``` @@ -60,7 +60,7 @@ These are not yet implemented ```rust,noplaypen #[ffi_export] fn set_log_level ( - #[repr_c(unsafe { skip_sanity_checks() })] + #[safer_ffi(unsafe { skip_sanity_checks() })] level: LogLevel, ... ) -> ... diff --git a/guide/src/ffi-export/sanity-checks.md b/guide/src/ffi-export/sanity-checks.md index 28646d96b2..b53794bc6c 100644 --- a/guide/src/ffi-export/sanity-checks.md +++ b/guide/src/ffi-export/sanity-checks.md @@ -35,10 +35,10 @@ trigger "instant UB"**, contrary to a hand-crafted definition. instantly trigger Undefined Behavior no matter what the body of `set_log_level` would be. - Instead, when using `repr_c`, the following code: + Instead, when using `safer_ffi`, the following code: ```rust,noplaypen - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; #[derive_ReprC] #[repr(u8)] // Associated CType: a plain `u8` @@ -68,7 +68,7 @@ trigger "instant UB"**, contrary to a hand-crafted definition. #[no_mangle] pub unsafe extern "C" fn set_log_level (level: u8) { - match ::repr_c::layout::from_raw(level) { + match ::safer_ffi::layout::from_raw(level) { | Some(level /* : LogLevel */) => { super::set_log_level(level) }, @@ -96,12 +96,12 @@ the [`ReprC`] type is guaranteed to be aligned). ### Caveats -Such check cannot be exhaustive (in the case of pointers for instance, `repr_c` +Such check cannot be exhaustive (in the case of pointers for instance, `safer_ffi` cannot possibly know if it is valid to dereference a non-null and well-aligned pointer). This means that there are still cases where UB can be triggered nevertheless, hence it being named a _sanity_ check and not a _safety_ check. - - Only in the case of a (field-less) `enum` can `repr_c` ensure lack of + - Only in the case of a (field-less) `enum` can `safer_ffi` ensure lack of UB no matter the (integral) [C type][`CType`] instance given as input. As you may notice by looking at the code, there is a `compile_time_condition()` diff --git a/guide/src/introduction/_.md b/guide/src/introduction/_.md index 1657372a1c..cf6babccbc 100644 --- a/guide/src/introduction/_.md +++ b/guide/src/introduction/_.md @@ -4,7 +4,7 @@ # Introduction -> `repr_c` is a rust framework to generate a foreign function interface (or FFI) easily and safely. +> `safer_ffi` is a rust framework to generate a foreign function interface (or FFI) easily and safely. This framework is primarily used to annotate rust functions and types to generate C headers without polluting nor endangering the rust code with diff --git a/guide/src/introduction/getting_started.md b/guide/src/introduction/getting_started.md index 28b57c9f96..a51e73d3f0 100644 --- a/guide/src/introduction/getting_started.md +++ b/guide/src/introduction/getting_started.md @@ -4,17 +4,17 @@ ### `Cargo.toml` -To start using `::repr_c`, edit your `Cargo.toml` like so: +To start using `::safer_ffi`, edit your `Cargo.toml` like so: ```toml [lib] crate-type = ["staticlib"] # and/or "cdylib" [dependencies] -repr_c = { version = "...", features = ["proc_macros"] } +safer_ffi = { version = "...", features = ["proc_macros"] } [features] -c-headers = ["repr_c/headers"] +c-headers = ["safer_ffi/headers"] ``` - See the [dedicated chapter on `Cargo.toml`][cargo-toml] for more info. @@ -25,7 +25,7 @@ Then, to export a Rust function to FFI, add the [`#[ffi_export]`][ffi_export] attribute like so: ```rust,noplaypen -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; /// Adds two integers together. /// @@ -46,15 +46,15 @@ file at `target/{debug,release}/libcrate_name.ext` #### Generating the C header file -To let `repr_c` generate the corresponding C headers, add also the following to +To let `safer_ffi` generate the corresponding C headers, add also the following to your **`src/lib.rs`**: ```rust,noplaypen -#[::repr_c::cfg_headers] +#[::safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - ::repr_c::headers::builder() + ::safer_ffi::headers::builder() .to_file("filename.h")? .generate() } @@ -70,13 +70,13 @@ cargo test --features c-headers -- generate_headers --nocapture ```C /*! \file */ -/**************************************** - * * - * File auto-generated by `::repr_c`. * - * * - * Do not manually edit this file. * - * * - ****************************************/ +/******************************************* + * * + * File auto-generated by `::safer_ffi`. * + * * + * Do not manually edit this file. * + * * + *******************************************/ #ifndef __RUST_EXAMPLE__ #define __RUST_EXAMPLE__ @@ -150,7 +150,7 @@ Custom types are also supported, as long as they: ### Usage with structs ```rust,noplaypen -#[derive_ReprC] // <- `::repr_c`'s attribute +#[derive_ReprC] // <- `::safer_ffi`'s attribute #[repr(C)] // <- defined C layout is mandatory! struct Point { x: i32, @@ -163,7 +163,7 @@ struct Point { ### Usage with enums ```rust,noplaypen -#[derive_ReprC] // <- `::repr_c`'s attribute +#[derive_ReprC] // <- `::safer_ffi`'s attribute #[repr(u8)] // <- explicit integer `repr` is mandatory! pub enum Direction { Up = 1, diff --git a/guide/src/links.md b/guide/src/links.md index 1fc47d335a..0bd79a8c7e 100644 --- a/guide/src/links.md +++ b/guide/src/links.md @@ -33,7 +33,7 @@ [usage]: /usage/_.md [ffi_export]: /ffi-export/_.md [derive_ReprC]: /derive-reprc/_.md -[how-does-repr_c-work]: /appendix/how-does-repr_c-work.md +[how-does-safer_ffi-work]: /appendix/how-does-safer_ffi-work.md [cargo-toml]: /usage/cargo-toml.md [header-generation]: /usage/lib-rs.md#header-generation [c-compilation]: /appendix/c-compilation.md @@ -42,17 +42,17 @@ [comments]: <> ( RUST DOCUMENTATION ) -[Rust documentation]: /rustdoc/repr_c/ -[`ReprC`]: /rustdoc/repr_c/layout/trait.ReprC.html -[`CType`]: /rustdoc/repr_c/layout/trait.CType.html -[`repr_c::Box`]: /rustdoc/repr_c/boxed/struct.Box.html -[`c_slice::Box`]: /rustdoc/repr_c/slice/struct.slice_boxed.html -[`c_slice::Ref`]: /rustdoc/repr_c/slice/struct.slice_ref.html -[`c_slice::Mut`]: /rustdoc/repr_c/slice/struct.slice_mut.html -[`repr_c::Vec`]: /rustdoc/repr_c/vec/struct.Vec.html -[`repr_c::String`]: /rustdoc/repr_c/string/struct.String.html -[`str::Box`]: /rustdoc/repr_c/string/struct.str_boxed.html -[`str::Ref`]: /rustdoc/repr_c/string/struct.str_ref.html -[`char_p::Box`]: /rustdoc/repr_c/char_p/struct.char_p_boxed.html -[`char_p::Ref`]: /rustdoc/repr_c/char_p/struct.char_p_ref.html -[`repr_c::headers::builder()`]: /rustdoc/repr_c/headers/struct.Builder.html +[Rust documentation]: /rustdoc/safer_ffi/ +[`ReprC`]: /rustdoc/safer_ffi/layout/trait.ReprC.html +[`CType`]: /rustdoc/safer_ffi/layout/trait.CType.html +[`repr_c::Box`]: /rustdoc/safer_ffi/boxed/struct.Box.html +[`c_slice::Box`]: /rustdoc/safer_ffi/slice/struct.slice_boxed.html +[`c_slice::Ref`]: /rustdoc/safer_ffi/slice/struct.slice_ref.html +[`c_slice::Mut`]: /rustdoc/safer_ffi/slice/struct.slice_mut.html +[`repr_c::Vec`]: /rustdoc/safer_ffi/vec/struct.Vec.html +[`repr_c::String`]: /rustdoc/safer_ffi/string/struct.String.html +[`str::Box`]: /rustdoc/safer_ffi/string/struct.str_boxed.html +[`str::Ref`]: /rustdoc/safer_ffi/string/struct.str_ref.html +[`char_p::Box`]: /rustdoc/safer_ffi/char_p/struct.char_p_boxed.html +[`char_p::Ref`]: /rustdoc/safer_ffi/char_p/struct.char_p_ref.html +[`safer_ffi::headers::builder()`]: /rustdoc/safer_ffi/headers/struct.Builder.html diff --git a/guide/src/motivation/_.md b/guide/src/motivation/_.md index 6d0d536bae..7cdd20e005 100644 --- a/guide/src/motivation/_.md +++ b/guide/src/motivation/_.md @@ -1,7 +1,7 @@ {{#include ../links.md}} In this chapter we will see why my company, -[![Ditto][Ditto-logo]][Ditto], and I, chose to develop the `::repr_c` +[![Ditto][Ditto-logo]][Ditto], and I, chose to develop the `::safer_ffi` framework, which should help illustrate why using it can also be a good thing for you. @@ -9,4 +9,4 @@ good thing for you. traditional-ffi.md), 1. We will then discuss about using idiomatic Rust types such as `Vec` and - `[_]` slices in FFI, and how `::repr_c` helps in that regard. + `[_]` slices in FFI, and how `::safer_ffi` helps in that regard. diff --git a/guide/src/motivation/repr-c-forall.md b/guide/src/motivation/repr-c-forall.md index 6744d971a2..2a12169386 100644 --- a/guide/src/motivation/repr-c-forall.md +++ b/guide/src/motivation/repr-c-forall.md @@ -2,7 +2,7 @@ # Idiomatic Rust types in FFI signatures? -That was the main objective when creating and using `::repr_c`: +That was the main objective when creating and using `::safer_ffi`: Why go through **the dangerously `unsafe` hassle** of: @@ -27,9 +27,9 @@ equivalent to `[_]` slices, `Vec`s and `String`s? And _quid_ of closure types? To which the answer is _yes!_ All these types can be FFI-compatible, -**provided they have a defined C layout**. And this is precisely what `repr_c` does: +**provided they have a defined C layout**. And this is precisely what `safer_ffi` does: -> `repr_c` defines a bunch of idiomatic Rust types with a defined `#[repr(C)]` +> `safer_ffi` defines a bunch of idiomatic Rust types with a defined `#[repr(C)]` layout, to get both FFI compatibility and non-`unsafe` ergonomics. That is, for any type `T` that has a defined C layout, _i.e._, that is diff --git a/guide/src/motivation/traditional-ffi.md b/guide/src/motivation/traditional-ffi.md index ee3fd23181..fa72b42fa8 100644 --- a/guide/src/motivation/traditional-ffi.md +++ b/guide/src/motivation/traditional-ffi.md @@ -1,6 +1,6 @@ {{#include ../links.md}} -# Why use `repr_c`? +# Why use `safer_ffi`? Traditionally, to generate FFI from Rust to C developers would use `#[no_mangle]` and [`cbindgen`] like so: @@ -29,7 +29,7 @@ and has run into the limitations outlined below. [Learn more about Ditto's experience with FFI and Rust.](../ditto/_.md) -## `repr_c` features that traditional FFI struggles to support +## `safer_ffi` features that traditional FFI struggles to support - _These have been tested with (`cbindgen v0.14.2`)._ @@ -60,7 +60,7 @@ and invariants][parse-dont-validate], you quickly stumble upon this limitation. This is why, traditional Rust→C FFI code uses "flat" raw pointers. **This results in `unsafe` implementations which are more error-prone**. -`::repr_c` solves this issue by using more evolved types: +`::safer_ffi` solves this issue by using more evolved types: {{#include ../repr_c-types.md}} @@ -100,7 +100,7 @@ fn my_free_supports_null (ptr: Option>) ### Consistent support for macro-generated definitions -Since `repr_c` is integrated within the compiler, it supports macros expanding +Since `safer_ffi` is integrated within the compiler, it supports macros expanding to `#[ffi_export]` function definitions or `#[derive_ReprC]` type definitions.
Example @@ -145,7 +145,7 @@ one only has to: ### Support for shadowed paths -Since `repr_c` is integrated withing the compiler, the types the code refers to +Since `safer_ffi` is integrated withing the compiler, the types the code refers to are unambiguously understood by both `#[derive_ReprC]` and `#[ffi_export]`.
Example diff --git a/guide/src/repr_c-types.md b/guide/src/repr_c-types.md index 4ac1b4b195..c018504a66 100644 --- a/guide/src/repr_c-types.md +++ b/guide/src/repr_c-types.md @@ -1,4 +1,4 @@ -| | Traditional FFI | `repr_c` | +| | Traditional FFI | `safer_ffi` | |-------------------------|-----------------|--------------------------| | Mutable pointer or NULL | `*mut T` | `Option<&mut T>` | | Mutable pointer | `*mut T` | `&mut T` | diff --git a/guide/src/simple-examples/max.md b/guide/src/simple-examples/max.md index e8ac8aeb10..e3fc36038c 100644 --- a/guide/src/simple-examples/max.md +++ b/guide/src/simple-examples/max.md @@ -3,7 +3,7 @@ ```rust,noplaypen #![deny(unsafe_code)] /* No `unsafe` needed! */ -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; /// Returns a pointer to the maximum element of the slice /// when it is not empty, and `NULL` otherwise. diff --git a/guide/src/simple-examples/string_concat.md b/guide/src/simple-examples/string_concat.md index 46b12ec57e..8598856a78 100644 --- a/guide/src/simple-examples/string_concat.md +++ b/guide/src/simple-examples/string_concat.md @@ -3,7 +3,7 @@ ```rust,noplaypen #![deny(unsafe_code)] /* No `unsafe` needed! */ -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; /// Concatenate two input UTF-8 (_e.g._, ASCII) strings. /// diff --git a/guide/src/usage/_.md b/guide/src/usage/_.md index d32d250270..864680d2cf 100644 --- a/guide/src/usage/_.md +++ b/guide/src/usage/_.md @@ -2,7 +2,7 @@ # Usage -Using `repr_c` is pretty simple, provided one knows [how C compilation +Using `safer_ffi` is pretty simple, provided one knows [how C compilation works][c-compilation]. ## TL,DR @@ -14,16 +14,16 @@ works][c-compilation]. crate-type = ["staticlib"] [dependencies] -repr_c = { version = "...", features = ["proc_macros"] } +safer_ffi = { version = "...", features = ["proc_macros"] } [features] -c-headers = ["repr_c/headers"] +c-headers = ["safer_ffi/headers"] ``` ### `src/lib.rs` ```rust,noplaypen -use ::repr_c::prelude::*; +use ::safer_ffi::prelude::*; #[ffi_export] fn add (x: i32, y: i32) -> i32 @@ -31,11 +31,11 @@ fn add (x: i32, y: i32) -> i32 x.wrapping_add(y) } -#[::repr_c::cfg_headers] +#[::safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - ::repr_c::headers::builder() + ::safer_ffi::headers::builder() .to_file("filename.h")? .generate() } diff --git a/guide/src/usage/cargo-toml.md b/guide/src/usage/cargo-toml.md index d717447901..e7e2e3393e 100644 --- a/guide/src/usage/cargo-toml.md +++ b/guide/src/usage/cargo-toml.md @@ -22,22 +22,22 @@ crate-type = [ ] ``` -### `[dependencies.repr_c]` +### `[dependencies.safer_ffi]` -To get access to `repr_c` and its ergonomic attribute macros we add `repr_c` as +To get access to `safer_ffi` and its ergonomic attribute macros we add `safer_ffi` as a dependency, and enable the `proc-macros` feature: ```toml [dependencies] -repr_c = { version = "...", features = ["proc-macros"] } +safer_ffi = { version = "...", features = ["proc-macros"] } ``` - - or instead simply run `cargo add repr_c --features proc-macros` if you + - or instead simply run `cargo add safer_ffi --features proc-macros` if you have [`cargo edit`][cargo-edit].
About the proc-macros feature -although still a WIP, the author of `repr_c` is making an important effort +although still a WIP, the author of `safer_ffi` is making an important effort to make the usage of procedural macros be as optional as possible, so as to allow downstream users to avoid pulling the very heavyweight `syn + quote` dependencies, by offering alternative basic macros @@ -49,10 +49,10 @@ alternatives (such as `ReprC!` instead of `#[derive_ReprC]`) `std` feature by adding `default-features = false`. - if, however, you still have access to an allocator, you can enable the - `alloc` feature, to get the defintions of `repr_c::{Box, String, Vec}` + `alloc` feature, to get the defintions of `safer_ffi::{Box, String, Vec}` _etc._ - - You may also enable the `log` feature so that `repr_c` may log `error!`s + - You may also enable the `log` feature so that `safer_ffi` may log `error!`s when the semi-checked casts from raw C types into their Rust counterparts fail (_e.g._, when receiving a `bool` that is nether `0` nor `1`). @@ -61,15 +61,15 @@ alternatives (such as `ReprC!` instead of `#[derive_ReprC]`) Finally, in order to alleviate the compile-time when not generating the headers (it is customary to bundle pre-generated headers when distributing an FFI-compatible Rust crate), the runtime C reflection and header generation -machinery (the most heavyweight part of `repr_c`) is feature-gated away by -default (behind the `repr_c/headers` feature). +machinery (the most heavyweight part of `safer_ffi`) is feature-gated away by +default (behind the `safer_ffi/headers` feature). However, when [generating the headers][header-generation], such machinery is needed. Thus the simplest solution is for the FFI crate to have a Cargo feature -(flag) that transitively enables the `repr_c/headers` feature. You can name such +(flag) that transitively enables the `safer_ffi/headers` feature. You can name such feature however you want. In this guide, it is named `c-headers`. ```toml [features] -c-headers = ["repr_c/headers"] +c-headers = ["safer_ffi/headers"] ``` diff --git a/guide/src/usage/lib-rs.md b/guide/src/usage/lib-rs.md index f13ce0f43e..7a24bc8ea4 100644 --- a/guide/src/usage/lib-rs.md +++ b/guide/src/usage/lib-rs.md @@ -14,31 +14,31 @@ The only currently supported such "item"s are function definitions: `const` and -If using non-primitive non-`repr_c`-provided types, then those must be +If using non-primitive non-`safer_ffi`-provided types, then those must be [`#[derive_ReprC]` annotated][derive_ReprC]. At which point the only thing remaining is to generate the header file. ### Header generation -Given how `repr_c` implements the C reflection logic as methods within +Given how `safer_ffi` implements the C reflection logic as methods within [a trait][`CType`] related to [`ReprC`], the only way to generate the headers is to be a "Rust downstream" user of the library, within the same compilation unit / crate (a limitation that comes from the way the machinery currently operates). That is, a **unit test**. So you need to define a `cfg`-gated unit test that calls into the -[`repr_c::headers::builder()`] to `.generate()` the headers into the given +[`safer_ffi::headers::builder()`] to `.generate()` the headers into the given file(name), or into the given `Write`-able / "write sink": - Basic example: ```rust,noplaypen - #[::repr_c::cfg_headers] + #[::safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - ::repr_c::headers::builder() + ::safer_ffi::headers::builder() .to_file("filename.h")? .generate() } @@ -56,11 +56,11 @@ file(name), or into the given `Write`-able / "write sink": More advanced example (runtime-dependent header output) ```rust,noplaypen -#[::repr_c::cfg_headers] +#[::safer_ffi::cfg_headers] #[test] fn generate_headers () -> ::std::io::Result<()> { - let builder = ::repr_c::headers::builder(); + let builder = ::safer_ffi::headers::builder(); if let Ok(filename) = ::std::env::var("HEADERS_FILE") { builder .to_file(&filename)? diff --git a/guide/scripts/build_docs.sh b/scripts/build_docs.sh similarity index 100% rename from guide/scripts/build_docs.sh rename to scripts/build_docs.sh diff --git a/src/_lib.rs b/src/_lib.rs index c85e5caf93..6b1cd96fd2 100644 --- a/src/_lib.rs +++ b/src/_lib.rs @@ -17,6 +17,8 @@ unused_must_use, )] +//! See the [user guide](https://getditto.github.io/safer_ffi). + #[cfg(feature = "proc_macros")] #[macro_use] extern crate require_unsafe_in_body; @@ -76,18 +78,19 @@ cfg_alloc! { } cfg_alloc! { - #[doc(inline)] - pub use boxed::{Box, slice_boxed, str_boxed}; - pub mod boxed; + pub + mod boxed; } use self::c_char_module::c_char; #[path = "c_char.rs"] mod c_char_module; -pub mod char_p; +pub +mod char_p; -pub mod closure; +pub +mod closure; const _: () = { #[path = "ffi_export.rs"] @@ -97,19 +100,16 @@ const _: () = { pub mod ptr; -#[doc(inline)] -pub use slice::{slice_ref, slice_mut}; -pub mod slice; - - -#[doc(inline)] -pub use string::str_ref; +pub +mod slice; #[path = "string/_mod.rs"] -pub mod string; +pub +mod string; #[doc(no_inline)] -pub use tuple::*; +pub +use tuple::*; pub mod tuple; @@ -183,6 +183,17 @@ mod prelude { } } pub + mod repr_c { + cfg_alloc! { + #[doc(no_inline)] + pub use crate::{ + boxed::Box, + string::String, + vec::Vec, + }; + } + } + pub mod str { #[doc(no_inline)] pub use crate::string::{ @@ -217,9 +228,9 @@ mod prelude { // Out reference itself Out, // Helper trait to go from `&mut T` and `&mut MaybeUninit` to `Out` - AsOut as _, + AsOut, // Helper trait to have `AsOut` when `T : !Copy` - ManuallyDropMut as _, + ManuallyDropMut, }; } diff --git a/src/headers/_mod.rs b/src/headers/_mod.rs index daba533934..b7df524eb3 100644 --- a/src/headers/_mod.rs +++ b/src/headers/_mod.rs @@ -1,25 +1,25 @@ //! C headers generation. //! -//! This module is only enabled when the `"headers"` feature of `::repr_c` is +//! This module is only enabled when the `"headers"` feature of `::safer_ffi` is //! enabled, which is expected to be done through a cargo feature within the //! (downstream) crate defining the `#[ffi_export]`ed //! functions. //! //! ```toml //! [dependencies] -//! repr_c = { version = "...", features = ["proc_macros"] } +//! safer_ffi = { version = "...", features = ["proc_macros"] } //! //! [features] -//! generate-headers = ["repr_c/headers"] +//! generate-headers = ["safer_ffi/headers"] //! ``` //! //! Then, to generate the bindings, just define a -//! `#[repr_c::cfg_headers]`-gated `#[test]` function, +//! `#[safer_ffi::cfg_headers]`-gated `#[test]` function, //! which can then call the [`builder`] to do the work: //! //! ```rust //! use ::std::{io, fs}; -//! use ::repr_c::prelude::*; +//! use ::safer_ffi::prelude::*; //! //! /// Concatenate two strings. //! /// @@ -41,13 +41,13 @@ //! } //! //! # macro_rules! ignore { ($($t:tt)*) => () } ignore! { -//! #[::repr_c::cfg_headers] +//! #[::safer_ffi::cfg_headers] //! #[test] //! # } //! fn generate_c_header () //! -> io::Result<()> //! { -//! ::repr_c::headers::builder() +//! ::safer_ffi::headers::builder() //! .with_guard("__ASGARD__") //! .to_file("filename.h")? //! .generate() @@ -67,13 +67,13 @@ //! the following contents: //! //!
/*! \file */
-//! /****************************************
-//!  *                                      *
-//!  *  File auto-generated by `::repr_c`.  *
-//!  *                                      *
-//!  *  Do not manually edit this file.     *
-//!  *                                      *
-//!  ****************************************/
+//! /*******************************************
+//!  *                                         *
+//!  *  File auto-generated by `::safer_ffi`.  *
+//!  *                                         *
+//!  *  Do not manually edit this file.        *
+//!  *                                         *
+//!  *******************************************/
 //!
 //! #ifndef __ASGARD__
 //! #define __ASGARD__
@@ -176,7 +176,7 @@ macro_rules! with_optional_fields {(
         ///
         /// ```rust,no_run
         /// # fn main () -> ::std::io::Result<()> { Ok({
-        /// ::repr_c::headers::builder()
+        /// ::safer_ffi::headers::builder()
         ///     .to_file("my_header.h")?
         ///     .generate()?
         /// # })}
@@ -207,7 +207,7 @@ macro_rules! with_optional_fields {(
         /// // Display the headers to the standard output
         /// // (may need the `--nocapture` flag when running the tests)
         /// # fn main () -> ::std::io::Result<()> { Ok({
-        /// ::repr_c::headers::builder()
+        /// ::safer_ffi::headers::builder()
         ///     .to_writer(::std::io::stdout())
         ///     .generate()?
         /// # })}
@@ -280,24 +280,24 @@ with_optional_fields! {
     /// ```rust,ignore
     /// concat!(
     ///     "/*! \\file */\n",
-    ///     "/****************************************\n",
-    ///     " *                                      *\n",
-    ///     " *  File auto-generated by `::repr_c`.  *\n",
-    ///     " *                                      *\n",
-    ///     " *  Do not manually edit this file.     *\n",
-    ///     " *                                      *\n",
-    ///     " ****************************************/\n",
+    ///     "/*******************************************\n",
+    ///     " *                                         *\n",
+    ///     " *  File auto-generated by `::safer_ffi`.  *\n",
+    ///     " *                                         *\n",
+    ///     " *  Do not manually edit this file.        *\n",
+    ///     " *                                         *\n",
+    ///     " *******************************************/\n",
     /// )
     /// ```
     ///
     /// 
/*! \file */
-    /// /****************************************
-    ///  *                                      *
-    ///  *  File auto-generated by `::repr_c`.  *
-    ///  *                                      *
-    ///  *  Do not manually edit this file.     *
-    ///  *                                      *
-    ///  ****************************************/
+    /// /*******************************************
+    ///  *                                         *
+    ///  *  File auto-generated by `::safer_ffi`.  *
+    ///  *                                         *
+    ///  *  Do not manually edit this file.        *
+    ///  *                                         *
+    ///  *******************************************/
     /// 
banner: &'__ str, } @@ -325,13 +325,13 @@ impl Builder<'_, WhereTo> { ; let banner: &'_ str = config.banner.unwrap_or(concat!( "/*! \\file */\n", - "/****************************************\n", - " * *\n", - " * File auto-generated by `::repr_c`. *\n", - " * *\n", - " * Do not manually edit this file. *\n", - " * *\n", - " ****************************************/", + "/*******************************************\n", + " * *\n", + " * File auto-generated by `::safer_ffi`. *\n", + " * *\n", + " * Do not manually edit this file. *\n", + " * *\n", + " *******************************************/", )); write!(definer.out(), diff --git a/src/layout/_mod.rs b/src/layout/_mod.rs index d769965b4b..32f9239be2 100644 --- a/src/layout/_mod.rs +++ b/src/layout/_mod.rs @@ -46,8 +46,8 @@ fn __assert_concrete__() where /// - If you trully want a manual implementation of `CType` (_e.g._, for an /// "opaque type" pattern, _i.e._, a forward declaration), then, to /// implement the trait so that it works no matter the status of -/// the `repr_c/headers` feature, one must define the methods as if -/// feature was present, but with a `#[::repr_c::cfg_headers]` gate slapped +/// the `safer_ffi/headers` feature, one must define the methods as if +/// feature was present, but with a `#[::safer_ffi::cfg_headers]` gate slapped /// on _each_ method. /// /// # Safety @@ -199,7 +199,7 @@ unsafe trait CType /// /// ```rust,ignore /// unsafe impl CType for i32 { - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_define_self (definer: &'_ mut dyn Definer) /// -> io::Result<()> /// { @@ -221,7 +221,7 @@ unsafe trait CType /// } /// /// unsafe impl CType for i32 { - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_define_self (definer: &'_ mut dyn Definer) /// -> io::Result<()> /// { @@ -274,7 +274,7 @@ unsafe trait CType /// /// ```rust,ignore /// unsafe impl CType for i32 { - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_var_fmt ( /// fmt: &'_ mut fmt::Formatter<'_>, /// var_name: &'_ str, @@ -291,7 +291,7 @@ unsafe trait CType /// /// ```rust,ignore /// unsafe impl CType for Option u32> { - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_var_fmt ( /// fmt: &'_ mut fmt::Formatter<'_>, /// var_name: &'_ str, @@ -308,7 +308,7 @@ unsafe trait CType /// /// ```rust,ignore /// unsafe impl CType for [i32; 42] { - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_var_fmt ( /// fmt: &'_ mut fmt::Formatter<'_>, /// var_name: &'_ str, @@ -320,7 +320,7 @@ unsafe trait CType /// /// // Since `c_var_fmt()` requires a one-time typedef, overriding /// // `c_define_self()` is necessary: - /// #[::repr_c::cfg_headers] + /// #[::safer_ffi::cfg_headers] /// fn c_define_self (definer: &'_ mut dyn Definer) /// -> fmt::Result /// { @@ -448,7 +448,7 @@ __cfg_headers__! { /// /// It is generally recommended to avoid manually (and `unsafe`-ly) /// implementing the [`ReprC`] trait. Instead, the recommended and blessed way -/// is to use the [`#[derive_ReprC]`](/repr_c/layout/attr.derive_ReprC.html) +/// is to use the [`#[derive_ReprC]`](/safer_ffi/layout/attr.derive_ReprC.html) /// attribute (when the `proc_macros` feature is enabled, or the [`ReprC!`] /// macro when it is not) on your `#[repr(C)] struct` (or your field-less /// `#[repr()] enum`). @@ -461,7 +461,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(C)] @@ -485,7 +485,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// ReprC! { /// #[repr(C)] @@ -500,7 +500,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(u8)] @@ -530,7 +530,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// ReprC! { /// #[repr(u8)] @@ -548,7 +548,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(C)] @@ -586,7 +586,7 @@ __cfg_headers__! { /// /// ```rust,no_run /// # fn main () {} -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// ReprC! { /// #[repr(C)] diff --git a/src/layout/macros.rs b/src/layout/macros.rs index e054a7163c..f215c30c24 100644 --- a/src/layout/macros.rs +++ b/src/layout/macros.rs @@ -208,7 +208,7 @@ macro_rules! CType {( /// - Instead of: /// /// ```rust,compile_fail -/// use ::repr_c::layout::ReprC; +/// use ::safer_ffi::layout::ReprC; /// /// ReprC! { /// #[repr(C)] @@ -224,7 +224,7 @@ macro_rules! CType {( /// - You need to write: /// /// ```rust -/// use ::repr_c::layout::ReprC; +/// use ::safer_ffi::layout::ReprC; /// /// ReprC! { /// #[repr(C)] @@ -247,11 +247,11 @@ macro_rules! CType {( /// /// ```toml /// [dependencies] -/// repr_c = { version = "...", features = ["proc_macros"] } +/// safer_ffi = { version = "...", features = ["proc_macros"] } /// ``` /// /// and use the [`#[derive_ReprC]`]( -/// /repr_c/layout/attr.derive_ReprC.html) attribute macro instead, +/// /safer_ffi/layout/attr.derive_ReprC.html) attribute macro instead, /// which will do the rewriting for you. #[macro_export] macro_rules! ReprC { @@ -307,7 +307,7 @@ macro_rules! ReprC { $crate::paste::item! { #[allow(nonstandard_style)] $pub use - [< __ $StructName _repr_c_mod >]::$StructName + [< __ $StructName _safer_ffi_mod >]::$StructName as [< $StructName _Layout >] ; @@ -358,7 +358,7 @@ macro_rules! ReprC { } $crate::paste::item! { #[allow(nonstandard_style, trivial_bounds)] - mod [< __ $StructName _repr_c_mod >] { + mod [< __ $StructName _safer_ffi_mod >] { #[allow(unused_imports)] use super::*; @@ -397,7 +397,7 @@ macro_rules! ReprC { } const _: () = { $crate::paste::item! { - use [< __ $StructName _repr_c_mod >]::*; + use [< __ $StructName _safer_ffi_mod >]::*; } impl $(<$($lt ,)* $($($generics),+)?>)? $crate::core::marker::Copy @@ -730,8 +730,7 @@ macro_rules! ReprC { ( $(#[doc = $prev_doc:tt])* #[ - $(::)? - repr_c + ReprC :: opaque $( @@ -762,7 +761,7 @@ macro_rules! ReprC { const _: () = { pub - struct __repr_c_Opaque__ $( + struct __safer_ffi_Opaque__ $( <$($lt ,)* $($($generics),+)?> $( where $($bounds)* @@ -785,7 +784,7 @@ macro_rules! ReprC { impl $(<$($lt ,)* $($($generics),+)?>)? $crate::core::marker::Copy for - __repr_c_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? + __safer_ffi_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? $( where $($($bounds)*)? @@ -795,7 +794,7 @@ macro_rules! ReprC { impl $(<$($lt ,)* $($($generics),+)?>)? $crate::core::clone::Clone for - __repr_c_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? + __safer_ffi_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? $( where $($($bounds)*)? @@ -811,7 +810,7 @@ macro_rules! ReprC { impl $(<$($lt ,)* $($($generics),+)?>)? $crate::layout::CType for - __repr_c_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? + __safer_ffi_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? $( where $($($bounds)*)? @@ -871,7 +870,7 @@ macro_rules! ReprC { } $crate::layout::from_CType_impl_ReprC! { $(@for[$($lt ,)* $($($generics),+)?])? - __repr_c_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? + __safer_ffi_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? $( where $($($bounds)*)? @@ -889,7 +888,7 @@ macro_rules! ReprC { )? { type CLayout = - __repr_c_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? + __safer_ffi_Opaque__ $(<$($lt ,)* $($($generics),+)?>)? ; fn is_valid (it: &'_ Self::CLayout) @@ -1044,7 +1043,7 @@ mod test { } ReprC! { - #[repr_c::opaque("Opaque")] + #[ReprC::opaque("Opaque")] struct Opaque {} } @@ -1063,7 +1062,7 @@ mod test { cfg_proc_macros! { doc_test! { derive_ReprC_supports_generics: fn main () {} - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; /// Some docstring before #[derive_ReprC] @@ -1090,10 +1089,10 @@ mod test { doc_test! { unused: fn main () {} - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; ReprC! { - #[::repr_c::opaque("Foo")] + #[ReprC::opaque("Foo")] struct Foo {} } } @@ -1101,10 +1100,10 @@ mod test { doc_test! { with_indirection: fn main () {} - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; ReprC! { - #[::repr_c::opaque("Foo")] + #[ReprC::opaque("Foo")] pub struct Foo {} } @@ -1118,10 +1117,10 @@ mod test { #![compile_fail] fn main () {} - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; ReprC! { - #[::repr_c::opaque] + #[ReprC::opaque] pub struct Foo {} } diff --git a/src/proc_macro/Cargo.toml b/src/proc_macro/Cargo.toml index 7981303c53..993eeba2d4 100644 --- a/src/proc_macro/Cargo.toml +++ b/src/proc_macro/Cargo.toml @@ -3,7 +3,7 @@ path = "mod.rs" proc-macro = true [package] -name = "repr_c-proc_macro" +name = "safer_ffi-proc_macro" version = "0.1.0" authors = ["Daniel Henry-Mantilla "] edition = "2018" diff --git a/src/proc_macro/c_str.rs b/src/proc_macro/c_str.rs index 42a9c53cde..d5a5acc319 100644 --- a/src/proc_macro/c_str.rs +++ b/src/proc_macro/c_str.rs @@ -4,7 +4,7 @@ fn c_str (input: TokenStream) { let input: LitStr = if let Some(it) = parse_macro_input!(input) { it } else { return ::quote::quote!( - ::repr_c::char_p::char_p_ref::EMPTY + ::safer_ffi::char_p::char_p_ref::EMPTY ).into(); }; let bytes = input.value(); @@ -28,8 +28,8 @@ fn c_str (input: TokenStream) ::quote::quote!( unsafe { const STATIC_BYTES: &'static [u8] = #byte_str; - ::repr_c::char_p::char_p_ref::from_ptr_unchecked( - ::repr_c::ptr::NonNull::new_unchecked(STATIC_BYTES.as_ptr() as _) + ::safer_ffi::char_p::char_p_ref::from_ptr_unchecked( + ::safer_ffi::ptr::NonNull::new_unchecked(STATIC_BYTES.as_ptr() as _) ) } ).into() diff --git a/src/proc_macro/derives.rs b/src/proc_macro/derives.rs index a18d40615c..14769638dd 100644 --- a/src/proc_macro/derives.rs +++ b/src/proc_macro/derives.rs @@ -14,7 +14,7 @@ fn feed_to_macro_rules (input: TokenStream, name: Ident) ref variants, .. }) => quote! { - ::repr_c::layout::ReprC! { + ::safer_ffi::layout::ReprC! { #(#attrs)* #vis #enum_ #ident { @@ -29,7 +29,7 @@ fn feed_to_macro_rules (input: TokenStream, name: Ident) }) => { let (params, bounds) = generics.my_split(); quote! { - ::repr_c::layout::#name! { + ::safer_ffi::layout::#name! { #(#attrs)* #vis #struct_ #ident @@ -57,14 +57,14 @@ fn feed_to_macro_rules (input: TokenStream, name: Ident) /// Safely implement [`ReprC`] /// for a `#[repr(C)]` struct **when all its fields are [`ReprC`]**. /// -/// [`ReprC`]: /repr_c/layout/trait.ReprC.html +/// [`ReprC`]: /safer_ffi/layout/trait.ReprC.html /// /// # Examples /// /// ### Simple `struct` /// /// ```rust -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(C)] @@ -86,7 +86,7 @@ fn feed_to_macro_rules (input: TokenStream, name: Ident) /// ### Field-less `enum` /// /// ```rust -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(u8)] @@ -117,7 +117,7 @@ fn feed_to_macro_rules (input: TokenStream, name: Ident) /// `: ReprC` bound each: /// /// ```rust -/// use ::repr_c::prelude::*; +/// use ::safer_ffi::prelude::*; /// /// #[derive_ReprC] /// #[repr(C)] diff --git a/src/proc_macro/ffi_export.rs b/src/proc_macro/ffi_export.rs index ad5007c51d..bfff6e95a3 100644 --- a/src/proc_macro/ffi_export.rs +++ b/src/proc_macro/ffi_export.rs @@ -3,7 +3,7 @@ /// # Example /// /// ```rust -/// use ::repr_c::prelude::ffi_export; +/// use ::safer_ffi::prelude::ffi_export; /// /// #[ffi_export] /// /// Add two integers together. @@ -13,7 +13,7 @@ /// } /// ``` /// -/// - ensures that [the generated headers](/repr_c/headers/) will include the +/// - ensures that [the generated headers](/safer_ffi/headers/) will include the /// following definition: /// /// ```C @@ -36,7 +36,7 @@ /// /// # `ReprC` /// -/// [`ReprC`]: /repr_c/layout/trait.ReprC.html +/// [`ReprC`]: /safer_ffi/layout/trait.ReprC.html /// /// You can use any Rust types in the singature of an `#[ffi_export]`- /// function, provided each of the types involved in the signature is [`ReprC`]. @@ -46,7 +46,7 @@ /// /// To have custom structs implement [`ReprC`], it suffices to annotate the /// `struct` definitions with the [`#[derive_ReprC]`]( -/// /repr_c/layout/attr.derive_ReprC.html) +/// /safer_ffi/layout/attr.derive_ReprC.html) /// (on top of the obviously required `#[repr(C)]`). #[proc_macro_attribute] pub fn ffi_export (attrs: TokenStream, input: TokenStream) @@ -65,7 +65,7 @@ fn ffi_export (attrs: TokenStream, input: TokenStream) TT::Punct(Punct::new(':', Spacing::Joint)), TT::Punct(Punct::new(':', Spacing::Alone)), - TT::Ident(Ident::new("repr_c", span)), + TT::Ident(Ident::new("safer_ffi", span)), TT::Punct(Punct::new(':', Spacing::Joint)), TT::Punct(Punct::new(':', Spacing::Alone)), diff --git a/src/utils/macros.rs b/src/utils/macros.rs index bc1409a2c3..3d2da26104 100644 --- a/src/utils/macros.rs +++ b/src/utils/macros.rs @@ -175,13 +175,13 @@ macro_rules! doc_test { cfg_proc_macros! { doc_test! { c_str: - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; let _ = c!("Hello, World!"); } doc_test! { c_str_inner_nul_byte: #![compile_fail] - use ::repr_c::prelude::*; + use ::safer_ffi::prelude::*; let _ = c!("Hell\0, World!"); } diff --git a/src/utils/prelude.rs b/src/utils/prelude.rs index abc685b23c..ad19452136 100644 --- a/src/utils/prelude.rs +++ b/src/utils/prelude.rs @@ -8,7 +8,7 @@ pub(in crate) use crate::{ utils::markers::*, }; cfg_alloc! { - pub(in crate) use crate::{ + pub(in crate) use crate::prelude::repr_c::{ Box, String, Vec, diff --git a/tests/layout_macros.rs b/tests/layout_macros.rs index 241d324989..abe70b130a 100644 --- a/tests/layout_macros.rs +++ b/tests/layout_macros.rs @@ -10,7 +10,7 @@ use ::std::{ ptr, ops::Not as _, }; -use ::repr_c::{ +use ::safer_ffi::{ prelude::*, layout::{ CType, @@ -329,12 +329,12 @@ fn generate_headers () out: &mut ::std::io::stderr(), defines: Default::default(), }; - ::repr_c::inventory::iter + ::safer_ffi::inventory::iter .into_iter() .collect::>() .into_iter() .rev() - .try_for_each(|::repr_c::FfiExport(define)| define(definer)) + .try_for_each(|::safer_ffi::FfiExport(define)| define(definer)) ? ; @@ -343,7 +343,7 @@ fn generate_headers () out: &'out mut dyn io::Write, defines: Set, } - impl ::repr_c::headers::Definer + impl ::safer_ffi::headers::Definer for MyDefiner<'_> { fn insert (self: &'_ mut Self, name: &'_ str)