Path: blob/main/crates/bevy_reflect/src/func/args/info.rs
6600 views
use alloc::borrow::Cow;12use crate::{3func::args::{GetOwnership, Ownership},4type_info::impl_type_methods,5Type, TypePath,6};78/// Type information for an [`Arg`] used in a [`DynamicFunction`] or [`DynamicFunctionMut`].9///10/// [`Arg`]: crate::func::args::Arg11/// [`DynamicFunction`]: crate::func::DynamicFunction12/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut13#[derive(Debug, Clone)]14pub struct ArgInfo {15/// The index of the argument within its function.16index: usize,17/// The name of the argument (if provided).18name: Option<Cow<'static, str>>,19/// The ownership of the argument.20ownership: Ownership,21/// The [type] of the argument.22///23/// [type]: Type24ty: Type,25}2627impl ArgInfo {28/// Create a new [`ArgInfo`] with the given argument index and type `T`.29///30/// To set the name of the argument, use [`Self::with_name`].31pub fn new<T: TypePath + GetOwnership>(index: usize) -> Self {32Self {33index,34name: None,35ownership: T::ownership(),36ty: Type::of::<T>(),37}38}3940/// Set the name of the argument.41///42/// Reflected arguments are not required to have a name and by default are not given one,43/// so this method must be called manually to set the name.44pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {45self.name = Some(name.into());46self47}4849/// The index of the argument within its function.50pub fn index(&self) -> usize {51self.index52}5354/// The name of the argument, if it was given one.55///56/// Note that this may return `None` even if the argument has a name.57/// This is because the name needs to be manually set using [`Self::with_name`]58/// since the name can't be inferred from the function type alone.59///60/// For [`DynamicFunctions`] created using [`IntoFunction`]61/// and [`DynamicFunctionMuts`] created using [`IntoFunctionMut`],62/// the name will always be `None`.63///64/// [`DynamicFunctions`]: crate::func::DynamicFunction65/// [`IntoFunction`]: crate::func::IntoFunction66/// [`DynamicFunctionMuts`]: crate::func::DynamicFunctionMut67/// [`IntoFunctionMut`]: crate::func::IntoFunctionMut68pub fn name(&self) -> Option<&str> {69self.name.as_deref()70}7172/// The ownership of the argument.73pub fn ownership(&self) -> Ownership {74self.ownership75}7677impl_type_methods!(ty);7879/// Get an ID representing the argument.80///81/// This will return `ArgId::Name` if the argument has a name,82/// otherwise `ArgId::Index`.83pub fn id(&self) -> ArgId {84self.name85.clone()86.map(ArgId::Name)87.unwrap_or_else(|| ArgId::Index(self.index))88}89}9091/// A representation of an argument.92///93/// This is primarily used for error reporting and debugging.94#[derive(Debug, Clone, PartialEq, Eq)]95pub enum ArgId {96/// The index of the argument within its function.97Index(usize),98/// The name of the argument.99Name(Cow<'static, str>),100}101102103