Relax Default impl constraint (#664)

* Relax Default impl constraint

For EmptyMutation and EmptySubscription. The built-in derive
expects `T: Default`, which is not necessary for a PhantomData
wrapper.

* Add test
This commit is contained in:
Caio 2020-05-19 12:46:56 -03:00 committed by GitHub
parent 5021ae80e1
commit 0bb1c5beac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -324,7 +324,7 @@ where
///
/// If you instantiate `RootNode` with this as the mutation, no mutation will be
/// generated for the schema.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct EmptyMutation<T> {
phantom: PhantomData<T>,
}
@ -370,11 +370,18 @@ where
{
}
impl<T> Default for EmptyMutation<T> {
fn default() -> Self {
Self {
phantom: PhantomData,
}
}
}
/// Utillity type to define read-only schemas
///
/// If you instantiate `RootNode` with this as the subscription,
/// no subscriptions will be generated for the schema.
#[derive(Default)]
pub struct EmptySubscription<T> {
phantom: PhantomData<T>,
}
@ -420,6 +427,14 @@ where
{
}
impl<T> Default for EmptySubscription<T> {
fn default() -> Self {
Self {
phantom: PhantomData,
}
}
}
#[cfg(test)]
mod tests {
use super::{EmptyMutation, EmptySubscription, ID};
@ -481,4 +496,11 @@ mod tests {
fn check_if_send<T: Send>() {}
check_if_send::<EmptySubscription<()>>();
}
#[test]
fn default_is_invariant_over_type() {
struct Bar;
let _ = EmptySubscription::<Bar>::default();
let _ = EmptyMutation::<Bar>::default();
}
}