|
1 | | -use crate::func::args::{ArgError, ArgInfo, Ownership}; |
2 | | -use crate::Reflect; |
| 1 | +use crate::func::args::{ArgError, FromArg, Ownership}; |
| 2 | +use crate::{Reflect, TypePath}; |
| 3 | +use std::ops::Deref; |
3 | 4 |
|
4 | | -/// Represents an argument that can be passed to a [`DynamicFunction`] or [`DynamicClosure`]. |
| 5 | +/// Represents an argument that can be passed to a [`DynamicFunction`], [`DynamicClosure`], |
| 6 | +/// or [`DynamicClosureMut`]. |
5 | 7 | /// |
6 | 8 | /// [`DynamicFunction`]: crate::func::DynamicFunction |
7 | 9 | /// [`DynamicClosure`]: crate::func::DynamicClosure |
| 10 | +/// [`DynamicClosureMut`]: crate::func::DynamicClosureMut |
8 | 11 | #[derive(Debug)] |
9 | | -pub enum Arg<'a> { |
10 | | - Owned(Box<dyn Reflect>), |
11 | | - Ref(&'a dyn Reflect), |
12 | | - Mut(&'a mut dyn Reflect), |
| 12 | +pub struct Arg<'a> { |
| 13 | + index: usize, |
| 14 | + value: ArgValue<'a>, |
13 | 15 | } |
14 | 16 |
|
15 | 17 | impl<'a> Arg<'a> { |
16 | | - /// Returns `Ok(T)` if the argument is [`Arg::Owned`]. |
17 | | - pub fn take_owned<T: Reflect>(self, info: &ArgInfo) -> Result<T, ArgError> { |
18 | | - match self { |
19 | | - Arg::Owned(arg) => arg.take().map_err(|arg| ArgError::UnexpectedType { |
20 | | - id: info.id().clone(), |
21 | | - expected: ::std::borrow::Cow::Borrowed(info.type_path()), |
22 | | - received: ::std::borrow::Cow::Owned(arg.reflect_type_path().to_string()), |
| 18 | + /// Create a new [`Arg`] with the given index and value. |
| 19 | + pub fn new(index: usize, value: ArgValue<'a>) -> Self { |
| 20 | + Self { index, value } |
| 21 | + } |
| 22 | + |
| 23 | + /// The index of the argument. |
| 24 | + pub fn index(&self) -> usize { |
| 25 | + self.index |
| 26 | + } |
| 27 | + |
| 28 | + /// Set the index of the argument. |
| 29 | + pub(crate) fn set_index(&mut self, index: usize) { |
| 30 | + self.index = index; |
| 31 | + } |
| 32 | + |
| 33 | + /// The value of the argument. |
| 34 | + pub fn value(&self) -> &ArgValue<'a> { |
| 35 | + &self.value |
| 36 | + } |
| 37 | + |
| 38 | + /// Take the value of the argument. |
| 39 | + pub fn take_value(self) -> ArgValue<'a> { |
| 40 | + self.value |
| 41 | + } |
| 42 | + |
| 43 | + /// Take the value of the argument and attempt to convert it to a concrete value, `T`. |
| 44 | + /// |
| 45 | + /// This is a convenience method for calling [`FromArg::from_arg`] on the argument. |
| 46 | + /// |
| 47 | + /// # Example |
| 48 | + /// |
| 49 | + /// ``` |
| 50 | + /// # use bevy_reflect::func::ArgList; |
| 51 | + /// let a = 1u32; |
| 52 | + /// let b = 2u32; |
| 53 | + /// let mut c = 3u32; |
| 54 | + /// let mut args = ArgList::new().push_owned(a).push_ref(&b).push_mut(&mut c); |
| 55 | + /// |
| 56 | + /// let a = args.take::<u32>().unwrap(); |
| 57 | + /// assert_eq!(a, 1); |
| 58 | + /// |
| 59 | + /// let b = args.take::<&u32>().unwrap(); |
| 60 | + /// assert_eq!(*b, 2); |
| 61 | + /// |
| 62 | + /// let c = args.take::<&mut u32>().unwrap(); |
| 63 | + /// assert_eq!(*c, 3); |
| 64 | + /// ``` |
| 65 | + pub fn take<T: FromArg>(self) -> Result<T::This<'a>, ArgError> { |
| 66 | + T::from_arg(self) |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns `Ok(T)` if the argument is [`ArgValue::Owned`]. |
| 70 | + /// |
| 71 | + /// If the argument is not owned, returns an error. |
| 72 | + /// |
| 73 | + /// It's generally preferred to use [`Self::take`] instead of this method. |
| 74 | + /// |
| 75 | + /// # Example |
| 76 | + /// |
| 77 | + /// ``` |
| 78 | + /// # use bevy_reflect::func::ArgList; |
| 79 | + /// let value = 123u32; |
| 80 | + /// let mut args = ArgList::new().push_owned(value); |
| 81 | + /// let value = args.take_owned::<u32>().unwrap(); |
| 82 | + /// assert_eq!(value, 123); |
| 83 | + /// ``` |
| 84 | + pub fn take_owned<T: Reflect + TypePath>(self) -> Result<T, ArgError> { |
| 85 | + match self.value { |
| 86 | + ArgValue::Owned(arg) => arg.take().map_err(|arg| ArgError::UnexpectedType { |
| 87 | + index: self.index, |
| 88 | + expected: std::borrow::Cow::Borrowed(T::type_path()), |
| 89 | + received: std::borrow::Cow::Owned(arg.reflect_type_path().to_string()), |
23 | 90 | }), |
24 | | - Arg::Ref(_) => Err(ArgError::InvalidOwnership { |
25 | | - id: info.id().clone(), |
| 91 | + ArgValue::Ref(_) => Err(ArgError::InvalidOwnership { |
| 92 | + index: self.index, |
26 | 93 | expected: Ownership::Owned, |
27 | 94 | received: Ownership::Ref, |
28 | 95 | }), |
29 | | - Arg::Mut(_) => Err(ArgError::InvalidOwnership { |
30 | | - id: info.id().clone(), |
| 96 | + ArgValue::Mut(_) => Err(ArgError::InvalidOwnership { |
| 97 | + index: self.index, |
31 | 98 | expected: Ownership::Owned, |
32 | 99 | received: Ownership::Mut, |
33 | 100 | }), |
34 | 101 | } |
35 | 102 | } |
36 | 103 |
|
37 | | - /// Returns `Ok(&T)` if the argument is [`Arg::Ref`]. |
38 | | - pub fn take_ref<T: Reflect>(self, info: &ArgInfo) -> Result<&'a T, ArgError> { |
39 | | - match self { |
40 | | - Arg::Owned(_) => Err(ArgError::InvalidOwnership { |
41 | | - id: info.id().clone(), |
| 104 | + /// Returns `Ok(&T)` if the argument is [`ArgValue::Ref`]. |
| 105 | + /// |
| 106 | + /// If the argument is not a reference, returns an error. |
| 107 | + /// |
| 108 | + /// It's generally preferred to use [`Self::take`] instead of this method. |
| 109 | + /// |
| 110 | + /// # Example |
| 111 | + /// |
| 112 | + /// ``` |
| 113 | + /// # use bevy_reflect::func::ArgList; |
| 114 | + /// let value = 123u32; |
| 115 | + /// let mut args = ArgList::new().push_ref(&value); |
| 116 | + /// let value = args.take_ref::<u32>().unwrap(); |
| 117 | + /// assert_eq!(*value, 123); |
| 118 | + /// ``` |
| 119 | + pub fn take_ref<T: Reflect + TypePath>(self) -> Result<&'a T, ArgError> { |
| 120 | + match self.value { |
| 121 | + ArgValue::Owned(_) => Err(ArgError::InvalidOwnership { |
| 122 | + index: self.index, |
42 | 123 | expected: Ownership::Ref, |
43 | 124 | received: Ownership::Owned, |
44 | 125 | }), |
45 | | - Arg::Ref(arg) => Ok(arg.downcast_ref().ok_or_else(|| ArgError::UnexpectedType { |
46 | | - id: info.id().clone(), |
47 | | - expected: ::std::borrow::Cow::Borrowed(info.type_path()), |
48 | | - received: ::std::borrow::Cow::Owned(arg.reflect_type_path().to_string()), |
49 | | - })?), |
50 | | - Arg::Mut(_) => Err(ArgError::InvalidOwnership { |
51 | | - id: info.id().clone(), |
| 126 | + ArgValue::Ref(arg) => { |
| 127 | + Ok(arg.downcast_ref().ok_or_else(|| ArgError::UnexpectedType { |
| 128 | + index: self.index, |
| 129 | + expected: std::borrow::Cow::Borrowed(T::type_path()), |
| 130 | + received: std::borrow::Cow::Owned(arg.reflect_type_path().to_string()), |
| 131 | + })?) |
| 132 | + } |
| 133 | + ArgValue::Mut(_) => Err(ArgError::InvalidOwnership { |
| 134 | + index: self.index, |
52 | 135 | expected: Ownership::Ref, |
53 | 136 | received: Ownership::Mut, |
54 | 137 | }), |
55 | 138 | } |
56 | 139 | } |
57 | 140 |
|
58 | | - /// Returns `Ok(&mut T)` if the argument is [`Arg::Mut`]. |
59 | | - pub fn take_mut<T: Reflect>(self, info: &ArgInfo) -> Result<&'a mut T, ArgError> { |
60 | | - match self { |
61 | | - Arg::Owned(_) => Err(ArgError::InvalidOwnership { |
62 | | - id: info.id().clone(), |
| 141 | + /// Returns `Ok(&mut T)` if the argument is [`ArgValue::Mut`]. |
| 142 | + /// |
| 143 | + /// If the argument is not a mutable reference, returns an error. |
| 144 | + /// |
| 145 | + /// It's generally preferred to use [`Self::take`] instead of this method. |
| 146 | + /// |
| 147 | + /// # Example |
| 148 | + /// |
| 149 | + /// ``` |
| 150 | + /// # use bevy_reflect::func::ArgList; |
| 151 | + /// let mut value = 123u32; |
| 152 | + /// let mut args = ArgList::new().push_mut(&mut value); |
| 153 | + /// let value = args.take_mut::<u32>().unwrap(); |
| 154 | + /// assert_eq!(*value, 123); |
| 155 | + /// ``` |
| 156 | + pub fn take_mut<T: Reflect + TypePath>(self) -> Result<&'a mut T, ArgError> { |
| 157 | + match self.value { |
| 158 | + ArgValue::Owned(_) => Err(ArgError::InvalidOwnership { |
| 159 | + index: self.index, |
63 | 160 | expected: Ownership::Mut, |
64 | 161 | received: Ownership::Owned, |
65 | 162 | }), |
66 | | - Arg::Ref(_) => Err(ArgError::InvalidOwnership { |
67 | | - id: info.id().clone(), |
| 163 | + ArgValue::Ref(_) => Err(ArgError::InvalidOwnership { |
| 164 | + index: self.index, |
68 | 165 | expected: Ownership::Mut, |
69 | 166 | received: Ownership::Ref, |
70 | 167 | }), |
71 | | - Arg::Mut(arg) => { |
72 | | - let received = ::std::borrow::Cow::Owned(arg.reflect_type_path().to_string()); |
| 168 | + ArgValue::Mut(arg) => { |
| 169 | + let received = std::borrow::Cow::Owned(arg.reflect_type_path().to_string()); |
73 | 170 | Ok(arg.downcast_mut().ok_or_else(|| ArgError::UnexpectedType { |
74 | | - id: info.id().clone(), |
75 | | - expected: ::std::borrow::Cow::Borrowed(info.type_path()), |
| 171 | + index: self.index, |
| 172 | + expected: std::borrow::Cow::Borrowed(T::type_path()), |
76 | 173 | received, |
77 | 174 | })?) |
78 | 175 | } |
79 | 176 | } |
80 | 177 | } |
81 | 178 | } |
| 179 | + |
| 180 | +/// Represents an argument that can be passed to a [`DynamicFunction`]. |
| 181 | +/// |
| 182 | +/// [`DynamicFunction`]: crate::func::DynamicFunction |
| 183 | +#[derive(Debug)] |
| 184 | +pub enum ArgValue<'a> { |
| 185 | + Owned(Box<dyn Reflect>), |
| 186 | + Ref(&'a dyn Reflect), |
| 187 | + Mut(&'a mut dyn Reflect), |
| 188 | +} |
| 189 | + |
| 190 | +impl<'a> Deref for ArgValue<'a> { |
| 191 | + type Target = dyn Reflect; |
| 192 | + |
| 193 | + fn deref(&self) -> &Self::Target { |
| 194 | + match self { |
| 195 | + ArgValue::Owned(arg) => arg.as_ref(), |
| 196 | + ArgValue::Ref(arg) => *arg, |
| 197 | + ArgValue::Mut(arg) => *arg, |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments