-
Notifications
You must be signed in to change notification settings - Fork 2
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
Vec-of-array #2
Comments
That is not true as you can see here. You can arbitrarily nest types here. Both array-in-vec and array-in-array are supported, but also array-in-hashmap, etc. The crate is implemented without any support from serde. |
@jonasbb I think you missed the key word there: arbitrary levels of nesting. Consider for example a By contrast, take a The best the rest of us can do -- whether that's |
Yes, I mean arbitrary levels of nesting. As long as the type shape within the #[serde_with::serde_as]
#[derive(serde::Serialize)]
struct Wrapper<T: Serialize, const M: usize, const N: usize, const O: usize, const P: usize>(
#[serde_as(as = "[[[[_; M]; N]; O]; P]")] [[[[T; M]; N]; O]; P],
); Code examplefn make_array<T: Copy, const M: usize, const N: usize, const O: usize, const P: usize>(
t: T,
) -> [[[[T; M]; N]; O]; P] {
[[[[t; M]; N]; O]; P]
}
fn serialize_nested_arrays<
T: Serialize + Copy,
const M: usize,
const N: usize,
const O: usize,
const P: usize,
>(
t: T,
) {
let arr: [[[[T; M]; N]; O]; P] = make_array(t);
#[serde_with::serde_as]
#[derive(Serialize)]
struct Wrapper<T: Serialize, const M: usize, const N: usize, const O: usize, const P: usize>(
#[serde_as(as = "[[[[_; M]; N]; O]; P]")] [[[[T; M]; N]; O]; P],
);
let s = serde_json::to_string(&Wrapper(arr)).unwrap();
println!("{}", s.len());
}
// serialize_nested_arrays::<_, 33, 3, 3, 33>(55);
// Prints 30262 You are right that the nested arrays cannot be made
In a way that is true, but at least in the case of |
Thank you for that clarification and correction! I'm going to have to look deeper into how It seems a great crate (that I had no idea existed until after I published this one), though I really don't like the syntax they use, so if I can find a "cleaner" way to approach it I think my crate can still have something to offer. |
I can give you some hints where to look. The The |
First step toward full support for nested arrays, and a small step toward supporting arbitrary nesting. refs #2
Plan to leverage this by refactoring the `nested` module into lib.rs, thereby supporting both "flat" and nested arrays with same simple syntax refs #2
Nested and "flat" arrays are now supported identically in serialization. Deserialization is still to-do. refs #2
Serialization came together quite quickly, and was beautifully magical. Deserialization is proving to be another story altogether. I believe the issue is that I'm trying to get Rust to infer the generic type parameter based on the function output, and more specifically on an associated type of a trait, but Rust's inference either depends on the function input and/or can't peek into traits' associated types. I think I'm finally beginning to see why |
Oh, no, I really needed this for my current toy project :_/
I wanted to serialize/deserialize the login/password attempt "sessions" so that I don't have to start from scratch when re-starting my program, hence The discussion (and work) being done here is a bit over my current Rust skill level, so I'm just asking as a user (kudos for this crate, btw!): will nested Serialization/Deserialization be available anytime soon or |
@brainstorm I had some personal/family issues come up that suffice to say distracted me from even thinking about this. It has crept back onto my radar recently though, and I'm hoping to tackle this soon. The trick isn't so much implementing nested arrays, but doing so in a manner that won't leave me coded into a corner should I want to expand that support further later. |
While
Vec
isSerialize
/Deserialize
, const generic and large arrays are not, meaning thatVec
s containing these can not be handled through Serde. Neither does this library (currently) handle them, since we're unable to apply Serde's traits to these types due to Rust's orphan rule.We should however be able to implement support for
Vec<[T; N]>
, as well as[[T; N]; M]
, which I expect should cover most use cases where these structures are being nested; one example of the former can be seen here.Unfortunately, it appears that without support from Serde itself, supporting arbitrary levels of nesting is not possible.
The text was updated successfully, but these errors were encountered: