From 0bb1c5beac8ccf0fbfb497653560a4d5962126b0 Mon Sep 17 00:00:00 2001 From: Caio <c410.f3r@gmail.com> Date: Tue, 19 May 2020 12:46:56 -0300 Subject: [PATCH] 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 --- juniper/src/types/scalars.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/juniper/src/types/scalars.rs b/juniper/src/types/scalars.rs index 1e9c29b5..a9443df4 100644 --- a/juniper/src/types/scalars.rs +++ b/juniper/src/types/scalars.rs @@ -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(); + } }