Add automatic type conversion from number to Vec2 by splatting (#3394)

This commit is contained in:
Keavon Chambers 2025-11-18 03:10:33 -08:00 committed by GitHub
parent 57b0b9c7ed
commit 6a3b098681
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 8 deletions

View File

@ -57,6 +57,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
// into_node!(from: Table<Raster<CPU>>, to: Table<Raster<SRGBA8>>), // into_node!(from: Table<Raster<CPU>>, to: Table<Raster<SRGBA8>>),
#[cfg(feature = "gpu")] #[cfg(feature = "gpu")]
into_node!(from: &WasmEditorApi, to: &WgpuExecutor), into_node!(from: &WasmEditorApi, to: &WgpuExecutor),
convert_node!(from: DVec2, to: DVec2),
convert_node!(from: String, to: String), convert_node!(from: String, to: String),
convert_node!(from: bool, to: String), convert_node!(from: bool, to: String),
convert_node!(from: DVec2, to: String), convert_node!(from: DVec2, to: String),
@ -275,6 +276,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
convert_node!(from: u128, to: numbers), convert_node!(from: u128, to: numbers),
convert_node!(from: isize, to: numbers), convert_node!(from: isize, to: numbers),
convert_node!(from: usize, to: numbers), convert_node!(from: usize, to: numbers),
convert_node!(from: numbers, to: DVec2),
convert_node!(from: numbers, to: String),
] ]
.into_iter() .into_iter()
.flatten(), .flatten(),
@ -386,7 +389,25 @@ mod node_registry_macros {
convert_node!(from: $from, to: u128), convert_node!(from: $from, to: u128),
convert_node!(from: $from, to: isize), convert_node!(from: $from, to: isize),
convert_node!(from: $from, to: usize), convert_node!(from: $from, to: usize),
convert_node!(from: $from, to: String), ];
x
}};
(from: numbers, to: $to:ty) => {{
let x: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
convert_node!(from: f32, to: $to),
convert_node!(from: f64, to: $to),
convert_node!(from: i8, to: $to),
convert_node!(from: u8, to: $to),
convert_node!(from: u16, to: $to),
convert_node!(from: i16, to: $to),
convert_node!(from: i32, to: $to),
convert_node!(from: u32, to: $to),
convert_node!(from: i64, to: $to),
convert_node!(from: u64, to: $to),
convert_node!(from: i128, to: $to),
convert_node!(from: u128, to: $to),
convert_node!(from: isize, to: $to),
convert_node!(from: usize, to: $to),
]; ];
x x
}}; }};

View File

@ -1,8 +1,7 @@
use crate::{ use crate::Node;
Node, use crate::table::{Table, TableRow};
table::{Table, TableRow}, use crate::transform::Footprint;
transform::Footprint, use glam::DVec2;
};
use std::future::Future; use std::future::Future;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -55,12 +54,10 @@ impl<T: ToString + Send> Convert<String, ()> for T {
} }
} }
// trait mentioning inner type in args
pub trait TableConvert<U> { pub trait TableConvert<U> {
fn convert_row(self) -> U; fn convert_row(self) -> U;
} }
//
impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> { impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> {
async fn convert(self, _: Footprint, _: ()) -> Table<U> { async fn convert(self, _: Footprint, _: ()) -> Table<U> {
let table: Table<U> = self let table: Table<U> = self
@ -76,6 +73,12 @@ impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> {
} }
} }
impl Convert<DVec2, ()> for DVec2 {
async fn convert(self, _: Footprint, _: ()) -> DVec2 {
self
}
}
/// Implements the [`Convert`] trait for conversion between the cartesian product of Rust's primitive numeric types. /// Implements the [`Convert`] trait for conversion between the cartesian product of Rust's primitive numeric types.
macro_rules! impl_convert { macro_rules! impl_convert {
($from:ty, $to:ty) => { ($from:ty, $to:ty) => {
@ -100,6 +103,12 @@ macro_rules! impl_convert {
impl_convert!(u128, $to); impl_convert!(u128, $to);
impl_convert!(isize, $to); impl_convert!(isize, $to);
impl_convert!(usize, $to); impl_convert!(usize, $to);
impl Convert<DVec2, ()> for $to {
async fn convert(self, _: Footprint, _: ()) -> DVec2 {
DVec2::splat(self as f64)
}
}
}; };
} }
impl_convert!(f32); impl_convert!(f32);