Skip to content

Commit c48374a

Browse files
committed
Make Option<{Owned,Borrowed}{Fd,Socket}> FFI-safe.
Mark `{Owned,Borrowed}{Fd,Socket}` as `rustc_nonnull_optimization_guaranteed` so that they can be used in `Option` in FFI. This allows the removal of the `allow(improper_ctypes)`.
1 parent 0e61b03 commit c48374a

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/example_ffi.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//! This is just a sample of what FFI using this crate can look like.
22
3-
// Disable `improper_ctypes` warnings so that we don't lint about
4-
// `Option<OwnedFd>` appearing in an FFI signature. In the future rustc
5-
// would be modified to recognize this case.
6-
#![cfg_attr(rustc_attrs, allow(improper_ctypes))]
73
#![cfg_attr(not(rustc_attrs), allow(unused_imports))]
84
#![allow(missing_docs)]
95

src/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const INVALID_SOCKET: usize = !0 as _;
5757
#[cfg(any(unix, target_os = "wasi"))]
5858
#[derive(Copy, Clone)]
5959
#[repr(transparent)]
60+
#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)]
6061
#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))]
6162
// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
6263
// 32-bit c_int. Below is -2, in two's complement, but that only works out
@@ -108,6 +109,7 @@ pub struct BorrowedHandle<'handle> {
108109
#[cfg(windows)]
109110
#[derive(Copy, Clone)]
110111
#[repr(transparent)]
112+
#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)]
111113
#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))]
112114
// This is -2, in two's complement. -1 is `INVALID_SOCKET`.
113115
#[cfg_attr(
@@ -133,6 +135,7 @@ pub struct BorrowedSocket<'socket> {
133135
/// has the value `-1`.
134136
#[cfg(any(unix, target_os = "wasi"))]
135137
#[repr(transparent)]
138+
#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)]
136139
#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))]
137140
// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
138141
// 32-bit c_int. Below is -2, in two's complement, but that only works out
@@ -266,6 +269,7 @@ impl OwnedHandle {
266269
/// [`INVALID_SOCKET`].
267270
#[cfg(windows)]
268271
#[repr(transparent)]
272+
#[cfg_attr(rustc_attrs, rustc_nonnull_optimization_guaranteed)]
269273
#[cfg_attr(rustc_attrs, rustc_layout_scalar_valid_range_start(0))]
270274
// This is -2, in two's complement. -1 is `INVALID_SOCKET`.
271275
#[cfg_attr(

0 commit comments

Comments
 (0)