1

I'm trying to use neon to produce bindings to a rust library. I am using serde to handle the data, but it only has a macro for arrays defined up to length 32.

That macro code is:

macro_rules! array_impls {
    ($($len:tt)+) => {
        $(
            impl<T> Serialize for [T; $len]
            where
                T: Serialize,
            {
                #[inline]
                fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
                where
                    S: Serializer,
                {
                    let mut seq = try!(serializer.serialize_tuple($len));
                    for e in self {
                        try!(seq.serialize_element(e));
                    }
                    seq.end()
                }
            }
        )+
    }
}

array_impls! {
    01 02 03 04 05 06 07 08 09 10
    11 12 13 14 15 16 17 18 19 20
    21 22 23 24 25 26 27 28 29 30
    31 32
}

I tried at first to paste the macro in and call array_impls! { 1024 }, but rust won't allow modification of types from outside this crate, which the generic in the macro would potentially do.

My best guess as to a implementation is:

impl Serialize for [PolyCoeff; N] {
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_tuple(N)?;
        for e in self.iter_mut().enumerate() {
            seq.serialize_element(e)?;
        }
        seq.end()
    }
}

There are a couple of different errors. The main one is "this is not defined in the current crate because arrays are always foreign".

I found a github issue for the array size limit. They suggested the following as a workaround:

struct S {
    #[serde(serialize_with = "<[_]>::serialize")]
    arr: [u8; 256],
}

I haven't been able to get it to compile yet.

dysbulic
  • 3,005
  • 2
  • 28
  • 48
  • "Edit: I figured out the issue. There is a macro that defines a type pattern for serializing arrays, but it is only instantiated up to 32." yes this is what I said... I think I will never answer you again. BTW don't answer in question, I rollback. Add your own answer – Stargateur May 25 '20 at 08:01
  • I don't have the solution yet. I have the macro that is used to generate the 32 existing array serializers, but it doesn't work if I paste it in my code. If I had a solution, I would have posted it. The code you removed is simply my best hope for a solution and I wanted others to have it to work from. – dysbulic May 25 '20 at 08:08
  • I again answer you with my answer about your "best hope", see the last link of my answer. Now you change so much your question that my answer don't make sense. It's not allowed in SO. Also, again create a [mcve] – Stargateur May 25 '20 at 15:15
  • I tried to continue my original post with updated information and you deleted half of it. I rewrote it for brevity's sake. The question remained the same, just the data that I had about the solution evolved. – dysbulic May 26 '20 at 00:03

2 Answers2

1

This is an issue that's been encountered in serde before, and there's a crate dealing with it. My final code looked something like:

use serde::{Serialize, Deserialize};
use serde_big_array::big_array;

big_array! { BigArray; N }

#[derive(Copy, Clone, Serialize, Deserialize)]
pub struct Poly {
    #[serde(with = "BigArray")]
    coeffs: [PolyCoeff; N],
}
dysbulic
  • 3,005
  • 2
  • 28
  • 48
-1

Serde implement its trait for a limited number of array size, this is due to compiler limitation about generic size array. You will found this limitation in a lot of crate. You didn't include an example of data you are using so I can't advice you a lot more, you could prefer a Vec or try:

#[derive(Serialize, Deserialize)]
pub struct Poly<'a> {
    coeffs: &'a [u8],
}

See:

Stargateur
  • 24,473
  • 8
  • 65
  • 91