Skip to main content

bevy_reflect/info/
opaque.rs

1use crate::{
2    generics::impl_generic_info_methods, ty::impl_type_methods, Generics, Reflect, Type, TypePath,
3};
4
5/// A container for compile-time info related to reflection-opaque types, including primitives.
6///
7/// This typically represents a type which cannot be broken down any further. This is often
8/// due to technical reasons (or by definition), but it can also be a purposeful choice.
9///
10/// For example, [`i32`] cannot be broken down any further, so it is represented by an [`OpaqueInfo`].
11/// And while [`String`] itself is a struct, its fields are private, so we don't really treat
12/// it _as_ a struct. It therefore makes more sense to represent it as an [`OpaqueInfo`].
13///
14/// [`String`]: alloc::string::String
15#[derive(Debug, Clone)]
16pub struct OpaqueInfo {
17    ty: Type,
18    generics: Generics,
19    #[cfg(feature = "reflect_documentation")]
20    docs: Option<&'static str>,
21}
22
23impl OpaqueInfo {
24    /// Creates a new [`OpaqueInfo`].
25    pub fn new<T: Reflect + TypePath + ?Sized>() -> Self {
26        Self {
27            ty: Type::of::<T>(),
28            generics: Generics::new(),
29            #[cfg(feature = "reflect_documentation")]
30            docs: None,
31        }
32    }
33
34    /// Sets the docstring for this type.
35    #[cfg(feature = "reflect_documentation")]
36    pub fn with_docs(self, doc: Option<&'static str>) -> Self {
37        Self { docs: doc, ..self }
38    }
39
40    impl_type_methods!(ty);
41
42    /// The docstring of this dynamic type, if any.
43    #[cfg(feature = "reflect_documentation")]
44    pub fn docs(&self) -> Option<&'static str> {
45        self.docs
46    }
47
48    impl_generic_info_methods!(generics);
49}