Skip to content

Commit 3e470d4

Browse files
committed
Leverage derive_more::AsRef, derive_more::AsMut and derive_more::Deref
1 parent 4884aec commit 3e470d4

File tree

6 files changed

+19
-51
lines changed

6 files changed

+19
-51
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bson = { version = "2.4", optional = true }
5151
chrono = { version = "0.4.30", features = ["alloc"], default-features = false, optional = true }
5252
chrono-tz = { version = "0.10", default-features = false, optional = true }
5353
compact_str = "0.9"
54-
derive_more = { version = "2.0", features = ["debug", "display", "error", "from", "into", "into_iterator"] }
54+
derive_more = { version = "2.0", features = ["debug", "deref", "display", "error", "from", "into", "into_iterator"] }
5555
fnv = "1.0.5"
5656
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
5757
graphql-parser = { version = "0.4", optional = true }

juniper/src/types/scalars.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{char, marker::PhantomData, ops::Deref, rc::Rc, thread::JoinHandle};
1+
use std::{char, marker::PhantomData, rc::Rc, thread::JoinHandle};
22

3-
use derive_more::with_trait::{Display, From, Into};
3+
use derive_more::with_trait::{Deref, Display, From, Into};
44
use serde::{Deserialize, Serialize};
55

66
use crate::{
@@ -23,8 +23,9 @@ use crate::{
2323
///
2424
/// Represented as a string, but can be converted _to_ from an integer as well.
2525
#[derive(
26-
Clone, Debug, Deserialize, Display, Eq, From, GraphQLScalar, Into, PartialEq, Serialize,
26+
Clone, Debug, Deref, Deserialize, Display, Eq, From, GraphQLScalar, Into, PartialEq, Serialize,
2727
)]
28+
#[deref(forward)]
2829
#[graphql(parse_token(String, i32))]
2930
pub struct ID(String);
3031

@@ -49,14 +50,6 @@ impl ID {
4950
}
5051
}
5152

52-
impl Deref for ID {
53-
type Target = str;
54-
55-
fn deref(&self) -> &str {
56-
&self.0
57-
}
58-
}
59-
6053
#[graphql_scalar]
6154
#[graphql(with = impl_string_scalar)]
6255
type String = std::string::String;

juniper_codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exclude = ["/release.toml"]
2222
proc-macro = true
2323

2424
[dependencies]
25-
derive_more = { version = "2.0", features = ["display"] }
25+
derive_more = { version = "2.0", features = ["as_ref", "deref", "display"] }
2626
proc-macro2 = "1.0.4"
2727
quote = "1.0.9"
2828
syn = { version = "2.0", features = ["extra-traits", "full", "visit", "visit-mut"] }

juniper_codegen/src/common/span_container.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use std::{
2-
hash::{Hash, Hasher},
3-
ops,
4-
};
1+
use std::hash::{Hash, Hasher};
52

3+
use derive_more::with_trait::{AsRef, Deref};
64
use proc_macro2::{Span, TokenStream};
75
use quote::ToTokens;
86

9-
#[derive(Clone, Copy, Debug)]
7+
#[derive(AsRef, Clone, Copy, Debug, Deref)]
108
pub(crate) struct SpanContainer<T> {
119
expr: Option<Span>,
1210
ident: Span,
11+
#[as_ref]
12+
#[deref]
1313
val: T,
1414
}
1515

@@ -46,20 +46,6 @@ impl<T> SpanContainer<T> {
4646
}
4747
}
4848

49-
impl<T> AsRef<T> for SpanContainer<T> {
50-
fn as_ref(&self) -> &T {
51-
&self.val
52-
}
53-
}
54-
55-
impl<T> ops::Deref for SpanContainer<T> {
56-
type Target = T;
57-
58-
fn deref(&self) -> &Self::Target {
59-
&self.val
60-
}
61-
}
62-
6349
impl<T: PartialEq> PartialEq for SpanContainer<T> {
6450
fn eq(&self, other: &Self) -> bool {
6551
self.val == other.val

juniper_rocket/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ keywords = ["apollo", "graphql", "juniper", "rocket"]
1919
exclude = ["/examples/", "/tests/", "/release.toml"]
2020

2121
[dependencies]
22+
derive_more = { version = "2.0", features = ["as_ref"] }
2223
juniper = { version = "0.16", path = "../juniper", default-features = false }
2324
rocket = { version = "0.5", default-features = false }
2425
serde_json = "1.0.18"

juniper_rocket/src/lib.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ mod for_tests_only {
1515

1616
use std::{borrow::Cow, io::Cursor};
1717

18+
use derive_more::with_trait::{AsMut, AsRef};
19+
use juniper::{
20+
DefaultScalarValue, FieldError, GraphQLSubscriptionType, GraphQLType, GraphQLTypeAsync,
21+
InputValue, RootNode, ScalarValue,
22+
http::{self, GraphQLBatchRequest},
23+
};
1824
use rocket::{
1925
Data, Request,
2026
data::{self, FromData, ToByteUnit},
@@ -24,12 +30,6 @@ use rocket::{
2430
response::{self, Responder, Response, content::RawHtml},
2531
};
2632

27-
use juniper::{
28-
DefaultScalarValue, FieldError, GraphQLSubscriptionType, GraphQLType, GraphQLTypeAsync,
29-
InputValue, RootNode, ScalarValue,
30-
http::{self, GraphQLBatchRequest},
31-
};
32-
3333
/// Simple wrapper around an incoming GraphQL request.
3434
///
3535
/// See the [`http`] module for more information. This type can be constructed automatically from
@@ -74,23 +74,11 @@ use juniper::{
7474
/// .manage(Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()))
7575
/// .mount("/", routes![get_graphql_handler, post_graphql_handler]);
7676
/// ```
77-
#[derive(Debug, PartialEq)]
77+
#[derive(AsRef, AsMut, Debug, PartialEq)]
7878
pub struct GraphQLRequest<S = DefaultScalarValue>(GraphQLBatchRequest<S>)
7979
where
8080
S: ScalarValue;
8181

82-
impl<S: ScalarValue> AsRef<GraphQLBatchRequest<S>> for GraphQLRequest<S> {
83-
fn as_ref(&self) -> &GraphQLBatchRequest<S> {
84-
&self.0
85-
}
86-
}
87-
88-
impl<S: ScalarValue> AsMut<GraphQLBatchRequest<S>> for GraphQLRequest<S> {
89-
fn as_mut(&mut self) -> &mut GraphQLBatchRequest<S> {
90-
&mut self.0
91-
}
92-
}
93-
9482
/// Simple wrapper around the result of executing a GraphQL query
9583
pub struct GraphQLResponse(pub Status, pub String);
9684

0 commit comments

Comments
 (0)