Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Convert<Option<TypeTagged>> and Convert<Vec<TypeTagged>> #1

Closed
Shute052 opened this issue Apr 10, 2024 · 6 comments
Closed

Add Convert<Option<TypeTagged>> and Convert<Vec<TypeTagged>> #1

Shute052 opened this issue Apr 10, 2024 · 6 comments

Comments

@Shute052
Copy link

Shute052 commented Apr 10, 2024

Would it be possible to add these Converters? I would really appreciate it. They are essential for

@mintlu8
Copy link
Owner

mintlu8 commented Apr 11, 2024

I think these are entirely possible to be created by the user.
I'll see if I can come up with some good abstractions for these types.

@mintlu8
Copy link
Owner

mintlu8 commented Apr 11, 2024

The new ProjectOption and ProjectVec might fit this use case.

https://github.com/mintlu8/bevy_serde_project/releases/tag/0.2.0

@Shute052
Copy link
Author

Shute052 commented Apr 12, 2024

Thank you, but how do they work with TypeTagged? I'm curious! These are failed

#[serde_project("ProjectVec<Vec<TypeTagged<Box<dyn DualAxisProcessor>>>>")]
pub(crate) sequence: Vec<Box<dyn DualAxisProcessor>>,

#[serde_project("ProjectVec<Vec<Box<dyn DualAxisProcessor>>>")]
pub(crate) sequence: Vec<Box<dyn DualAxisProcessor>>,

@mintlu8
Copy link
Owner

mintlu8 commented Apr 12, 2024

The first field should be an exact copy of the Vec's type, the second field (optional) should be the projection of individual fields.

ProjectVec<Vec<Box<dyn DualAxisProcessor>>,  TypeTagged<Box<dyn DualAxisProcessor>>>"

@Shute052
Copy link
Author

Shute052 commented Apr 12, 2024

Yeah, that fixes the issue.

But it seems like I'm running into another problem with recursive dyn Traits. It looks like Serialize, Deserialize, and SerdeProject can't play nice together, and the generated __Ser is out of reach from the outside.

#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize, SerdeProject)]
pub struct DualAxisProcessingPipeline {
    #[serde_project(
        "ProjectVec<Vec<Box<dyn DualAxisProcessor>>, TypeTagged<Box<dyn DualAxisProcessor>>>"
    )]
    pub(crate) sequence: Vec<Box<dyn DualAxisProcessor>>,
}

impl DualAxisProcessor for DualAxisProcessingPipeline {
    /// Computes the result by passing the `input_value` through this pipeline.
    #[must_use]
    #[inline]
    fn process(&self, input_value: Vec2) -> Vec2 {
        self.sequence
            .iter()
            .fold(input_value, |value, next| next.process(value))
    }

    #[doc(hidden)]
    fn typetag_name(&self) -> &'static str {
        "DualAxisProcessingPipeline"
    }

    #[doc(hidden)]
    fn as_ser(&self) -> &dyn bevy::reflect::erased_serde::Serialize {
        self
    }
}

impl IntoTypeTagged<Box<dyn DualAxisProcessor>> for DualAxisProcessingPipeline {
    fn name() -> impl AsRef<str> {
        "DualAxisProcessingPipeline"
    }

    fn into_type_tagged(self) -> Box<dyn DualAxisProcessor> {
        Box::new(self)
    }
}

But no worries, it seems that bevy_serde_project primarily focuses on serializing and deserializing Components and Resources, rather than their internal fields.

If the approach is too tricky, I'm also exploring an alternative solution using serde_tagged

@mintlu8
Copy link
Owner

mintlu8 commented Apr 12, 2024

But it seems like I'm running into another problem with recursive dyn Traits. It looks like Serialize, Deserialize, and SerdeProject can't play nice together, and the generated __Ser is out of reach from the outside.

All Serialize + DeserializeOwned structs are SerdeProject so you should choose one of them. SerdeProject is meant to serialize non-serde structs.

Closing this since the issue is solved.

@mintlu8 mintlu8 closed this as completed Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants