1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
use crate::prelude::*;
use std::any::TypeId;
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
use yarnspinner_macros::all_tuples;
/// A function that can be registered into and called from Yarn.
/// It must have the following properties:
/// - It is allowed to have zero or more parameters
/// - Each parameter must be a [`YarnFnParam`], which means of the following types or a reference to them:
/// - [`bool`]
/// - A numeric type, i.e. one of [`f32`], [`f64`], [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`], [`isize`]
/// - [`String`] (for a reference, [`&str`] may be used instead of `&String`)
/// - [`YarnValue`], which means that a parameter may be any of the above types
/// - Tuples of the above types.
/// - It must return a value.
/// - Its return type must be one of the following types:
/// - [`bool`]
/// - A numeric type, i.e. one of [`f32`], [`f64`], [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], [`usize`], [`isize`]
/// - [`String`]
/// Note that in particular, no references can be returned.
/// ## Examples
/// ```rust
/// fn give_summary(name: &str, age: usize, is_cool: bool) -> String {
/// format!("{name} is {age} years old and is {} cool", if is_cool { "very" } else { "not" })
/// }
/// ```
/// Which may be called from Yarn as follows:
/// ```text
/// <<set $name to "Bob">>
/// <<set $age to 42>>
/// <<set $is_cool to true>>
/// Narrator: {give_summary($name, $age, $is_cool)}
/// ```
pub trait YarnFn<Marker>: Clone + Send + Sync {
/// The type of the value returned by this function. See [`YarnFn`] for more information about what is allowed.
type Out: IntoYarnValueFromNonYarnValue + 'static;
#[doc(hidden)]
fn call(&self, input: Vec<YarnValue>) -> Self::Out;
/// The [`TypeId`]s of the parameters of this function.
fn parameter_types(&self) -> Vec<TypeId>;
/// The [`TypeId`] of the return type of this function.
fn return_type(&self) -> TypeId {
TypeId::of::<Self::Out>()
}
}
/// A [`YarnFn`] with the `Marker` type parameter erased.
/// See its documentation for more information about what kind of functions are allowed.
pub trait UntypedYarnFn: Debug + Display + Send + Sync {
#[doc(hidden)]
fn call(&self, input: Vec<YarnValue>) -> YarnValue;
#[doc(hidden)]
fn clone_box(&self) -> Box<dyn UntypedYarnFn>;
/// The [`TypeId`]s of the parameters of this function.
fn parameter_types(&self) -> Vec<TypeId>;
/// The [`TypeId`] of the return type of this function.
fn return_type(&self) -> TypeId;
}
impl Clone for Box<dyn UntypedYarnFn> {
fn clone(&self) -> Self {
self.clone_box()
}
}
impl<Marker, F> UntypedYarnFn for YarnFnWrapper<Marker, F>
where
Marker: 'static,
F: YarnFn<Marker> + 'static + Clone,
F::Out: IntoYarnValueFromNonYarnValue + 'static + Clone,
{
fn call(&self, input: Vec<YarnValue>) -> YarnValue {
let output = self.function.call(input);
output.into_yarn_value()
}
fn clone_box(&self) -> Box<dyn UntypedYarnFn> {
Box::new(self.clone())
}
fn parameter_types(&self) -> Vec<TypeId> {
self.function.parameter_types()
}
fn return_type(&self) -> TypeId {
self.function.return_type()
}
}
pub(crate) struct YarnFnWrapper<Marker, F>
where
F: YarnFn<Marker>,
{
function: F,
// NOTE: PhantomData<fn()-> T> gives this safe Send/Sync impls
_marker: PhantomData<fn() -> Marker>,
}
impl<Marker, F> Clone for YarnFnWrapper<Marker, F>
where
F: YarnFn<Marker>,
{
fn clone(&self) -> Self {
Self {
function: self.function.clone(),
_marker: PhantomData,
}
}
}
impl<Marker, F> From<F> for YarnFnWrapper<Marker, F>
where
F: YarnFn<Marker>,
{
fn from(function: F) -> Self {
Self {
function,
_marker: PhantomData,
}
}
}
impl<Marker, F> Debug for YarnFnWrapper<Marker, F>
where
F: YarnFn<Marker>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let signature = std::any::type_name::<Marker>();
let function_path = std::any::type_name::<F>();
let debug_message = format!("{signature} {{{function_path}}}");
f.debug_struct(&debug_message).finish()
}
}
impl<Marker, F> Display for YarnFnWrapper<Marker, F>
where
F: YarnFn<Marker>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let signature = std::any::type_name::<Marker>();
f.write_str(signature)
}
}
impl PartialEq for Box<dyn UntypedYarnFn> {
fn eq(&self, other: &Self) -> bool {
// Not guaranteed to be unique, but it's good enough for our purposes.
let debug = format!("{:?}", self);
let other_debug = format!("{:?}", other);
debug == other_debug
}
}
impl Eq for Box<dyn UntypedYarnFn> {}
/// A macro for using [`YarnFn`] as a return type or parameter type without needing
/// to know the implementation details of the [`YarnFn`] trait.
///
/// This is useful when registering functions in a [`Library`] with [`Library::add_function`].
#[macro_export]
macro_rules! yarn_fn_type {
(impl Fn($($param:ty),+) -> $ret:ty) => {
impl $crate::prelude::YarnFn<fn($($param),+) -> $ret, Out = $ret>
};
}
pub use yarn_fn_type;
/// Adapted from <https://github.com/bevyengine/bevy/blob/fe852fd0adbce6856f5886d66d20d62cfc936287/crates/bevy_ecs/src/system/system_param.rs#L1370>
macro_rules! impl_yarn_fn_tuple {
($($param: ident),*) => {
#[allow(non_snake_case)]
impl<F, O, $($param,)*> YarnFn<fn($($param,)*) -> O> for F
where
for<'a> F:
Send + Sync + Clone +
Fn($($param,)*) -> O +
Fn($(<$param as YarnFnParam>::Item<'a>,)*) -> O,
O: IntoYarnValueFromNonYarnValue + 'static,
$($param: YarnFnParam + 'static,)*
{
type Out = O;
#[allow(non_snake_case)]
fn call(&self, input: Vec<YarnValue>) -> Self::Out {
let mut params: Vec<_> = input.into_iter().map(YarnValueWrapper::from).collect();
#[allow(unused_variables, unused_mut)] // for n = 0 tuples
let mut iter = params.iter_mut();
// $param is the type implementing YarnFnParam
let input = (
$($param::retrieve(&mut iter),)*
);
assert!(iter.next().is_none(), "Passed too many arguments to YarnFn");
let ($($param,)*) = input;
self($($param,)*)
}
fn parameter_types(&self) -> Vec<TypeId> {
vec![$(TypeId::of::<$param>()),*]
}
}
};
}
all_tuples!(impl_yarn_fn_tuple, 0, 16, P);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_no_params() {
fn f() -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_string() {
fn f(_: String) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_string_ref() {
fn f(_: &String) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_string_slice() {
fn f(_: &str) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_usize() {
fn f(_: usize) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_usize_ref() {
fn f(_: &usize) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_yarn_value() {
fn f(_: YarnValue) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_yarn_value_ref() {
fn f(_: &YarnValue) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_multiple_strings() {
fn f(s: String, _: String, _: &str, _: String, _: &str) -> String {
s
}
accept_yarn_fn(f);
}
#[test]
fn accepts_lots_of_different_types() {
#[allow(clippy::too_many_arguments)]
fn f(
_: String,
_: usize,
_: &str,
_: &YarnValue,
_: &bool,
_: isize,
_: String,
_: &u32,
) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn accepts_tuples() {
#[allow(clippy::too_many_arguments)]
fn f(
_: (String, usize),
_: usize,
_: (&str, (&str, &String)),
_: &YarnValue,
_: (&bool, bool, bool, (&str, String)),
_: isize,
_: String,
_: &u32,
) -> bool {
true
}
accept_yarn_fn(f);
}
#[test]
fn unpacks_tuples_in_right_order() {
#[allow(clippy::too_many_arguments)]
fn f(a: usize, (b, c): (usize, usize), d: usize, (e, f, g): (usize, usize, usize)) -> bool {
a == 1 && b == 2 && c == 3 && d == 4 && e == 5 && f == 6 && g == 7
}
let input: Vec<_> = (1..=7).map(YarnValue::from).collect();
let result = apply_yarn_fn(f, input);
assert!(result);
}
#[test]
fn accepts_function_with_single_tuple_param() {
fn f(_: (usize, isize, (String, &str))) -> bool {
true
}
accept_yarn_fn(f);
}
fn accept_yarn_fn<Marker>(_: impl YarnFn<Marker>) {}
fn apply_yarn_fn<T, Marker>(f: T, input: Vec<YarnValue>) -> T::Out
where
T: YarnFn<Marker>,
{
f.call(input)
}
}