bevy/crates/bevy_reflect/derive/src/struct_utility.rs
Zachary Harrold d70595b667
Add core and alloc over std Lints (#15281)
# Objective

- Fixes #6370
- Closes #6581

## Solution

- Added the following lints to the workspace:
  - `std_instead_of_core`
  - `std_instead_of_alloc`
  - `alloc_instead_of_core`
- Used `cargo +nightly fmt` with [item level use
formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Item%5C%3A)
to split all `use` statements into single items.
- Used `cargo clippy --workspace --all-targets --all-features --fix
--allow-dirty` to _attempt_ to resolve the new linting issues, and
intervened where the lint was unable to resolve the issue automatically
(usually due to needing an `extern crate alloc;` statement in a crate
root).
- Manually removed certain uses of `std` where negative feature gating
prevented `--all-features` from finding the offending uses.
- Used `cargo +nightly fmt` with [crate level use
formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Crate%5C%3A)
to re-merge all `use` statements matching Bevy's previous styling.
- Manually fixed cases where the `fmt` tool could not re-merge `use`
statements due to conditional compilation attributes.

## Testing

- Ran CI locally

## Migration Guide

The MSRV is now 1.81. Please update to this version or higher.

## Notes

- This is a _massive_ change to try and push through, which is why I've
outlined the semi-automatic steps I used to create this PR, in case this
fails and someone else tries again in the future.
- Making this change has no impact on user code, but does mean Bevy
contributors will be warned to use `core` and `alloc` instead of `std`
where possible.
- This lint is a critical first step towards investigating `no_std`
options for Bevy.

---------

Co-authored-by: François Mockers <francois.mockers@vleue.com>
2024-09-27 00:59:59 +00:00

81 lines
2.7 KiB
Rust

use crate::{derive_data::StructField, ReflectStruct};
use quote::quote;
/// A helper struct for creating remote-aware field accessors.
///
/// These are "remote-aware" because when a field is a remote field, it uses a [`transmute`] internally
/// to access the field.
///
/// [`transmute`]: std::mem::transmute
pub(crate) struct FieldAccessors {
/// The referenced field accessors, such as `&self.foo`.
pub fields_ref: Vec<proc_macro2::TokenStream>,
/// The mutably referenced field accessors, such as `&mut self.foo`.
pub fields_mut: Vec<proc_macro2::TokenStream>,
/// The ordered set of field indices (basically just the range of [0, `field_count`).
pub field_indices: Vec<usize>,
/// The number of fields in the reflected struct.
pub field_count: usize,
}
impl FieldAccessors {
pub fn new(reflect_struct: &ReflectStruct) -> Self {
let bevy_reflect_path = reflect_struct.meta().bevy_reflect_path();
let fields_ref = Self::get_fields(reflect_struct, |field, accessor| {
match &field.attrs.remote {
Some(wrapper_ty) => {
quote! {
<#wrapper_ty as #bevy_reflect_path::ReflectRemote>::as_wrapper(&#accessor)
}
}
None => quote!(& #accessor),
}
});
let fields_mut = Self::get_fields(reflect_struct, |field, accessor| {
match &field.attrs.remote {
Some(wrapper_ty) => {
quote! {
<#wrapper_ty as #bevy_reflect_path::ReflectRemote>::as_wrapper_mut(&mut #accessor)
}
}
None => quote!(&mut #accessor),
}
});
let field_count = fields_ref.len();
let field_indices = (0..field_count).collect();
Self {
fields_ref,
fields_mut,
field_indices,
field_count,
}
}
fn get_fields<F>(
reflect_struct: &ReflectStruct,
mut wrapper_fn: F,
) -> Vec<proc_macro2::TokenStream>
where
F: FnMut(&StructField, proc_macro2::TokenStream) -> proc_macro2::TokenStream,
{
let is_remote = reflect_struct.meta().is_remote_wrapper();
reflect_struct
.active_fields()
.map(|field| {
let member = crate::ident::ident_or_index(
field.data.ident.as_ref(),
field.declaration_index,
);
let accessor = if is_remote {
quote!(self.0.#member)
} else {
quote!(self.#member)
};
wrapper_fn(field, accessor)
})
.collect::<Vec<_>>()
}
}