Skip to content

Commit 7f25665

Browse files
committed
Revert "Bikeshedding, vol.8 [skip ci]"
This reverts commit 63a005d.
1 parent 63a005d commit 7f25665

File tree

11 files changed

+216
-211
lines changed

11 files changed

+216
-211
lines changed

juniper/src/ast.rs

Lines changed: 11 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
use std::{
2-
borrow::{Borrow, Cow},
3-
fmt,
4-
hash::Hash,
5-
ops::Deref,
6-
slice, vec,
7-
};
1+
use std::{borrow::Cow, fmt, hash::Hash, slice, vec};
82

93
use arcstr::ArcStr;
104

@@ -16,114 +10,29 @@ use crate::{
1610
value::{DefaultScalarValue, ScalarValue},
1711
};
1812

19-
/// Name of a [`Type`] literal.
20-
#[derive(Clone, Debug, Eq, PartialEq)]
21-
pub enum TypeName<'a> {
22-
/// Owned version of this name.
23-
Owned(ArcStr),
24-
25-
/// Borrowed version of this name.
26-
Borrowed(&'a str),
27-
}
28-
29-
impl fmt::Display for TypeName<'_> {
30-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31-
match self {
32-
Self::Owned(n) => fmt::Display::fmt(n, f),
33-
Self::Borrowed(n) => fmt::Display::fmt(n, f),
34-
}
35-
}
36-
}
37-
38-
impl AsRef<str> for TypeName<'_> {
39-
fn as_ref(&self) -> &str {
40-
match self {
41-
Self::Owned(n) => n.as_ref(),
42-
Self::Borrowed(n) => n,
43-
}
44-
}
45-
}
46-
47-
impl Borrow<str> for TypeName<'_> {
48-
fn borrow(&self) -> &str {
49-
self.as_ref()
50-
}
51-
}
52-
53-
impl Deref for TypeName<'_> {
54-
type Target = str;
55-
56-
fn deref(&self) -> &Self::Target {
57-
self.as_ref()
58-
}
59-
}
60-
61-
impl<'a> From<&'a str> for TypeName<'a> {
62-
fn from(v: &'a str) -> Self {
63-
Self::Borrowed(v)
64-
}
65-
}
66-
67-
impl From<ArcStr> for TypeName<'_> {
68-
fn from(v: ArcStr) -> Self {
69-
Self::Owned(v)
70-
}
71-
}
72-
73-
impl<'a> From<&'a ArcStr> for TypeName<'a> {
74-
fn from(v: &'a ArcStr) -> Self {
75-
Self::Borrowed(v.as_ref())
76-
}
77-
}
78-
79-
impl From<TypeName<'_>> for Box<str> {
80-
fn from(n: TypeName<'_>) -> Self {
81-
n.as_ref().into()
82-
}
83-
}
84-
85-
impl From<&TypeName<'_>> for Box<str> {
86-
fn from(n: &TypeName<'_>) -> Self {
87-
n.as_ref().into()
88-
}
89-
}
90-
91-
impl TypeName<'_> {
92-
/// Return owned value if this [`TypeName`].
93-
///
94-
/// [`Clone`]s if it's [`ArcStr`] already, otherwise allocates a new one.
95-
#[must_use]
96-
pub fn to_owned(&self) -> ArcStr {
97-
match self {
98-
Self::Owned(n) => n.clone(),
99-
Self::Borrowed(n) => (*n).into(),
100-
}
101-
}
102-
}
103-
10413
/// Type literal in a syntax tree.
10514
///
10615
/// This enum carries no semantic information and might refer to types that do not exist.
10716
#[derive(Clone, Debug, Eq, PartialEq)]
108-
pub enum Type<'a> {
17+
pub enum Type<N = ArcStr> {
10918
/// `null`able named type, e.g. `String`.
110-
Named(TypeName<'a>),
19+
Named(N),
11120

11221
/// `null`able list type, e.g. `[String]`.
11322
///
11423
/// The list itself is `null`able, the containing [`Type`] might be non-`null`.
115-
List(Box<Type<'a>>, Option<usize>),
24+
List(Box<Type<N>>, Option<usize>),
11625

11726
/// Non-`null` named type, e.g. `String!`.
118-
NonNullNamed(TypeName<'a>),
27+
NonNullNamed(N),
11928

12029
/// Non-`null` list type, e.g. `[String]!`.
12130
///
12231
/// The list itself is non-`null`, the containing [`Type`] might be `null`able.
123-
NonNullList(Box<Type<'a>>, Option<usize>),
32+
NonNullList(Box<Type<N>>, Option<usize>),
12433
}
12534

126-
impl fmt::Display for Type<'_> {
35+
impl<N: fmt::Display> fmt::Display for Type<N> {
12736
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12837
match self {
12938
Self::Named(n) => write!(f, "{n}"),
@@ -134,7 +43,7 @@ impl fmt::Display for Type<'_> {
13443
}
13544
}
13645

137-
impl<'a> Type<'a> {
46+
impl<N: AsRef<str>> Type<N> {
13847
/// Returns the name of this named [`Type`].
13948
///
14049
/// Only applies to named [`Type`]s. Lists will return [`None`].
@@ -150,24 +59,13 @@ impl<'a> Type<'a> {
15059
///
15160
/// All [`Type`] literals contain exactly one named type.
15261
#[must_use]
153-
pub fn innermost_name(&self) -> &TypeName<'_> {
62+
pub fn innermost_name(&self) -> &str {
15463
match self {
155-
Self::Named(n) | Self::NonNullNamed(n) => n,
64+
Self::Named(n) | Self::NonNullNamed(n) => n.as_ref(),
15665
Self::List(l, ..) | Self::NonNullList(l, ..) => l.innermost_name(),
15766
}
15867
}
15968

160-
/// Returns the owned innermost name of this [`Type`] by unpacking lists.
161-
///
162-
/// All [`Type`] literals contain exactly one named type.
163-
#[must_use]
164-
pub fn to_innermost_name(&self) -> ArcStr {
165-
match self.innermost_name() {
166-
TypeName::Owned(n) => n.clone(),
167-
TypeName::Borrowed(n) => (*n).into(),
168-
}
169-
}
170-
17169
/// Indicates whether this [`Type`] can only represent non-`null` values.
17270
#[must_use]
17371
pub fn is_non_null(&self) -> bool {
@@ -197,7 +95,7 @@ pub enum InputValue<S = DefaultScalarValue> {
19795

19896
#[derive(Clone, Debug, PartialEq)]
19997
pub struct VariableDefinition<'a, S> {
200-
pub var_type: Spanning<Type<'a>>,
98+
pub var_type: Spanning<Type<&'a str>>,
20199
pub default_value: Option<Spanning<InputValue<S>>>,
202100
pub directives: Option<Vec<Spanning<Directive<'a, S>>>>,
203101
}

juniper/src/executor/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use futures::Stream;
1515
use crate::{
1616
ast::{
1717
Definition, Document, Fragment, FromInputValue, InputValue, Operation, OperationType,
18-
Selection, ToInputValue, Type, TypeName,
18+
Selection, ToInputValue, Type,
1919
},
2020
parser::{SourcePosition, Spanning},
2121
schema::{
@@ -1149,18 +1149,15 @@ impl<S> Registry<S> {
11491149
///
11501150
/// If this [`Registry`] hasn't seen a [`Type`] with such [`GraphQLType::name`] before, it will
11511151
/// construct the one and store it.
1152-
pub fn get_type<T>(&mut self, info: &T::TypeInfo) -> Type<'static>
1152+
pub fn get_type<T>(&mut self, info: &T::TypeInfo) -> Type
11531153
where
11541154
T: GraphQLType<S> + ?Sized,
11551155
S: ScalarValue,
11561156
{
11571157
if let Some(name) = T::name(info) {
11581158
let validated_name = Name::new(name.clone()).unwrap();
11591159
if !self.types.contains_key(&name) {
1160-
self.insert_placeholder(
1161-
validated_name.clone(),
1162-
Type::NonNullNamed(TypeName::Owned(name.clone())),
1163-
);
1160+
self.insert_placeholder(validated_name.clone(), Type::NonNullNamed(name.clone()));
11641161
let meta = T::meta(info, self);
11651162
self.types.insert(validated_name, meta);
11661163
}
@@ -1227,7 +1224,7 @@ impl<S> Registry<S> {
12271224
Argument::new(name, self.get_type::<T>(info)).default_value(value.to_input_value())
12281225
}
12291226

1230-
fn insert_placeholder(&mut self, name: Name, of_type: Type<'static>) {
1227+
fn insert_placeholder(&mut self, name: Name, of_type: Type) {
12311228
self.types
12321229
.entry(name)
12331230
.or_insert(MetaType::Placeholder(PlaceholderMeta { of_type }));
@@ -1315,7 +1312,7 @@ impl<S> Registry<S> {
13151312
}
13161313

13171314
/// Creates an [`UnionMeta`] type of the given `types`.
1318-
pub fn build_union_type<T>(&mut self, info: &T::TypeInfo, types: &[Type<'static>]) -> UnionMeta
1315+
pub fn build_union_type<T>(&mut self, info: &T::TypeInfo, types: &[Type]) -> UnionMeta
13191316
where
13201317
T: GraphQLType<S> + ?Sized,
13211318
S: ScalarValue,

juniper/src/parser/document.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ where
507507
))
508508
}
509509

510-
pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<'a>> {
510+
pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<&'a str>> {
511511
let parsed_type = if let Some(Spanning {
512512
span: ref start_span,
513513
..
@@ -521,7 +521,7 @@ pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<'a>> {
521521
Type::List(Box::new(inner_type.item), None),
522522
)
523523
} else {
524-
parser.expect_name()?.map(|n| Type::Named(n.into()))
524+
parser.expect_name()?.map(Type::Named)
525525
};
526526

527527
Ok(match *parser.peek() {
@@ -533,7 +533,10 @@ pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<Type<'a>> {
533533
})
534534
}
535535

536-
fn wrap_non_null<'a>(parser: &mut Parser<'a>, inner: Spanning<Type<'a>>) -> ParseResult<Type<'a>> {
536+
fn wrap_non_null<'a>(
537+
parser: &mut Parser<'a>,
538+
inner: Spanning<Type<&'a str>>,
539+
) -> ParseResult<Type<&'a str>> {
537540
let end_pos = &parser.expect(&Token::ExclamationMark)?.span.end;
538541

539542
let wrapped = match inner.item {

juniper/src/schema/meta.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Types used to describe a `GraphQL` schema
22
3-
use std::fmt;
3+
use std::{borrow::ToOwned, fmt};
44

55
use arcstr::ArcStr;
66

@@ -114,7 +114,7 @@ pub type ScalarTokenParseFn<S> = for<'b> fn(ScalarToken<'b>) -> Result<S, ParseE
114114
#[derive(Debug)]
115115
pub struct ListMeta {
116116
#[doc(hidden)]
117-
pub of_type: Type<'static>,
117+
pub of_type: Type,
118118

119119
#[doc(hidden)]
120120
pub expected_size: Option<usize>,
@@ -125,7 +125,7 @@ impl ListMeta {
125125
///
126126
/// Specifying `expected_size` will be used to ensure that values of this type will always match
127127
/// it.
128-
pub fn new(of_type: Type<'static>, expected_size: Option<usize>) -> Self {
128+
pub fn new(of_type: Type, expected_size: Option<usize>) -> Self {
129129
Self {
130130
of_type,
131131
expected_size,
@@ -142,12 +142,12 @@ impl ListMeta {
142142
#[derive(Debug)]
143143
pub struct NullableMeta {
144144
#[doc(hidden)]
145-
pub of_type: Type<'static>,
145+
pub of_type: Type,
146146
}
147147

148148
impl NullableMeta {
149149
/// Builds a new [`NullableMeta`] type by wrapping the specified [`Type`].
150-
pub fn new(of_type: Type<'static>) -> Self {
150+
pub fn new(of_type: Type) -> Self {
151151
Self { of_type }
152152
}
153153

@@ -197,10 +197,10 @@ impl<S> ObjectMeta<S> {
197197
///
198198
/// Overwrites any previously set list of interfaces.
199199
#[must_use]
200-
pub fn interfaces(mut self, interfaces: &[Type<'static>]) -> Self {
200+
pub fn interfaces(mut self, interfaces: &[Type]) -> Self {
201201
self.interface_names = interfaces
202202
.iter()
203-
.map(|t| t.innermost_name().to_owned())
203+
.map(|t| t.innermost_name().into())
204204
.collect();
205205
self
206206
}
@@ -302,10 +302,10 @@ impl<S> InterfaceMeta<S> {
302302
///
303303
/// Overwrites any previously set list of interfaces.
304304
#[must_use]
305-
pub fn interfaces(mut self, interfaces: &[Type<'static>]) -> Self {
305+
pub fn interfaces(mut self, interfaces: &[Type]) -> Self {
306306
self.interface_names = interfaces
307307
.iter()
308-
.map(|t| t.innermost_name().to_owned())
308+
.map(|t| t.innermost_name().into())
309309
.collect();
310310
self
311311
}
@@ -329,14 +329,11 @@ pub struct UnionMeta {
329329

330330
impl UnionMeta {
331331
/// Builds a new [`UnionMeta`] type with the specified `name` and possible [`Type`]s.
332-
pub fn new(name: impl Into<ArcStr>, of_types: &[Type<'static>]) -> Self {
332+
pub fn new(name: impl Into<ArcStr>, of_types: &[Type]) -> Self {
333333
Self {
334334
name: name.into(),
335335
description: None,
336-
of_type_names: of_types
337-
.iter()
338-
.map(|t| t.innermost_name().to_owned())
339-
.collect(),
336+
of_type_names: of_types.iter().map(|t| t.innermost_name().into()).collect(),
340337
}
341338
}
342339

@@ -414,7 +411,7 @@ impl<S> InputObjectMeta<S> {
414411
#[derive(Debug)]
415412
pub struct PlaceholderMeta {
416413
#[doc(hidden)]
417-
pub of_type: Type<'static>,
414+
pub of_type: Type,
418415
}
419416

420417
/// Metadata for a field
@@ -427,7 +424,7 @@ pub struct Field<S> {
427424
#[doc(hidden)]
428425
pub arguments: Option<Vec<Argument<S>>>,
429426
#[doc(hidden)]
430-
pub field_type: Type<'static>,
427+
pub field_type: Type,
431428
#[doc(hidden)]
432429
pub deprecation_status: DeprecationStatus,
433430
}
@@ -483,14 +480,14 @@ pub struct Argument<S> {
483480
#[doc(hidden)]
484481
pub description: Option<ArcStr>,
485482
#[doc(hidden)]
486-
pub arg_type: Type<'static>,
483+
pub arg_type: Type,
487484
#[doc(hidden)]
488485
pub default_value: Option<InputValue<S>>,
489486
}
490487

491488
impl<S> Argument<S> {
492489
/// Builds a new [`Argument`] of the given [`Type`] with the given `name`.
493-
pub fn new(name: impl Into<ArcStr>, arg_type: Type<'static>) -> Self {
490+
pub fn new(name: impl Into<ArcStr>, arg_type: Type) -> Self {
494491
Self {
495492
name: name.into(),
496493
description: None,
@@ -705,14 +702,14 @@ impl<S> MetaType<S> {
705702
}
706703

707704
/// Construct a [`Type`] literal out of this [`MetaType`].
708-
pub fn as_type(&self) -> Type<'static> {
705+
pub fn as_type(&self) -> Type {
709706
match self {
710707
Self::Enum(EnumMeta { name, .. })
711708
| Self::InputObject(InputObjectMeta { name, .. })
712709
| Self::Interface(InterfaceMeta { name, .. })
713710
| Self::Object(ObjectMeta { name, .. })
714711
| Self::Scalar(ScalarMeta { name, .. })
715-
| Self::Union(UnionMeta { name, .. }) => Type::NonNullNamed(name.clone().into()),
712+
| Self::Union(UnionMeta { name, .. }) => Type::NonNullNamed(name.clone()),
716713
Self::List(ListMeta {
717714
of_type,
718715
expected_size,

0 commit comments

Comments
 (0)