Path: blob/main/crates/bevy_reflect/src/serde/ser/error_utils.rs
6600 views
use core::fmt::Display;1use serde::ser::Error;23#[cfg(feature = "debug_stack")]4use std::thread_local;56#[cfg(feature = "debug_stack")]7thread_local! {8/// The thread-local [`TypeInfoStack`] used for debugging.9///10/// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack11pub(super) static TYPE_INFO_STACK: core::cell::RefCell<crate::type_info_stack::TypeInfoStack> = const { core::cell::RefCell::new(12crate::type_info_stack::TypeInfoStack::new()13) };14}1516/// A helper function for generating a custom serialization error message.17///18/// This function should be preferred over [`Error::custom`] as it will include19/// other useful information, such as the [type info stack].20///21/// [type info stack]: crate::type_info_stack::TypeInfoStack22pub(super) fn make_custom_error<E: Error>(msg: impl Display) -> E {23#[cfg(feature = "debug_stack")]24return TYPE_INFO_STACK25.with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})")));26#[cfg(not(feature = "debug_stack"))]27return E::custom(msg);28}293031