Request Details
I want to be able to define a SpacetimeType, impl From<> for a shared type, then have that impl optionally added to the codegen for use on client. The best example I can think of for this is Vec3 and Vec2 shapes, so maybe something like this:
Define your type and the impl blocks, then add #[spacetime_export] to them. This is also helpful if you want to write common functionality in the impl block and share between server + client instead of writing a separate common crate.
#[derive(SpacetimeType)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[spacetime_export]
impl From<Vec3> for glam::Vec3 {
fn from(v: Vec3) -> Self {
glam::Vec3::new(v.x, v.y, v.z)
}
}
#[spacetime_export]
impl From<glam::Vec3> for Vec3 {
fn from(v: glam::Vec3) -> Self {
Self { x: v.x, y: v.y, z: v.z }
}
}
Requested by @onx2 via the SpacetimeDB site.
Activity
View on GitHub#5196Traced the type all the way down —
crates/schema/src/def.rsTypeDef(line 1987) is constructed as just{ accessor_name: ScopedTypeName, ty: AlgebraicTypeRef, custom_ordering: bool }
— pure schema metadata, no code/tokens/impl body anywhere in it. TheModuleDefbuilt from these is what every codegen backend
(crates/codegen/src/{rust,typescript,csharp,cpp}.rs) operates on, so
there's currently no path for an arbitraryimplblock's function body
to survive from your Rust source into any generated client language.A
#[spacetime_export]that "just works" across all client languages
would effectively require transpiling Rust expressions into 4+ target
languages, which doesn't exist today. A realistic first version seems
like it'd need to scope this to Rust-only client codegen (rust.rs),
where the impl block's tokens could be re-emitted verbatim — no
translation needed. C#/TS/C++/Unreal would need a different mechanism
(or be explicitly out of scope for v1).Is that the direction you had in mind, or is there a different design
already in mind for this?
