Serde: Deriving serialize/deserialize fails when including PhantomData<T: ?Sized>

Created on 27 Dec 2017  路  2Comments  路  Source: serde-rs/serde

I don't really know if there's a solution to this, but when a type parameter is only wrapped in PhantomData, serde_derive still requires it to be Sized even though it's not used.

Example that doesn't compile:

use std::marker::PhantomData;

extern crate serde;
#[macro_use] extern crate serde_derive;

#[derive(Serialize, Deserialize)]
struct Foo<T> where T: ?Sized {
  #[serde(skip_serializing)]
  pub foo: PhantomData<T>
}

Here's the error message:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
 --> src/main.rs:7:21
  |
7 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^ `T` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required because of the requirements on the impl of `serde::Deserialize<'_>` for `std::marker::PhantomData<T>`
  = note: required by `serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
 --> src/main.rs:7:21
  |
7 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^ `T` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: required because of the requirements on the impl of `serde::Deserialize<'_>` for `std::marker::PhantomData<T>`
  = note: required by `serde::de::MapAccess::next_value`

error: aborting due to 2 previous errors
bug

Most helpful comment

Okay, that's got to set a record for fastest time between bug report to actual release, right? 馃槷

All 2 comments

Thanks for reporting this! I released serde 1.0.26 with a fix. Your original code should work.

Okay, that's got to set a record for fastest time between bug report to actual release, right? 馃槷

Was this page helpful?
0 / 5 - 0 ratings