Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,39 +264,41 @@ impl<T> FieldsBuilder<T> {
pub fn finalize(self) -> Vec<Field<MetaForm>> {
self.fields
}

fn push_field(mut self, field: Field) -> Self {
// filter out fields of PhantomData
if !field.ty().is_phantom() {
self.fields.push(field);
}
self
}
}

impl FieldsBuilder<NamedFields> {
/// Add a named field constructed using the builder.
pub fn field<F>(mut self, builder: F) -> Self
pub fn field<F>(self, builder: F) -> Self
where
F: Fn(
FieldBuilder,
)
-> FieldBuilder<field_state::NameAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
let field = builder.finalize();
// filter out fields of PhantomData
if !field.ty().is_phantom() {
self.fields.push(field);
}
self
self.push_field(builder.finalize())
}
}

impl FieldsBuilder<UnnamedFields> {
/// Add an unnamed field constructed using the builder.
pub fn field<F>(mut self, builder: F) -> Self
pub fn field<F>(self, builder: F) -> Self
where
F: Fn(
FieldBuilder,
)
-> FieldBuilder<field_state::NameNotAssigned, field_state::TypeAssigned>,
{
let builder = builder(FieldBuilder::new());
self.fields.push(builder.finalize());
self
self.push_field(builder.finalize())
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ impl<T> TypeInfo for PhantomData<T> {
type Identity = PhantomIdentity;

fn type_info() -> Type {
panic!("PhantomData type instances should be filtered out.")
// Fields of this type should be filtered out and never appear in the type graph.
Type::builder()
.path(Path::prelude("PhantomData"))
.docs(&["PhantomData placeholder, this type should be filtered out"])
.composite(Fields::unit())
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ fn prelude_items() {
}

#[test]
#[should_panic]
fn phantom_data() {
PhantomData::<i32>::type_info();
assert_type!(
PhantomData<i32>,
Type::builder()
.path(Path::prelude("PhantomData"))
.docs(&["PhantomData placeholder, this type should be filtered out"])
.composite(Fields::unit())
)
}

#[test]
Expand Down
24 changes: 15 additions & 9 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ fn phantom_data_field_is_erased() {
let ty = Type::builder()
.path(Path::new("P", "derive"))
.type_params(named_type_params!((T, bool)))
.composite(
Fields::named()
.field(|f| f.ty::<u8>().name("a").type_name("u8"))
.field(|f| {
f.ty::<PhantomData<bool>>()
.name("m")
.type_name("PhantomData<T>")
}),
);
.composite(Fields::named().field(|f| f.ty::<u8>().name("a").type_name("u8")));

assert_type!(P<bool>, ty);
}

#[test]
fn phantom_data_tuple_struct_field_is_erased() {
#[allow(unused)]
#[derive(TypeInfo)]
struct P<T>(u8, PhantomData<T>);

let ty = Type::builder()
.path(Path::new("P", "derive"))
.type_params(named_type_params!((T, bool)))
.composite(Fields::unnamed().field(|f| f.ty::<u8>().type_name("u8")));

assert_type!(P<bool>, ty);
}
Expand Down