
# Objective - Contributes to #15460 ## Solution - Added `std` feature (enabled by default) ## Testing - CI - `cargo check -p bevy_reflect --no-default-features --target "x86_64-unknown-none"` - UEFI demo application runs with this branch of `bevy_reflect`, allowing `derive(Reflect)` ## Notes - The [`spin`](https://crates.io/crates/spin) crate has been included to provide `RwLock` and `Once` (as an alternative to `OnceLock`) when the `std` feature is not enabled. Another alternative may be more desirable, please provide feedback if you have a strong opinion here! - Certain items (`Box`, `String`, `ToString`) provided by `alloc` have been added to `__macro_exports` as a way to avoid `alloc` vs `std` namespacing. I'm personally quite annoyed that we can't rely on `alloc` as a crate name in `std` environments within macros. I'd love an alternative to my approach here, but I suspect it's the least-bad option. - I would've liked to have an `alloc` feature (for allocation-free `bevy_reflect`), unfortunately, `erased_serde` unconditionally requires access to `Box`. Maybe one day we could design around this, but for now it just means `bevy_reflect` requires `alloc`. --------- Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
109 lines
3.2 KiB
Rust
109 lines
3.2 KiB
Rust
use proc_macro2::TokenStream;
|
|
use quote::{quote, ToTokens};
|
|
use syn::{spanned::Spanned, LitStr};
|
|
|
|
/// Contains tokens representing different kinds of string.
|
|
#[derive(Clone)]
|
|
pub(crate) enum StringExpr {
|
|
/// A string that is valid at compile time.
|
|
///
|
|
/// This is either a string literal like `"mystring"`,
|
|
/// or a string created by a macro like [`module_path`]
|
|
/// or [`concat`].
|
|
Const(TokenStream),
|
|
/// A [string slice](str) that is borrowed for a `'static` lifetime.
|
|
Borrowed(TokenStream),
|
|
/// An [owned string](String).
|
|
Owned(TokenStream),
|
|
}
|
|
|
|
impl<T: ToString + Spanned> From<T> for StringExpr {
|
|
fn from(value: T) -> Self {
|
|
Self::from_lit(&LitStr::new(&value.to_string(), value.span()))
|
|
}
|
|
}
|
|
|
|
impl StringExpr {
|
|
/// Creates a [constant] [`StringExpr`] from a [`struct@LitStr`].
|
|
///
|
|
/// [constant]: StringExpr::Const
|
|
pub fn from_lit(lit: &LitStr) -> Self {
|
|
Self::Const(lit.to_token_stream())
|
|
}
|
|
|
|
/// Creates a [constant] [`StringExpr`] by interpreting a [string slice][str] as a [`struct@LitStr`].
|
|
///
|
|
/// [constant]: StringExpr::Const
|
|
pub fn from_str(string: &str) -> Self {
|
|
Self::Const(string.into_token_stream())
|
|
}
|
|
|
|
/// Returns tokens for an [owned string](String).
|
|
///
|
|
/// The returned expression will allocate unless the [`StringExpr`] is [already owned].
|
|
///
|
|
/// [already owned]: StringExpr::Owned
|
|
pub fn into_owned(self) -> TokenStream {
|
|
let bevy_reflect_path = crate::meta::get_bevy_reflect_path();
|
|
|
|
match self {
|
|
Self::Const(tokens) | Self::Borrowed(tokens) => quote! {
|
|
#bevy_reflect_path::__macro_exports::alloc_utils::ToString::to_string(#tokens)
|
|
},
|
|
Self::Owned(owned) => owned,
|
|
}
|
|
}
|
|
|
|
/// Returns tokens for a statically borrowed [string slice](str).
|
|
pub fn into_borrowed(self) -> TokenStream {
|
|
match self {
|
|
Self::Const(tokens) | Self::Borrowed(tokens) => tokens,
|
|
Self::Owned(owned) => quote! {
|
|
&#owned
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Appends a [`StringExpr`] to another.
|
|
///
|
|
/// If both expressions are [`StringExpr::Const`] this will use [`concat`] to merge them.
|
|
pub fn appended_by(mut self, other: StringExpr) -> Self {
|
|
if let Self::Const(tokens) = self {
|
|
if let Self::Const(more) = other {
|
|
return Self::Const(quote! {
|
|
::core::concat!(#tokens, #more)
|
|
});
|
|
}
|
|
self = Self::Const(tokens);
|
|
}
|
|
|
|
let owned = self.into_owned();
|
|
let borrowed = other.into_borrowed();
|
|
Self::Owned(quote! {
|
|
#owned + #borrowed
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Default for StringExpr {
|
|
fn default() -> Self {
|
|
StringExpr::from_str("")
|
|
}
|
|
}
|
|
|
|
impl FromIterator<StringExpr> for StringExpr {
|
|
fn from_iter<T: IntoIterator<Item = StringExpr>>(iter: T) -> Self {
|
|
let mut iter = iter.into_iter();
|
|
match iter.next() {
|
|
Some(mut expr) => {
|
|
for next in iter {
|
|
expr = expr.appended_by(next);
|
|
}
|
|
|
|
expr
|
|
}
|
|
None => Default::default(),
|
|
}
|
|
}
|
|
}
|