Skip to content

Option to codegen impl blocks #5196

Description

@website-features

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions