Skip to main content

bevy_reflect/
error.rs

1use crate::FieldId;
2use alloc::{borrow::Cow, format};
3use thiserror::Error;
4
5/// An error that occurs when cloning a type via [`PartialReflect::reflect_clone`].
6///
7/// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
8#[derive(#[automatically_derived]
impl ::core::clone::Clone for ReflectCloneError {
    #[inline]
    fn clone(&self) -> ReflectCloneError {
        match self {
            ReflectCloneError::NotImplemented { type_path: __self_0 } =>
                ReflectCloneError::NotImplemented {
                    type_path: ::core::clone::Clone::clone(__self_0),
                },
            ReflectCloneError::NotCloneable { type_path: __self_0 } =>
                ReflectCloneError::NotCloneable {
                    type_path: ::core::clone::Clone::clone(__self_0),
                },
            ReflectCloneError::FieldNotCloneable {
                field: __self_0,
                variant: __self_1,
                container_type_path: __self_2 } =>
                ReflectCloneError::FieldNotCloneable {
                    field: ::core::clone::Clone::clone(__self_0),
                    variant: ::core::clone::Clone::clone(__self_1),
                    container_type_path: ::core::clone::Clone::clone(__self_2),
                },
            ReflectCloneError::FailedDowncast {
                expected: __self_0, received: __self_1 } =>
                ReflectCloneError::FailedDowncast {
                    expected: ::core::clone::Clone::clone(__self_0),
                    received: ::core::clone::Clone::clone(__self_1),
                },
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ReflectCloneError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ReflectCloneError::NotImplemented { type_path: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotImplemented", "type_path", &__self_0),
            ReflectCloneError::NotCloneable { type_path: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "NotCloneable", "type_path", &__self_0),
            ReflectCloneError::FieldNotCloneable {
                field: __self_0,
                variant: __self_1,
                container_type_path: __self_2 } =>
                ::core::fmt::Formatter::debug_struct_field3_finish(f,
                    "FieldNotCloneable", "field", __self_0, "variant", __self_1,
                    "container_type_path", &__self_2),
            ReflectCloneError::FailedDowncast {
                expected: __self_0, received: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "FailedDowncast", "expected", __self_0, "received",
                    &__self_1),
        }
    }
}Debug, #[allow(unused_qualifications)]
#[automatically_derived]
impl ::core::fmt::Display for ReflectCloneError {
    fn fmt(&self, __formatter: &mut ::core::fmt::Formatter)
        -> ::core::fmt::Result {
        use ::thiserror::__private18::AsDisplay as _;

        #[allow(unused_variables, deprecated, clippy ::
        used_underscore_binding)]
        match self {
            ReflectCloneError::NotImplemented { type_path } =>
                match (type_path.as_display(),) {
                    (__display_type_path,) =>
                        __formatter.write_fmt(format_args!("`PartialReflect::reflect_clone` not implemented for `{0}`",
                                __display_type_path)),
                },
            ReflectCloneError::NotCloneable { type_path } =>
                match (type_path.as_display(),) {
                    (__display_type_path,) =>
                        __formatter.write_fmt(format_args!("`{0}` cannot be made cloneable for `PartialReflect::reflect_clone`",
                                __display_type_path)),
                },
            ReflectCloneError::FieldNotCloneable {
                field, variant, container_type_path } =>
                __formatter.write_fmt(format_args!("field `{0}` cannot be made cloneable for `PartialReflect::reflect_clone` (are you missing a `#[reflect(clone)]` attribute?)",
                        full_path(field, variant.as_deref(), container_type_path))),
            ReflectCloneError::FailedDowncast { expected, received } =>
                match (expected.as_display(), received.as_display()) {
                    (__display_expected, __display_received) =>
                        __formatter.write_fmt(format_args!("expected downcast to `{0}`, but received `{1}`",
                                __display_expected, __display_received)),
                },
        }
    }
}Error, #[automatically_derived]
impl ::core::cmp::PartialEq for ReflectCloneError {
    #[inline]
    fn eq(&self, other: &ReflectCloneError) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ReflectCloneError::NotImplemented { type_path: __self_0 },
                    ReflectCloneError::NotImplemented { type_path: __arg1_0 })
                    => __self_0 == __arg1_0,
                (ReflectCloneError::NotCloneable { type_path: __self_0 },
                    ReflectCloneError::NotCloneable { type_path: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                (ReflectCloneError::FieldNotCloneable {
                    field: __self_0,
                    variant: __self_1,
                    container_type_path: __self_2 },
                    ReflectCloneError::FieldNotCloneable {
                    field: __arg1_0,
                    variant: __arg1_1,
                    container_type_path: __arg1_2 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                (ReflectCloneError::FailedDowncast {
                    expected: __self_0, received: __self_1 },
                    ReflectCloneError::FailedDowncast {
                    expected: __arg1_0, received: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ReflectCloneError {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Cow<'static, str>>;
        let _: ::core::cmp::AssertParamIsEq<Cow<'static, str>>;
        let _: ::core::cmp::AssertParamIsEq<FieldId>;
        let _: ::core::cmp::AssertParamIsEq<Option<Cow<'static, str>>>;
        let _: ::core::cmp::AssertParamIsEq<Cow<'static, str>>;
        let _: ::core::cmp::AssertParamIsEq<Cow<'static, str>>;
        let _: ::core::cmp::AssertParamIsEq<Cow<'static, str>>;
    }
}Eq)]
9pub enum ReflectCloneError {
10    /// The type does not have a custom implementation for [`PartialReflect::reflect_clone`].
11    ///
12    /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
13    #[error("`PartialReflect::reflect_clone` not implemented for `{type_path}`")]
14    NotImplemented {
15        /// The fully qualified path of the type that [`PartialReflect::reflect_clone`](crate::PartialReflect::reflect_clone) is not implemented for.
16        type_path: Cow<'static, str>,
17    },
18    /// The type cannot be cloned via [`PartialReflect::reflect_clone`].
19    ///
20    /// This type should be returned when a type is intentionally opting out of reflection cloning.
21    ///
22    /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
23    #[error("`{type_path}` cannot be made cloneable for `PartialReflect::reflect_clone`")]
24    NotCloneable {
25        /// The fully qualified path of the type that cannot be cloned via [`PartialReflect::reflect_clone`](crate::PartialReflect::reflect_clone).
26        type_path: Cow<'static, str>,
27    },
28    /// The field cannot be cloned via [`PartialReflect::reflect_clone`].
29    ///
30    /// When [deriving `Reflect`], this usually means that a field marked with `#[reflect(ignore)]`
31    /// is missing a `#[reflect(clone)]` attribute.
32    ///
33    /// This may be intentional if the field is not meant/able to be cloned.
34    ///
35    /// [`PartialReflect::reflect_clone`]: crate::PartialReflect::reflect_clone
36    /// [deriving `Reflect`]: derive@crate::Reflect
37    #[error(
38        "field `{}` cannot be made cloneable for `PartialReflect::reflect_clone` (are you missing a `#[reflect(clone)]` attribute?)",
39        full_path(.field, .variant.as_deref(), .container_type_path)
40    )]
41    FieldNotCloneable {
42        /// Struct field or enum variant field which cannot be cloned.
43        field: FieldId,
44        /// Variant this field is part of if the container is an enum, otherwise [`None`].
45        variant: Option<Cow<'static, str>>,
46        /// Fully qualified path of the type containing this field.
47        container_type_path: Cow<'static, str>,
48    },
49    /// Could not downcast to the expected type.
50    ///
51    /// Realistically this should only occur when a type has incorrectly implemented [`Reflect`].
52    ///
53    /// [`Reflect`]: crate::Reflect
54    #[error("expected downcast to `{expected}`, but received `{received}`")]
55    FailedDowncast {
56        /// The fully qualified path of the type that was expected.
57        expected: Cow<'static, str>,
58        /// The fully qualified path of the type that was received.
59        received: Cow<'static, str>,
60    },
61}
62
63fn full_path(
64    field: &FieldId,
65    variant: Option<&str>,
66    container_type_path: &str,
67) -> alloc::string::String {
68    match variant {
69        Some(variant) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{1}::{2}",
                container_type_path, variant, field))
    })format!("{container_type_path}::{variant}::{field}"),
70        None => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::{1}", container_type_path,
                field))
    })format!("{container_type_path}::{field}"),
71    }
72}