Serde compilation error with struct with interior mutability

I have a struct that, as soon as I add interior mutability to it through Rc<RefCell<...>> results in a compilation error:

error[E0277]: the trait bound `Rc<RefCell<Vec<UserDataExportUserItem>>>: websocket_server::_::_serde::Serialize` is not satisfied
    --> src/services/users/user_data_export/models.rs:22:17
     |
22   | #[derive(Debug, Serialize)]
     |                 ^^^^^^^^^ the trait `websocket_server::_::_serde::Serialize` is not implemented for `Rc<RefCell<Vec<UserDataExportUserItem>>>`
...
26   |     items: Rc<RefCell<Vec<UserDataExportUserItem>>>,
     |     ----- required by a bound introduced by this call
     |
     = help: the following other types implement trait `websocket_server::_::_serde::Serialize`:
               bool
               char
               isize
               i8
               i16
               i32
               i64
               i128
             and 2373 others
note: required by a bound in `websocket_server::_::_serde::ser::SerializeStruct::serialize_field`
    --> /home/remi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.197/src/ser/mod.rs:1865:12
     |
1859 |     fn serialize_field<T: ?Sized>(
     |        --------------- required by a bound in this associated function
...
1865 |         T: Serialize;
     |            ^^^^^^^^^ required by this bound in `SerializeStruct::serialize_field`

Some observations:

  • It's weird that the error considers that the serde module lives in websocket_server, which is one of my crate's modules. It's as if Serde's dependency was resolved as a transitive dependency.
  • I have confirmed that this should work, both by checking the PR that made Serde compatible with interior mutability types, and by replicating my code in the playground.

Since it doesn't fail in the playground, it means that the problem is specific to my project.

Does anyone have any ideas on what's going on?

1 Like

What happens if you use the fully qualified path of Serialize? I.e. #[derive(::serde::Serialize)]?

The error remains.

I also tried to simplify the error even further by putting a "no brainer" type (u8) inside of the refcell to see what would happen, and:

error[E0277]: the trait bound `Rc<RefCell<Vec<u8>>>: websocket_server::_::_serde::Serialize` is not satisfied
    --> src/services/users/user_data_export/models.rs:21:17
     |
21   | #[derive(Debug, Serialize)]
     |                 ^^^^^^^^^ the trait `websocket_server::_::_serde::Serialize` is not implemented for `Rc<RefCell<Vec<u8>>>`

That's extremely weird :confused: .

Have you enabled the rc feature? It's enabled on the playground.

4 Likes

Ah, so it's because Rc is feature-gated!

Thx, @jofas !

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.