Skip to content

Commit a390f6a

Browse files
committed
Add vslice![...] macro
1 parent 08fcd27 commit a390f6a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

godot-core/src/builtin/collections/array.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,8 @@ macro_rules! array {
15791579
/// To create a typed `Array` with a single element type, see the [`array!`] macro.
15801580
///
15811581
/// For dictionaries, a similar macro [`dict!`] exists.
1582+
///
1583+
/// To construct slices of variants, use [`vslice!`].
15821584
#[macro_export]
15831585
macro_rules! varray {
15841586
// Note: use to_variant() and not Variant::from(), as that works with both references and values
@@ -1594,6 +1596,49 @@ macro_rules! varray {
15941596
};
15951597
}
15961598

1599+
/// Constructs a slice of [`Variant`] literals, useful for passing to vararg functions.
1600+
///
1601+
/// Many APIs in Godot have variable-length arguments. GDScript can call such functions by simply passing more arguments, but in Rust,
1602+
/// the parameter type `&[Variant]` is used.
1603+
///
1604+
/// This macro creates a [slice](https://doc.rust-lang.org/std/primitive.slice.html) of `Variant` values.
1605+
///
1606+
/// # Examples
1607+
/// Variable number of arguments:
1608+
/// ```no_run
1609+
/// # use godot::prelude::*;
1610+
/// let slice: &[Variant] = vslice![42, "hello", true];
1611+
///
1612+
/// let concat: GString = godot::global::str(slice);
1613+
/// ```
1614+
/// _(In practice, you might want to use [`godot_str!`][crate::global::godot_str] instead of `str()`.)_
1615+
///
1616+
/// Dynamic function call via reflection. NIL can still be passed inside `vslice!`, just use `Variant::nil()`.
1617+
/// ```no_run
1618+
/// # use godot::prelude::*;
1619+
/// # fn some_object() -> Gd<Object> { unimplemented!() }
1620+
/// let mut obj: Gd<Object> = some_object();
1621+
/// obj.call("some_method", vslice![Vector2i::new(1, 2), Variant::nil()]);
1622+
/// ```
1623+
///
1624+
/// # See also
1625+
/// To create typed and untyped `Array`s, use the [`array!`] and [`varray!`] macros respectively.
1626+
///
1627+
/// For dictionaries, a similar macro [`dict!`] exists.
1628+
#[macro_export]
1629+
macro_rules! vslice {
1630+
// Note: use to_variant() and not Variant::from(), as that works with both references and values
1631+
($($elements:expr),* $(,)?) => {
1632+
{
1633+
use $crate::meta::ToGodot as _;
1634+
let mut array = $crate::builtin::VariantArray::default();
1635+
&[
1636+
$( $elements.to_variant(), )*
1637+
]
1638+
}
1639+
};
1640+
}
1641+
15971642
// ----------------------------------------------------------------------------------------------------------------------------------------------
15981643

15991644
#[cfg(feature = "serde")]

godot-core/src/builtin/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub mod __prelude_reexport {
4848
pub use vectors::*;
4949

5050
pub use super::{EulerOrder, Side, VariantOperator, VariantType};
51-
pub use crate::{array, dict, real, reals, varray};
51+
pub use crate::{array, dict, real, reals, varray, vslice};
5252
}
5353

5454
pub use __prelude_reexport::*;

0 commit comments

Comments
 (0)