|
| 1 | +//! The [`ReflectConvert`] type, which allows types to register conversions to |
| 2 | +//! and from one another. |
| 3 | +
|
| 4 | +use alloc::boxed::Box; |
| 5 | +use core::any::TypeId; |
| 6 | + |
| 7 | +use bevy_utils::TypeIdMap; |
| 8 | + |
| 9 | +use crate::{Reflect, TypePath}; |
| 10 | + |
| 11 | +/// Provides a mechanism for converting values of one type to another. |
| 12 | +/// |
| 13 | +/// This [`crate::type_registry::TypeData`] is associated with the type to be |
| 14 | +/// converted *into*, not the type to be converted *from*. To convert a value, |
| 15 | +/// use code like the following: |
| 16 | +/// |
| 17 | +/// ```rust |
| 18 | +/// # let mut registry = TypeRegistry::default(); |
| 19 | +/// # registry.add_registration(i32::get_type_registration()); |
| 20 | +/// # registry.add_registration(String::get_type_registration()); |
| 21 | +/// # registry.register_type_conversion(|x: i32| Ok(x.to_string())); |
| 22 | +/// |
| 23 | +/// let reflect_convert = registry |
| 24 | +/// .get_type_data::<ReflectConvert>(TypeId::of::<String>()) |
| 25 | +/// .unwrap(); |
| 26 | +/// let converted: String = reflect_convert |
| 27 | +/// .try_convert_from(Box::new(12345i32)) |
| 28 | +/// .unwrap() |
| 29 | +/// .downcast::<String>() |
| 30 | +/// .unwrap(); |
| 31 | +/// ``` |
| 32 | +#[derive(Default)] |
| 33 | +pub struct ReflectConvert { |
| 34 | + /// A mapping from the type to be converted *from* to its associated |
| 35 | + /// [`Converter`]. |
| 36 | + conversions: TypeIdMap<Box<dyn Converter>>, |
| 37 | +} |
| 38 | + |
| 39 | +/// An internal trait that wraps a conversion function in an untyped interface. |
| 40 | +trait Converter: Send + Sync { |
| 41 | + /// Converts the value to the appropriate type. |
| 42 | + /// |
| 43 | + /// This returns the converted value if the conversion succeeds or the |
| 44 | + /// original value if the conversion fails. |
| 45 | + fn convert(&self, input: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>; |
| 46 | + |
| 47 | + /// Returns a new boxed instance wrapping the same [`Converter`]. |
| 48 | + fn clone_converter(&self) -> Box<dyn Converter>; |
| 49 | +} |
| 50 | + |
| 51 | +/// A wrapper that contains a conversion function and implements [`Converter`]. |
| 52 | +struct TypedConverter<T, U>(fn(T) -> Result<U, T>) |
| 53 | +where |
| 54 | + T: Reflect + TypePath, |
| 55 | + U: Reflect + TypePath; |
| 56 | + |
| 57 | +impl ReflectConvert { |
| 58 | + /// Creates a new, empty [`ReflectConvert`] with no conversions registered. |
| 59 | + pub fn new() -> ReflectConvert { |
| 60 | + Self::default() |
| 61 | + } |
| 62 | + |
| 63 | + /// Attempts to construct an instance of this type from the provided |
| 64 | + /// `input`. |
| 65 | + /// |
| 66 | + /// If the conversion fails, either because no conversion has been |
| 67 | + /// registered from the type of `input` or because the conversion function |
| 68 | + /// itself returned `Err`, the `input` value is returned as an error. |
| 69 | + pub fn try_convert_from( |
| 70 | + &self, |
| 71 | + input: Box<dyn Reflect>, |
| 72 | + ) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> { |
| 73 | + let type_id = (*input.as_any()).type_id(); |
| 74 | + match self.conversions.get(&type_id) { |
| 75 | + Some(converter) => converter.convert(input), |
| 76 | + None => Err(input), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Adds a conversion function from the type `T` to this type. |
| 81 | + /// |
| 82 | + /// If the conversion succeeds, the function should return the converted |
| 83 | + /// value. If the conversion fails, the function should return the original |
| 84 | + /// input value. |
| 85 | + pub fn register_type_conversion<T, U>(&mut self, function: fn(T) -> Result<U, T>) |
| 86 | + where |
| 87 | + T: Reflect + TypePath, |
| 88 | + U: Reflect + TypePath, |
| 89 | + { |
| 90 | + self.conversions |
| 91 | + .insert(TypeId::of::<T>(), Box::new(TypedConverter(function))); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Clone for ReflectConvert { |
| 96 | + fn clone(&self) -> Self { |
| 97 | + ReflectConvert { |
| 98 | + conversions: self |
| 99 | + .conversions |
| 100 | + .iter() |
| 101 | + .map(|(type_id, converter)| (*type_id, converter.clone_converter())) |
| 102 | + .collect(), |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<T, U> Clone for TypedConverter<T, U> |
| 108 | +where |
| 109 | + T: Reflect + TypePath, |
| 110 | + U: Reflect + TypePath, |
| 111 | +{ |
| 112 | + fn clone(&self) -> Self { |
| 113 | + TypedConverter(self.0) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<T, U> Converter for TypedConverter<T, U> |
| 118 | +where |
| 119 | + T: Reflect + TypePath, |
| 120 | + U: Reflect + TypePath, |
| 121 | +{ |
| 122 | + fn convert(&self, input: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> { |
| 123 | + match (self.0)(input.take()?) { |
| 124 | + Ok(value) => Ok(Box::new(value)), |
| 125 | + Err(value) => Err(Box::new(value)), |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fn clone_converter(&self) -> Box<dyn Converter> { |
| 130 | + Box::new(self.clone()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#[cfg(test)] |
| 135 | +mod tests { |
| 136 | + use alloc::{ |
| 137 | + borrow::ToOwned as _, |
| 138 | + boxed::Box, |
| 139 | + string::{String, ToString}, |
| 140 | + }; |
| 141 | + use core::any::TypeId; |
| 142 | + |
| 143 | + use crate::{convert::ReflectConvert, type_registry::GetTypeRegistration, TypeRegistry}; |
| 144 | + |
| 145 | + /// Tests that `i32` can be converted to `String` if the appropriate |
| 146 | + /// conversion is registered. |
| 147 | + #[test] |
| 148 | + fn convert_from_i32_to_string() { |
| 149 | + // Register the types and the conversion. |
| 150 | + let mut registry = TypeRegistry::default(); |
| 151 | + registry.add_registration(i32::get_type_registration()); |
| 152 | + registry.add_registration(String::get_type_registration()); |
| 153 | + registry.register_type_conversion(|x: i32| Ok(x.to_string())); |
| 154 | + |
| 155 | + let reflect_convert = registry |
| 156 | + .get_type_data::<ReflectConvert>(TypeId::of::<String>()) |
| 157 | + .unwrap(); |
| 158 | + |
| 159 | + // Test that a successful conversion works. |
| 160 | + let converted = reflect_convert |
| 161 | + .try_convert_from(Box::new(12345i32)) |
| 162 | + .unwrap() |
| 163 | + .downcast::<String>() |
| 164 | + .unwrap(); |
| 165 | + assert_eq!(&**converted, "12345"); |
| 166 | + } |
| 167 | + |
| 168 | + /// Tests that `String` can be fallibly converted to `i32` if the |
| 169 | + /// appropriate conversion is registered. |
| 170 | + /// |
| 171 | + /// This also tests that the behavior of returning the original string on |
| 172 | + /// error is correct. |
| 173 | + #[test] |
| 174 | + fn convert_from_string_to_i32() { |
| 175 | + // Register the types and the conversion. |
| 176 | + let mut registry = TypeRegistry::default(); |
| 177 | + registry.add_registration(i32::get_type_registration()); |
| 178 | + registry.add_registration(String::get_type_registration()); |
| 179 | + registry.register_type_conversion(|x: String| match x.parse::<i32>() { |
| 180 | + Ok(value) => Ok(value), |
| 181 | + Err(_) => Err(x), |
| 182 | + }); |
| 183 | + |
| 184 | + let reflect_convert = registry |
| 185 | + .get_type_data::<ReflectConvert>(TypeId::of::<i32>()) |
| 186 | + .unwrap(); |
| 187 | + |
| 188 | + // Test a successful conversion from string to integer. |
| 189 | + let converted = reflect_convert |
| 190 | + .try_convert_from(Box::new("12345".to_owned())) |
| 191 | + .unwrap() |
| 192 | + .downcast::<i32>() |
| 193 | + .unwrap(); |
| 194 | + assert_eq!(*converted, 12345); |
| 195 | + |
| 196 | + // Test an unsuccessful conversion from string to integer. |
| 197 | + let error = reflect_convert |
| 198 | + .try_convert_from(Box::new("qqqqq".to_owned())) |
| 199 | + .unwrap_err() |
| 200 | + .downcast::<String>() |
| 201 | + .unwrap(); |
| 202 | + assert_eq!(&**error, "qqqqq"); |
| 203 | + } |
| 204 | + |
| 205 | + /// Tests that the error-handling behavior is correct when attempting a |
| 206 | + /// conversion that hasn't been registered. |
| 207 | + #[test] |
| 208 | + fn no_such_conversion() { |
| 209 | + let mut registry = TypeRegistry::default(); |
| 210 | + registry.add_registration(i32::get_type_registration()); |
| 211 | + registry.add_registration(String::get_type_registration()); |
| 212 | + |
| 213 | + let reflect_convert = registry |
| 214 | + .get_type_data::<ReflectConvert>(TypeId::of::<i32>()) |
| 215 | + .unwrap(); |
| 216 | + |
| 217 | + // Test that we get the original value back on error. |
| 218 | + let error = reflect_convert |
| 219 | + .try_convert_from(Box::new("12345".to_owned())) |
| 220 | + .unwrap_err() |
| 221 | + .downcast::<String>() |
| 222 | + .unwrap(); |
| 223 | + assert_eq!(&**error, "12345"); |
| 224 | + } |
| 225 | +} |
0 commit comments