Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/func/args/info.rs
6600 views
1
use alloc::borrow::Cow;
2
3
use crate::{
4
func::args::{GetOwnership, Ownership},
5
type_info::impl_type_methods,
6
Type, TypePath,
7
};
8
9
/// Type information for an [`Arg`] used in a [`DynamicFunction`] or [`DynamicFunctionMut`].
10
///
11
/// [`Arg`]: crate::func::args::Arg
12
/// [`DynamicFunction`]: crate::func::DynamicFunction
13
/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
14
#[derive(Debug, Clone)]
15
pub struct ArgInfo {
16
/// The index of the argument within its function.
17
index: usize,
18
/// The name of the argument (if provided).
19
name: Option<Cow<'static, str>>,
20
/// The ownership of the argument.
21
ownership: Ownership,
22
/// The [type] of the argument.
23
///
24
/// [type]: Type
25
ty: Type,
26
}
27
28
impl ArgInfo {
29
/// Create a new [`ArgInfo`] with the given argument index and type `T`.
30
///
31
/// To set the name of the argument, use [`Self::with_name`].
32
pub fn new<T: TypePath + GetOwnership>(index: usize) -> Self {
33
Self {
34
index,
35
name: None,
36
ownership: T::ownership(),
37
ty: Type::of::<T>(),
38
}
39
}
40
41
/// Set the name of the argument.
42
///
43
/// Reflected arguments are not required to have a name and by default are not given one,
44
/// so this method must be called manually to set the name.
45
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
46
self.name = Some(name.into());
47
self
48
}
49
50
/// The index of the argument within its function.
51
pub fn index(&self) -> usize {
52
self.index
53
}
54
55
/// The name of the argument, if it was given one.
56
///
57
/// Note that this may return `None` even if the argument has a name.
58
/// This is because the name needs to be manually set using [`Self::with_name`]
59
/// since the name can't be inferred from the function type alone.
60
///
61
/// For [`DynamicFunctions`] created using [`IntoFunction`]
62
/// and [`DynamicFunctionMuts`] created using [`IntoFunctionMut`],
63
/// the name will always be `None`.
64
///
65
/// [`DynamicFunctions`]: crate::func::DynamicFunction
66
/// [`IntoFunction`]: crate::func::IntoFunction
67
/// [`DynamicFunctionMuts`]: crate::func::DynamicFunctionMut
68
/// [`IntoFunctionMut`]: crate::func::IntoFunctionMut
69
pub fn name(&self) -> Option<&str> {
70
self.name.as_deref()
71
}
72
73
/// The ownership of the argument.
74
pub fn ownership(&self) -> Ownership {
75
self.ownership
76
}
77
78
impl_type_methods!(ty);
79
80
/// Get an ID representing the argument.
81
///
82
/// This will return `ArgId::Name` if the argument has a name,
83
/// otherwise `ArgId::Index`.
84
pub fn id(&self) -> ArgId {
85
self.name
86
.clone()
87
.map(ArgId::Name)
88
.unwrap_or_else(|| ArgId::Index(self.index))
89
}
90
}
91
92
/// A representation of an argument.
93
///
94
/// This is primarily used for error reporting and debugging.
95
#[derive(Debug, Clone, PartialEq, Eq)]
96
pub enum ArgId {
97
/// The index of the argument within its function.
98
Index(usize),
99
/// The name of the argument.
100
Name(Cow<'static, str>),
101
}
102
103