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

This commit is contained in:
Keavon Chambers
2025-11-18 11:10:33 +00:00
committed by GitHub
parent 57b0b9c7ed
commit 6a3b098681
2 changed files with 38 additions and 8 deletions
+16 -7
View File
@@ -1,8 +1,7 @@
use crate::{
Node,
table::{Table, TableRow},
transform::Footprint,
};
use crate::Node;
use crate::table::{Table, TableRow};
use crate::transform::Footprint;
use glam::DVec2;
use std::future::Future;
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> {
fn convert_row(self) -> U;
}
//
impl<U, T: TableConvert<U> + Send> Convert<Table<U>, ()> for Table<T> {
async fn convert(self, _: Footprint, _: ()) -> Table<U> {
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.
macro_rules! impl_convert {
($from:ty, $to:ty) => {
@@ -100,6 +103,12 @@ macro_rules! impl_convert {
impl_convert!(u128, $to);
impl_convert!(isize, $to);
impl_convert!(usize, $to);
impl Convert<DVec2, ()> for $to {
async fn convert(self, _: Footprint, _: ()) -> DVec2 {
DVec2::splat(self as f64)
}
}
};
}
impl_convert!(f32);