Skip to main content

bevy_reflect/
remote.rs

1use crate::Reflect;
2
3/// Marks a type as a [reflectable] wrapper for a remote type.
4///
5/// This allows types from external libraries (remote types) to be included in reflection.
6///
7/// The [`#[reflect_remote]`](crate::reflect_remote) attribute macro generates a
8/// `#[repr(transparent)]` wrapper and an implementation of this trait. Its conversion methods
9/// use the wrapper's transparent representation.
10///
11/// Manual implementations may use different representation and conversion behavior. The
12/// associated `Remote` type identifies the remote type represented by this wrapper.
13///
14/// # Example
15///
16/// ```
17/// use bevy_reflect_derive::{reflect_remote, Reflect};
18///
19/// mod some_lib {
20///   pub struct TheirType {
21///     pub value: u32
22///   }
23/// }
24///
25/// #[reflect_remote(some_lib::TheirType)]
26/// struct MyType {
27///   pub value: u32
28/// }
29///
30/// #[derive(Reflect)]
31/// struct MyStruct {
32///   #[reflect(remote = MyType)]
33///   data: some_lib::TheirType,
34/// }
35/// ```
36///
37/// [reflectable]: Reflect
38pub trait ReflectRemote: Reflect {
39    /// The remote type this type represents via reflection.
40    type Remote;
41
42    /// Converts a reference of this wrapper to a reference of its remote type.
43    fn as_remote(&self) -> &Self::Remote;
44    /// Converts a mutable reference of this wrapper to a mutable reference of its remote type.
45    fn as_remote_mut(&mut self) -> &mut Self::Remote;
46    /// Converts this wrapper into its remote type.
47    fn into_remote(self) -> Self::Remote;
48
49    /// Converts a reference of the remote type to a reference of this wrapper.
50    fn as_wrapper(remote: &Self::Remote) -> &Self;
51    /// Converts a mutable reference of the remote type to a mutable reference of this wrapper.
52    fn as_wrapper_mut(remote: &mut Self::Remote) -> &mut Self;
53    /// Converts the remote type into this wrapper.
54    fn into_wrapper(remote: Self::Remote) -> Self;
55}