Skip to content

Commit bec973c

Browse files
committed
Make Type an opaque type
1 parent 561b46a commit bec973c

File tree

19 files changed

+1500
-1197
lines changed

19 files changed

+1500
-1197
lines changed

codegen/src/type_gen.rs

+62-65
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const PG_RANGE_H: &'static str = include_str!("pg_range.h");
1414
struct Type {
1515
name: &'static str,
1616
variant: String,
17+
ident: String,
1718
kind: &'static str,
1819
element: u32,
1920
doc: String,
@@ -27,8 +28,8 @@ pub fn build(path: &Path) {
2728

2829
make_header(&mut file);
2930
make_enum(&mut file, &types);
30-
make_display_impl(&mut file);
3131
make_impl(&mut file, &types);
32+
make_consts(&mut file, &types);
3233
}
3334

3435
fn parse_ranges() -> BTreeMap<u32, u32> {
@@ -69,14 +70,10 @@ fn parse_types(ranges: &BTreeMap<u32, u32>) -> BTreeMap<u32, Type> {
6970

7071
let name = split[5];
7172

72-
let variant = match name {
73-
"anyarray" => "AnyArray".to_owned(),
74-
name => {
75-
let variant = range_vector_re.replace(name, "_$1");
76-
let variant = array_re.replace(&variant, "$1_array");
77-
snake_to_camel(&variant)
78-
}
79-
};
73+
let ident = range_vector_re.replace(name, "_$1");
74+
let ident = array_re.replace(&ident, "$1_array");
75+
let variant = snake_to_camel(&ident);
76+
let ident = ident.to_ascii_uppercase();
8077

8178
let kind = split[11];
8279

@@ -106,11 +103,12 @@ fn parse_types(ranges: &BTreeMap<u32, u32>) -> BTreeMap<u32, Type> {
106103
let doc = String::from_utf8(doc).unwrap();
107104

108105
let type_ = Type {
109-
name: name,
110-
variant: variant,
111-
kind: kind,
112-
element: element,
113-
doc: doc,
106+
name,
107+
variant,
108+
ident,
109+
kind,
110+
element,
111+
doc,
114112
};
115113

116114
types.insert(oid, type_);
@@ -123,74 +121,60 @@ fn make_header(w: &mut BufWriter<File>) {
123121
write!(
124122
w,
125123
"// Autogenerated file - DO NOT EDIT
126-
use std::fmt;
124+
use std::sync::Arc;
127125
128-
use types::{{Oid, Kind, Other}};
126+
use types::{{Type, Oid, Kind}};
129127
128+
#[derive(PartialEq, Eq, Debug)]
129+
pub struct Other {{
130+
pub name: String,
131+
pub oid: Oid,
132+
pub kind: Kind,
133+
pub schema: String,
134+
}}
130135
"
131136
).unwrap();
132137
}
133138

134139
fn make_enum(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
135140
write!(
136141
w,
137-
"/// A Postgres type.
142+
"
138143
#[derive(PartialEq, Eq, Clone, Debug)]
139-
pub enum Type {{
140-
"
144+
pub enum Inner {{"
141145
).unwrap();
142146

143147
for type_ in types.values() {
144148
write!(
145149
w,
146-
" /// {}
147-
{},
148-
",
149-
type_.doc,
150+
"
151+
{},",
150152
type_.variant
151153
).unwrap();
152154
}
153155

154156
write!(
155157
w,
156-
r" /// An unknown type.
157-
Other(Other),
158+
r"
159+
Other(Arc<Other>),
158160
}}
159161
160162
"
161163
).unwrap();
162164
}
163165

164-
fn make_display_impl(w: &mut BufWriter<File>) {
165-
write!(w,
166-
r#"impl fmt::Display for Type {{
167-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {{
168-
match self.schema() {{
169-
"public" | "pg_catalog" => {{}}
170-
schema => write!(fmt, "{{}}.", schema)?,
171-
}}
172-
fmt.write_str(self.name())
173-
}}
174-
}}
175-
176-
"#,
177-
).unwrap();
178-
}
179-
180166
fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
181167
write!(w,
182-
"impl Type {{
183-
/// Returns the `Type` corresponding to the provided `Oid` if it
184-
/// corresponds to a built-in type.
185-
pub fn from_oid(oid: Oid) -> Option<Type> {{
168+
"impl Inner {{
169+
pub fn from_oid(oid: Oid) -> Option<Inner> {{
186170
match oid {{
187171
",
188172
).unwrap();
189173

190174
for (oid, type_) in types {
191175
write!(
192176
w,
193-
" {} => Some(Type::{}),
177+
" {} => Some(Inner::{}),
194178
",
195179
oid,
196180
type_.variant
@@ -202,7 +186,6 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
202186
}}
203187
}}
204188
205-
/// Returns the OID of the `Type`.
206189
pub fn oid(&self) -> Oid {{
207190
match *self {{
208191
",
@@ -212,19 +195,18 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
212195
for (oid, type_) in types {
213196
write!(
214197
w,
215-
" Type::{} => {},
198+
" Inner::{} => {},
216199
",
217200
type_.variant,
218201
oid
219202
).unwrap();
220203
}
221204

222205
write!(w,
223-
" Type::Other(ref u) => u.oid(),
206+
" Inner::Other(ref u) => u.oid,
224207
}}
225208
}}
226209
227-
/// Returns the kind of this type.
228210
pub fn kind(&self) -> &Kind {{
229211
match *self {{
230212
",
@@ -233,14 +215,14 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
233215
for type_ in types.values() {
234216
let kind = match type_.kind {
235217
"P" => "Pseudo".to_owned(),
236-
"A" => format!("Array(Type::{})", types[&type_.element].variant),
237-
"R" => format!("Range(Type::{})", types[&type_.element].variant),
218+
"A" => format!("Array(Type(Inner::{}))", types[&type_.element].variant),
219+
"R" => format!("Range(Type(Inner::{}))", types[&type_.element].variant),
238220
_ => "Simple".to_owned(),
239221
};
240222

241223
write!(
242224
w,
243-
" Type::{} => {{
225+
" Inner::{} => {{
244226
const V: &'static Kind = &Kind::{};
245227
V
246228
}}
@@ -251,19 +233,10 @@ fn make_impl(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
251233
}
252234

253235
write!(w,
254-
r#" Type::Other(ref u) => u.kind(),
236+
r#" Inner::Other(ref u) => &u.kind,
255237
}}
256238
}}
257239
258-
/// Returns the schema of this type.
259-
pub fn schema(&self) -> &str {{
260-
match *self {{
261-
Type::Other(ref u) => u.schema(),
262-
_ => "pg_catalog",
263-
}}
264-
}}
265-
266-
/// Returns the name of this type.
267240
pub fn name(&self) -> &str {{
268241
match *self {{
269242
"#,
@@ -272,7 +245,7 @@ r#" Type::Other(ref u) => u.kind(),
272245
for type_ in types.values() {
273246
write!(
274247
w,
275-
r#" Type::{} => "{}",
248+
r#" Inner::{} => "{}",
276249
"#,
277250
type_.variant,
278251
type_.name
@@ -281,10 +254,34 @@ r#" Type::Other(ref u) => u.kind(),
281254

282255
write!(
283256
w,
284-
" Type::Other(ref u) => u.name(),
257+
" Inner::Other(ref u) => &u.name,
285258
}}
286259
}}
287260
}}
288261
"
289262
).unwrap();
290263
}
264+
265+
fn make_consts(w: &mut BufWriter<File>, types: &BTreeMap<u32, Type>) {
266+
write!(w,
267+
"pub mod consts {{
268+
use types::Type;
269+
use types::type_gen::Inner;
270+
",
271+
).unwrap();
272+
273+
for type_ in types.values() {
274+
write!(w,
275+
"
276+
/// {docs}
277+
pub const {ident}: Type = Type(Inner::{variant});
278+
",
279+
docs = type_.doc,
280+
ident = type_.ident,
281+
variant = type_.variant).unwrap();
282+
}
283+
284+
write!(w,
285+
"}}"
286+
).unwrap();
287+
}

postgres-shared/src/types/bit_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use postgres_protocol::types;
44
use self::bit_vec::BitVec;
55
use std::error::Error;
66

7-
use types::{FromSql, ToSql, IsNull, Type};
7+
use types::{FromSql, ToSql, IsNull, Type, BIT, VARBIT};
88

99
impl FromSql for BitVec {
1010
fn from_sql(_: &Type, raw: &[u8]) -> Result<BitVec, Box<Error + Sync + Send>> {
@@ -17,7 +17,7 @@ impl FromSql for BitVec {
1717
Ok(bitvec)
1818
}
1919

20-
accepts!(Type::Bit, Type::Varbit);
20+
accepts!(BIT, VARBIT);
2121
}
2222

2323
impl ToSql for BitVec {
@@ -26,6 +26,6 @@ impl ToSql for BitVec {
2626
Ok(IsNull::No)
2727
}
2828

29-
accepts!(Type::Bit, Type::Varbit);
29+
accepts!(BIT, VARBIT);
3030
to_sql_checked!();
3131
}

0 commit comments

Comments
 (0)