Expected literal

macro_rules! i {
  (...omitted...) => {
    struct A {
      #[serde(bound(serialize = concat!($ftype, ": std::fmt::Display")))]
      $fname: $ftype,
    }
  };
}

I can't use concat here, any other ways?

What's the matcher type of $ftype? I'm going to suspect that stringify!() could help

No, it only accept serialize = "".

I've try serialize = concat!(stringfy!($ftype), ": std::fmt::Display")

https://github.com/serde-rs/serde/issues/1636

Any workaround?

The nuclear workaround is to use a procedural macro to perform the concat manually and expand to a call to serde where serialize is fed this manually forged literal.

A sensible alternative, with a bit of XY problem, is to have the A struct always include the Display bounds that your serialize requires:

struct A
where
    $ftype : ::core::fmt::Display,
{
    $fname: $ftype,
}

I haven't tested it but I would expect serde to auto-include such bounds in its generated impls

1 Like

What if

#[cfg_attr(
  feature = "std",
  serde(bound(serialize = concat!($ftype, ": std::fmt::Display")))
)]

How to apply this to where condiction?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.