bevy/crates
James Liu 56bcbb0975
Forbid unsafe in most crates in the engine (#12684)
# Objective
Resolves #3824. `unsafe` code should be the exception, not the norm in
Rust. It's obviously needed for various use cases as it's interfacing
with platforms and essentially running the borrow checker at runtime in
the ECS, but the touted benefits of Bevy is that we are able to heavily
leverage Rust's safety, and we should be holding ourselves accountable
to that by minimizing our unsafe footprint.

## Solution
Deny `unsafe_code` workspace wide. Add explicit exceptions for the
following crates, and forbid it in almost all of the others.

* bevy_ecs - Obvious given how much unsafe is needed to achieve
performant results
* bevy_ptr - Works with raw pointers, even more low level than bevy_ecs.
 * bevy_render - due to needing to integrate with wgpu
 * bevy_window - due to needing to integrate with raw_window_handle
* bevy_utils - Several unsafe utilities used by bevy_ecs. Ideally moved
into bevy_ecs instead of made publicly usable.
 * bevy_reflect - Required for the unsafe type casting it's doing.
 * bevy_transform - for the parallel transform propagation
 * bevy_gizmos  - For the SystemParam impls it has.
* bevy_assets - To support reflection. Might not be required, not 100%
sure yet.
* bevy_mikktspace - due to being a conversion from a C library. Pending
safe rewrite.
* bevy_dynamic_plugin - Inherently unsafe due to the dynamic loading
nature.

Several uses of unsafe were rewritten, as they did not need to be using
them:

* bevy_text - a case of `Option::unchecked` could be rewritten as a
normal for loop and match instead of an iterator.
* bevy_color - the Pod/Zeroable implementations were replaceable with
bytemuck's derive macros.
2024-03-27 03:30:08 +00:00
..
bevy_a11y Set the logo and favicon for all of Bevy's published crates (#12696) 2024-03-25 18:52:50 +00:00
bevy_animation Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_app Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_asset Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_audio Set the logo and favicon for all of Bevy's published crates (#12696) 2024-03-25 18:52:50 +00:00
bevy_color Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_core Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_core_pipeline Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_derive Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_dev_tools Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_diagnostic Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_dylib Set the logo and favicon for all of Bevy's published crates (#12696) 2024-03-25 18:52:50 +00:00
bevy_dynamic_plugin Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_ecs Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_ecs_compile_fail_tests Fix Ci failing over dead code in tests (#12623) 2024-03-21 18:08:47 +00:00
bevy_encase_derive Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_gilrs Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_gizmos Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_gltf Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_hierarchy Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_input Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_internal Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_log Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_macro_utils Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_macros_compile_fail_tests Fix Ci failing over dead code in tests (#12623) 2024-03-21 18:08:47 +00:00
bevy_math Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_mikktspace Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_panic_handler Set the logo and favicon for all of Bevy's published crates (#12696) 2024-03-25 18:52:50 +00:00
bevy_pbr Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_ptr Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_reflect Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_reflect_compile_fail_tests Fix Ci failing over dead code in tests (#12623) 2024-03-21 18:08:47 +00:00
bevy_render Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_scene Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_sprite Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_tasks Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_text Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_time Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_transform Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_ui Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_utils Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_window Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00
bevy_winit Forbid unsafe in most crates in the engine (#12684) 2024-03-27 03:30:08 +00:00