Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/serde/de/error_utils.rs
6600 views
1
use core::fmt::Display;
2
use serde::de::Error;
3
4
#[cfg(feature = "debug_stack")]
5
use std::thread_local;
6
7
#[cfg(feature = "debug_stack")]
8
thread_local! {
9
/// The thread-local [`TypeInfoStack`] used for debugging.
10
///
11
/// [`TypeInfoStack`]: crate::type_info_stack::TypeInfoStack
12
pub(super) static TYPE_INFO_STACK: core::cell::RefCell<crate::type_info_stack::TypeInfoStack> = const { core::cell::RefCell::new(
13
crate::type_info_stack::TypeInfoStack::new()
14
) };
15
}
16
17
/// A helper function for generating a custom deserialization error message.
18
///
19
/// This function should be preferred over [`Error::custom`] as it will include
20
/// other useful information, such as the [type info stack].
21
///
22
/// [type info stack]: crate::type_info_stack::TypeInfoStack
23
pub(super) fn make_custom_error<E: Error>(msg: impl Display) -> E {
24
#[cfg(feature = "debug_stack")]
25
return TYPE_INFO_STACK
26
.with_borrow(|stack| E::custom(format_args!("{msg} (stack: {stack:?})")));
27
#[cfg(not(feature = "debug_stack"))]
28
return E::custom(msg);
29
}
30
31