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

Serde support #4

Closed
Ameyanagi opened this issue Nov 22, 2023 · 10 comments
Closed

Serde support #4

Ameyanagi opened this issue Nov 22, 2023 · 10 comments

Comments

@Ameyanagi
Copy link

Thank you for providing very easy-to-use crate.
I was wondering if you could add serde support.

I am now working a project creating an analytical package for scientific purposes, and it would be easy to save the data if we have a serde support.

@WalterSmuts
Copy link
Owner

WalterSmuts commented Nov 23, 2023

I assume you only need it for the two custom structs RealDft and DynRealDft. DynRealDft would be trivial AFAICT but not sure how serde handles the type ambiguity:

pub struct RealDft<T, const SIZE: usize>
where
    [T; SIZE / 2 + 1]: Sized,
{
    inner: [Complex<T>; SIZE / 2 + 1],
}

@WalterSmuts
Copy link
Owner

So thinking it through, I don't think you can have a deserialization implementation for a RealDft. You'd have to have a function return an unknown type. Or rather a type only known at runtime.

In Rust arrays have their length as part of the type signature. You can't have a generic function return a type where you don't know the type until runtime.

@Ameyanagi
Copy link
Author

@WalterSmuts

Thank you so much for your consideration.
In this case, do you think it might be better to impl Serialize and Deserialize within my code, so that the type will be known at the compile time?

I think this can be done with "serde from?" macro that serializes from the related struct. But in this case, I need an #5 .

Thank you anyway for creating a wonderful crate, I love it.

@WalterSmuts
Copy link
Owner

I was referring to RealDft. I suspect we can still have serde work for DynRealDft because the type doesn't change with size and stores a runtime variable for the original length.

@WalterSmuts
Copy link
Owner

Should be easily addable once rust-num/num-complex#119 is merged and published.

@Ameyanagi
Copy link
Author

Thank you very much for your work. It really helps my project.

@WalterSmuts
Copy link
Owner

v0.4.1 should have serde support when adding serde non-default feature.

@Ameyanagi
Copy link
Author

@WalterSmuts

Hi, Thank you for the update.
I worked try to integrate in my project and I found that there were no serde feature exposed.
Can you please add serde = ["dep:serde"] to Cargo.toml, so that the feature is exposed?

It works on my project once the above line is added to Cargo.toml.

[features]
const-realfft = []
fallible = []
serde = ["dep:serde"]
default = ["fallible"]

@WalterSmuts
Copy link
Owner

Can you please add serde = ["dep:serde"] to Cargo.toml, so that the feature is exposed?

I think the feature is already exposed:

[walter@cuddles easyfft-serde-user]$ cargo add easyfft --features serde
    Updating crates.io index
      Adding easyfft v0.4.1 to dependencies.
             Features:
             + fallible
             + serde
             - const-realfft

And it seems to work:

use easyfft::prelude::*;

fn main() {
    let a = [1; 10].real_fft();
    println!("{}", serde_json::to_string(&a).unwrap());
}

Produces:

[walter@cuddles easyfft-serde-user]$ cargo run
   Compiling easyfft-serde-user v0.1.0 (/home/walter/Development/easyfft-serde-user)
    Finished dev [unoptimized + debuginfo] target(s) in 0.49s
     Running `target/debug/easyfft-serde-user`
{"original_length":10,"inner":[[10,0],[0,0],[0,0],[0,0],[0,0],[0,0]]}

@Ameyanagi
Copy link
Author

@WalterSmuts

Thank you for your comments.
It seems that there was something in the cache that prevented from working.

I confirmed that it works perfectly good now.
Thank you for you excellent work!!

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