rust/library/std/src
Jubilee d87b5e4727
Rollup merge of #116816 - ChrisDenton:api.rs, r=workingjubilee
Create `windows/api.rs` for safer FFI

FFI is inherently unsafe. For memory safety we need to assert that some contract is being upheld on both sides of the FFI, though of course we can only ever check our side. In Rust, `unsafe` blocks are used to assert safety and `// SAFETY` comments describing why it is safe. Currently in sys/windows we have a lot of this unsafety spread all over the place, with variations on the same unsafe patterns repeated. And because of the repitition and frequency, we're a bit lax with the safety comments.

This PR aims to fix this and to make FFI safety more auditable by creating an `api` module with the goal of centralising and consolidating this unsafety. It contains thin wrappers around the Windows API that make most functions safe to call or, if that's not possible, then at least safer. Note that its goal is *only* to address safety. It does not stray far from the Windows API and intentionally does not attempt to make higher lever wrappers around, for example, file handles. This is better left to the existing modules. The windows/api.rs file has a top level comment to help future contributors understand the intent of the module and the design decisions made.

I chose two functions as a first tentative step towards the above goal:

- [`GetLastError`](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror) is trivially safe. There's no reason to wrap it in an `unsafe` block every time. So I simply created a safe `get_last_error` wrapper.
- [`SetFileInformationByHandle`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileinformationbyhandle) is more complex. It essentially takes a generic type but over a C API which necessitates some amount of ceremony. Rather than implementing similar unsafe patterns in multiple places, I provide a safe `set_file_information_by_handle` that takes a Rusty generic type and handles converting that to the form required by the C FFI.

r? libs
2023-10-28 01:07:36 -07:00
..
backtrace Implement UnwindSafe and RefUnwindSafe for Backtrace 2023-07-31 10:38:24 +02:00
collections s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
env Better Debug for Vars and VarsOs 2023-08-07 12:18:27 -04:00
error core/any: remove Provider trait 2023-08-13 13:07:53 -06:00
f32 Add gamma and ln_gamma functions to f32 and f64 2023-07-31 07:41:50 -07:00
f64 Add gamma and ln_gamma functions to f32 and f64 2023-07-31 07:41:50 -07:00
ffi Bump version placeholders 2023-10-03 20:26:36 -04:00
fs Skip test if Unix sockets are unsupported 2023-10-20 18:10:34 +01:00
io Hide internal methods from documentation 2023-10-27 04:30:49 -04:00
net Auto merge of #105394 - Patiga:improve-udpsocket-docs, r=workingjubilee 2023-10-03 20:35:38 +00:00
num rustc_expand: Mark inner #![test] attributes as soft-unstable 2020-11-20 19:35:03 +03:00
os changes from feedback 2023-10-20 23:55:14 +01:00
panic review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
path std: add tests for Path::with_extension 2023-07-14 13:19:45 -03:00
prelude correct std::prelude comment 2023-04-27 15:56:57 +02:00
process Rollup merge of #114379 - RalfJung:command-removed-env-vars, r=m-ou-se 2023-09-22 12:15:25 +02:00
sync Implement OnceCell/Lock::try_insert() 2023-10-13 14:54:32 +02:00
sys Rollup merge of #116816 - ChrisDenton:api.rs, r=workingjubilee 2023-10-28 01:07:36 -07:00
sys_common Auto merge of #116132 - darthunix:connect_poll, r=cuviper 2023-10-19 11:22:28 +00:00
thread std:🧵 add SAFETY comment 2023-10-27 15:18:32 +02:00
time Rollup merge of #103056 - beetrees:timespec-bug-fix, r=thomcc 2023-05-05 18:40:32 +05:30
alloc.rs Correct and expand documentation of handle_alloc_error and set_alloc_error_hook. 2023-08-19 13:27:03 -07:00
ascii.rs Add the basic ascii::Char type 2023-05-03 22:09:33 -07:00
backtrace.rs Implement UnwindSafe and RefUnwindSafe for Backtrace 2023-07-31 10:38:24 +02:00
env.rs add a csky-unknown-linux-gnuabiv2 target 2023-08-14 23:02:36 +08:00
error.rs Expose core::error::request_value in std 2023-08-18 13:06:53 -04:00
f32.rs Rollup merge of #114754 - workingjubilee:gamma-ray-logger, r=thomcc 2023-08-25 09:00:12 +02:00
f64.rs Rollup merge of #114754 - workingjubilee:gamma-ray-logger, r=thomcc 2023-08-25 09:00:12 +02:00
fs.rs Add more diagnostic items for clippy 2023-10-05 18:21:47 -04:00
keyword_docs.rs docs: Add example, reference link for type keyword. 2023-08-21 20:00:43 +07:00
lib.rs Stabilize [const_]pointer_byte_offsets 2023-10-25 22:35:12 +00:00
macros.rs print macros: add xrefs to format syntax documentation 2023-08-28 11:51:41 +02:00
num.rs Bump version placeholders 2023-10-03 20:26:36 -04:00
panic.rs Shorten lifetime of even more panic temporaries 2023-05-15 03:47:37 -07:00
panicking.rs Rollup merge of #116008 - m-ou-se:boxmeup, r=oli-obk 2023-09-21 00:11:37 +02:00
path.rs fix(std): Rename os_str_bytes to encoded_bytes 2023-09-01 19:33:16 -05:00
process.rs Deduplicate std::process Default impl feature names 2023-10-15 11:42:28 -07:00
rt.rs Automatically enable cross-crate inlining for small functions 2023-10-17 19:53:51 -04:00
time.rs Auto merge of #116238 - tamird:gettimeofday, r=thomcc 2023-10-24 04:15:39 +00:00