Skip to content

Commit b78001c

Browse files
authored
Generic types to generic (#30)
1 parent 7183546 commit b78001c

File tree

5 files changed

+65
-68
lines changed

5 files changed

+65
-68
lines changed

ast/asdl_rs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def emit_range(self, has_attributes, depth):
279279
if has_attributes:
280280
self.emit("pub range: R,", depth + 1)
281281
else:
282-
self.emit("pub range: crate::ranged::OptionalRange<R>,", depth + 1)
282+
self.emit("pub range: OptionalRange<R>,", depth + 1)
283283

284284
def simple_sum(self, sum, name, depth):
285285
rust_name = rust_type_name(name)

ast/src/gen/generic.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::text_size::TextRange;
44
#[derive(Clone, Debug, PartialEq)]
55
pub struct ModModule<R = TextRange> {
6-
pub range: crate::ranged::OptionalRange<R>,
6+
pub range: OptionalRange<R>,
77
pub body: Vec<Stmt<R>>,
88
pub type_ignores: Vec<TypeIgnore<R>>,
99
}
@@ -16,7 +16,7 @@ impl<R> From<ModModule<R>> for Mod<R> {
1616

1717
#[derive(Clone, Debug, PartialEq)]
1818
pub struct ModInteractive<R = TextRange> {
19-
pub range: crate::ranged::OptionalRange<R>,
19+
pub range: OptionalRange<R>,
2020
pub body: Vec<Stmt<R>>,
2121
}
2222

@@ -28,7 +28,7 @@ impl<R> From<ModInteractive<R>> for Mod<R> {
2828

2929
#[derive(Clone, Debug, PartialEq)]
3030
pub struct ModExpression<R = TextRange> {
31-
pub range: crate::ranged::OptionalRange<R>,
31+
pub range: OptionalRange<R>,
3232
pub body: Box<Expr<R>>,
3333
}
3434

@@ -40,7 +40,7 @@ impl<R> From<ModExpression<R>> for Mod<R> {
4040

4141
#[derive(Clone, Debug, PartialEq)]
4242
pub struct ModFunctionType<R = TextRange> {
43-
pub range: crate::ranged::OptionalRange<R>,
43+
pub range: OptionalRange<R>,
4444
pub argtypes: Vec<Expr<R>>,
4545
pub returns: Box<Expr<R>>,
4646
}
@@ -957,7 +957,7 @@ pub struct Comprehension<R = TextRange> {
957957
pub iter: Expr<R>,
958958
pub ifs: Vec<Expr<R>>,
959959
pub is_async: bool,
960-
pub range: crate::ranged::OptionalRange<R>,
960+
pub range: OptionalRange<R>,
961961
}
962962

963963
#[derive(Clone, Debug, PartialEq)]
@@ -988,7 +988,7 @@ pub struct Arguments<R = TextRange> {
988988
pub kw_defaults: Vec<Expr<R>>,
989989
pub kwarg: Option<Box<Arg<R>>>,
990990
pub defaults: Vec<Expr<R>>,
991-
pub range: crate::ranged::OptionalRange<R>,
991+
pub range: OptionalRange<R>,
992992
}
993993

994994
#[derive(Clone, Debug, PartialEq)]
@@ -1017,15 +1017,15 @@ pub struct Alias<R = TextRange> {
10171017
pub struct Withitem<R = TextRange> {
10181018
pub context_expr: Expr<R>,
10191019
pub optional_vars: Option<Box<Expr<R>>>,
1020-
pub range: crate::ranged::OptionalRange<R>,
1020+
pub range: OptionalRange<R>,
10211021
}
10221022

10231023
#[derive(Clone, Debug, PartialEq)]
10241024
pub struct MatchCase<R = TextRange> {
10251025
pub pattern: Pattern<R>,
10261026
pub guard: Option<Box<Expr<R>>>,
10271027
pub body: Vec<Stmt<R>>,
1028-
pub range: crate::ranged::OptionalRange<R>,
1028+
pub range: OptionalRange<R>,
10291029
}
10301030

10311031
#[derive(Clone, Debug, PartialEq)]
@@ -1144,7 +1144,7 @@ pub enum Pattern<R = TextRange> {
11441144

11451145
#[derive(Clone, Debug, PartialEq)]
11461146
pub struct TypeIgnoreTypeIgnore<R = TextRange> {
1147-
pub range: crate::ranged::OptionalRange<R>,
1147+
pub range: OptionalRange<R>,
11481148
pub lineno: Int,
11491149
pub tag: String,
11501150
}

ast/src/generic.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![allow(clippy::derive_partial_eq_without_eq)]
2+
pub use crate::{builtin::*, text_size::TextSize};
3+
use std::fmt::{Debug, Display, Formatter};
4+
use std::marker::PhantomData;
5+
6+
pub type Suite<R = TextRange> = Vec<Stmt<R>>;
7+
8+
#[cfg(feature = "all-nodes-with-ranges")]
9+
pub type OptionalRange<R> = R;
10+
11+
#[cfg(not(feature = "all-nodes-with-ranges"))]
12+
pub type OptionalRange<R> = EmptyRange<R>;
13+
14+
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
15+
pub struct EmptyRange<R> {
16+
phantom: PhantomData<R>,
17+
}
18+
19+
impl<R> EmptyRange<R> {
20+
#[inline(always)]
21+
pub fn new(_start: TextSize, _end: TextSize) -> Self {
22+
Self {
23+
phantom: PhantomData,
24+
}
25+
}
26+
}
27+
28+
impl<R> Display for EmptyRange<R> {
29+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30+
f.write_str("()")
31+
}
32+
}
33+
34+
impl<R> Debug for EmptyRange<R> {
35+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36+
Display::fmt(self, f)
37+
}
38+
}
39+
40+
impl<R> Default for EmptyRange<R> {
41+
fn default() -> Self {
42+
EmptyRange {
43+
phantom: PhantomData,
44+
}
45+
}
46+
}
47+
48+
include!("gen/generic.rs");

ast/src/lib.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
mod builtin;
2-
#[cfg(feature = "fold")]
3-
mod fold_helpers;
4-
mod generic {
5-
#![allow(clippy::derive_partial_eq_without_eq)]
6-
pub use crate::builtin::*;
7-
8-
include!("gen/generic.rs");
9-
}
2+
mod generic;
103
mod impls;
114
mod ranged;
12-
#[cfg(feature = "location")]
13-
mod source_locator;
145
#[cfg(feature = "unparse")]
156
mod unparse;
167

178
pub use builtin::*;
189
pub use generic::*;
19-
pub use ranged::{EmptyRange, OptionalRange, Ranged, Suite};
10+
pub use ranged::Ranged;
2011
pub use rustpython_parser_core::{text_size, ConversionFlag};
2112

13+
#[cfg(feature = "fold")]
14+
mod fold_helpers;
2215
#[cfg(feature = "fold")]
2316
pub mod fold {
2417
use super::generic::*;
@@ -35,14 +28,15 @@ mod visitor {
3528

3629
#[cfg(feature = "location")]
3730
pub mod located;
38-
31+
#[cfg(feature = "location")]
32+
mod source_locator;
3933
#[cfg(feature = "location")]
4034
pub use rustpython_parser_core::source_code;
35+
4136
#[cfg(feature = "visitor")]
4237
pub use visitor::Visitor;
4338

4439
#[cfg(feature = "constant-optimization")]
4540
mod optimizer;
46-
4741
#[cfg(feature = "constant-optimization")]
4842
pub use optimizer::ConstantOptimizer;

ast/src/ranged.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use crate::text_size::{TextRange, TextSize};
2-
use std::fmt::{Debug, Display, Formatter};
3-
use std::marker::PhantomData;
42

53
pub use crate::builtin::*;
6-
use crate::Stmt;
74

85
pub trait Ranged {
96
fn range(&self) -> TextRange;
@@ -17,46 +14,4 @@ pub trait Ranged {
1714
}
1815
}
1916

20-
pub type Suite<R = TextRange> = Vec<Stmt<R>>;
21-
22-
#[cfg(feature = "all-nodes-with-ranges")]
23-
pub type OptionalRange<R> = R;
24-
25-
#[cfg(not(feature = "all-nodes-with-ranges"))]
26-
pub type OptionalRange<R> = EmptyRange<R>;
27-
28-
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
29-
pub struct EmptyRange<R> {
30-
phantom: PhantomData<R>,
31-
}
32-
33-
impl<R> EmptyRange<R> {
34-
#[inline(always)]
35-
pub fn new(_start: TextSize, _end: TextSize) -> Self {
36-
Self {
37-
phantom: PhantomData,
38-
}
39-
}
40-
}
41-
42-
impl<R> Display for EmptyRange<R> {
43-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
44-
f.write_str("()")
45-
}
46-
}
47-
48-
impl<R> Debug for EmptyRange<R> {
49-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50-
Display::fmt(self, f)
51-
}
52-
}
53-
54-
impl<R> Default for EmptyRange<R> {
55-
fn default() -> Self {
56-
EmptyRange {
57-
phantom: PhantomData,
58-
}
59-
}
60-
}
61-
6217
include!("gen/ranged.rs");

0 commit comments

Comments
 (0)