Path: blob/main/crates/bevy_reflect/src/func/args/ownership.rs
6600 views
use core::fmt::{Display, Formatter};12/// The ownership of a type.3#[derive(Debug, Copy, Clone, PartialEq, Eq)]4pub enum Ownership {5/// The type is a reference (i.e. `&T`).6Ref,7/// The type is a mutable reference (i.e. `&mut T`).8Mut,9/// The type is owned (i.e. `T`).10Owned,11}1213impl Display for Ownership {14fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {15match self {16Self::Ref => write!(f, "reference"),17Self::Mut => write!(f, "mutable reference"),18Self::Owned => write!(f, "owned"),19}20}21}2223/// A trait for getting the ownership of a type.24///25/// This trait exists so that [`TypedFunction`] can automatically generate26/// [`FunctionInfo`] containing the proper [`Ownership`] for its [argument] types.27///28/// This trait is automatically implemented for non-reference types when using the `Reflect`29/// [derive macro]. Blanket impls cover `&T` and `&mut T`.30///31/// [`TypedFunction`]: crate::func::TypedFunction32/// [`FunctionInfo`]: crate::func::FunctionInfo33/// [argument]: crate::func::args::Arg34/// [derive macro]: derive@crate::Reflect35pub trait GetOwnership {36/// Returns the ownership of [`Self`].37fn ownership() -> Ownership {38Ownership::Owned39}40}4142// Blanket impl.43impl<T> GetOwnership for &'_ T {44fn ownership() -> Ownership {45Ownership::Ref46}47}4849// Blanket impl.50impl<T> GetOwnership for &'_ mut T {51fn ownership() -> Ownership {52Ownership::Mut53}54}5556/// Implements the [`GetOwnership`] trait for the given type.57///58/// This will implement it for `$ty`, `&$ty`, and `&mut $ty`.59///60/// See [`impl_function_traits`] for details on syntax.61///62/// [`impl_function_traits`]: crate::func::macros::impl_function_traits63macro_rules! impl_get_ownership {64(65$ty: ty66$(;67< $($T: ident $(: $T1: tt $(+ $T2: tt)*)?),* >68)?69$(70[ $(const $N: ident : $size: ident),* ]71)?72$(73where $($U: ty $(: $U1: tt $(+ $U2: tt)*)?),*74)?75) => {76impl <77$($($T $(: $T1 $(+ $T2)*)?),*)?78$(, $(const $N : $size),*)?79> $crate::func::args::GetOwnership for $ty80$(81where $($U $(: $U1 $(+ $U2)*)?),*82)?83{}84};85}8687pub(crate) use impl_get_ownership;888990