Skip to content

Commit

Permalink
Merge pull request #170 from getditto/libc-improvements
Browse files Browse the repository at this point in the history
`libc` improvements
  • Loading branch information
danielhenrymantilla authored Jul 7, 2023
2 parents 140dd8f + 42522f3 commit b4c13c7
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ path = "src/_lib.rs"

[package]
name = "safer-ffi"
version = "0.1.1" # Keep in sync
version = "0.1.2" # Keep in sync
authors = [
"Daniel Henry-Mantilla <[email protected]>",
]
Expand Down Expand Up @@ -126,6 +126,9 @@ inventory-0-3-1.optional = true
inventory-0-3-1.package = "inventory"
inventory-0-3-1.version = "0.3.1"

libc.version = "0.2.66"
libc.default-features = false

log.optional = true
log.version = "0.4.8"

Expand Down Expand Up @@ -161,11 +164,7 @@ version = "0.0.3"

[dependencies.safer_ffi-proc_macros]
path = "src/proc_macro"
version = "=0.1.1" # Keep in sync

[target.'cfg(not(target = "wasm32-unknown-unknown"))'.dependencies]
libc.version = "0.2.66"
libc.default-features = false
version = "=0.1.2" # Keep in sync

[workspace]
members = [
Expand Down
4 changes: 2 additions & 2 deletions ffi_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 6 additions & 12 deletions src/_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ cfg_alloc! {
mod boxed;
}

#[doc(inline)]
pub use self::c_char_module::c_char;
#[path = "c_char.rs"]
mod c_char_module;
Expand All @@ -294,6 +295,9 @@ mod dyn_traits;
#[doc(no_inline)]
pub use dyn_traits::futures;

pub
mod libc;

pub
mod ptr;

Expand All @@ -320,8 +324,8 @@ cfg_alloc! {
pub mod vec;
}

#[apply(hidden_export)]
use layout::impls::c_int;
#[doc(inline)]
pub use layout::impls::c_int;

pub
mod prelude {
Expand Down Expand Up @@ -446,16 +450,6 @@ macro_rules! __abort_with_msg__ { ($($tt:tt)*) => (
}}
)}

#[cfg(target_arch = "wasm32")]
#[allow(dead_code)]
mod libc {
pub type c_int = i32;
pub type size_t = u32;
pub type uintptr_t = u32;
}
#[cfg(not(target_arch = "wasm32"))]
use ::libc;

extern crate self as safer_ffi;

#[apply(hidden_export)]
Expand Down
13 changes: 10 additions & 3 deletions src/c_char.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
use_prelude!();

/// A `ReprC` _standalone_ type with the same layout and ABI as
/// [`::libc::c_char`][crate::libc::c_char].
///
/// By _standalone_, the idea is that this is defined as a (`transparent`) _newtype_ `struct`,
/// rather than as a _`type` alias_, which is error-prone and yields less-portable headers (since
/// the header generation will resolve the type alias and emit, for instance, `int8_t`, ⚠️).
///
/// By using this type, you guarantee that the C `char` type be used in the headers.
#[repr(transparent)]
#[derive(
Debug,
Expand All @@ -16,8 +24,7 @@ struct c_char /* = */ (
u8,
);

/// Assert that `::libc::c_char` is either `uint8_t` or `int8_t`.
#[cfg(not(any(target_arch = "wasm32", not(feature = "std"))))] // no libc on WASM nor no_std
/// Assert that `crate::libc::c_char` is either `uint8_t` or `int8_t`.
const _: () = {
trait IsU8OrI8
{}
Expand All @@ -33,7 +40,7 @@ const _: () = {
fn is_u8_or_i8<T>() where
T : IsU8OrI8,
{}
let _ = is_u8_or_i8::<::libc::c_char>;
let _ = is_u8_or_i8::<crate::libc::c_char>;
};
};

Expand Down
8 changes: 8 additions & 0 deletions src/layout/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,14 @@ unsafe
}
from_CType_impl_ReprC! { Bool }

/// A `ReprC` _standalone_ type with the same layout and ABI as
/// [`::libc::c_int`][crate::libc::c_int].
///
/// By _standalone_, the idea is that this is defined as a (`transparent`) _newtype_ `struct`,
/// rather than as a _`type` alias_, which is error-prone and yields less-portable headers (since
/// the header generation will resolve the type alias and emit, for instance, `int32_t`, ⚠️).
///
/// By using this type, you guarantee that the C `int` type be used in the headers.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub
Expand Down
73 changes: 73 additions & 0 deletions src/libc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
//! On certain platforms, `::libc` has no definitions for pervasive types such as `size_t`.
//!
//! We polyfill them here, and reëxport them for downstream users to use at leisure
//! (_e.g._, so that they don't have to do that themselves too!).
//!
//! ```rust
//! # #[cfg(any())] macro_rules! ignore {
#![doc = stringified_module_code!()]
//! # }
#![allow(warnings, clippy::all)]

use_libc_or_else! {
pub use ::libc::{
/// Note: your should probably be using [`crate::c_char`] instead.
c_char else u8,
/// Note: your should probably be using [`crate::c_int`] instead.
c_int else ::core::ffi::c_int,
///
size_t else usize,
///
uintptr_t else usize,
};
}

macro_rules! use_libc_or_else_ {(
pub use ::libc::{
$(
$(#$doc:tt)*
$c_type:ident else $FallbackTy:ty
),* $(,)?
};
) => (

$(
#[doc = concat!("A _`type` alias_ to [`::libc::", stringify!($c_type), "`].")]
///
$(#$doc)*
pub type $c_type = helper::$c_type;
)*

mod helper {
mod real_libc {
pub use ::libc::*;
$(
pub const $c_type: () = ();
)*
}

pub use real_libc::{
$(
$c_type,
)*
};

pub use fallback::*;
mod fallback {
$(
pub type $c_type = $FallbackTy;
)*
}
}
)} use use_libc_or_else_;

macro_rules! use_libc_or_else {(
$($input:tt)*
) => (
macro_rules! stringified_module_code {() => (
stringify!($($input)*)
)}

use_libc_or_else_!($($input)*);
)} use use_libc_or_else;
2 changes: 1 addition & 1 deletion src/proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ proc-macro = true

[package]
name = "safer_ffi-proc_macros"
version = "0.1.1" # Keep in sync
version = "0.1.2" # Keep in sync
authors = ["Daniel Henry-Mantilla <[email protected]>"]
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion src/utils/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cfg_match! {
pub(in crate) type size_t = u32;
},
_ => {
pub(in crate) use ::libc::size_t;
pub(in crate) use crate::libc::size_t;
},
}

Expand Down

0 comments on commit b4c13c7

Please sign in to comment.