Skip to content

Commit 36b1dba

Browse files
authored
Merge pull request #373 from dtolnay/include
Distinguish quoted vs bracketed includes in gen::include
2 parents d927633 + 4aae7c0 commit 36b1dba

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

gen/src/include.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::gen::out::OutFile;
2+
use crate::syntax::IncludeKind;
23
use std::fmt::{self, Display};
34

45
/// The complete contents of the "rust/cxx.h" header.
@@ -48,9 +49,15 @@ fn find_line(mut offset: usize, line: &str) -> Option<usize> {
4849
}
4950
}
5051

52+
#[derive(PartialEq)]
53+
pub struct Include {
54+
pub path: String,
55+
pub kind: IncludeKind,
56+
}
57+
5158
#[derive(Default, PartialEq)]
5259
pub struct Includes {
53-
custom: Vec<String>,
60+
custom: Vec<Include>,
5461
pub array: bool,
5562
pub cstddef: bool,
5663
pub cstdint: bool,
@@ -70,24 +77,30 @@ impl Includes {
7077
Includes::default()
7178
}
7279

73-
pub fn insert(&mut self, include: impl AsRef<str>) {
74-
self.custom.push(include.as_ref().to_owned());
80+
pub fn insert(&mut self, include: Include) {
81+
self.custom.push(include);
7582
}
7683
}
7784

78-
impl Extend<String> for Includes {
79-
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
80-
self.custom.extend(iter);
85+
impl<'a> Extend<&'a String> for Includes {
86+
fn extend<I: IntoIterator<Item = &'a String>>(&mut self, iter: I) {
87+
self.custom.extend(iter.into_iter().map(|path| Include {
88+
path: path.clone(),
89+
kind: IncludeKind::Quoted,
90+
}));
8191
}
8292
}
8393

8494
impl Display for Includes {
8595
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8696
for include in &self.custom {
87-
if include.starts_with('<') && include.ends_with('>') {
88-
writeln!(f, "#include {}", include)?;
89-
} else {
90-
writeln!(f, "#include \"{}\"", include.escape_default())?;
97+
match include.kind {
98+
IncludeKind::Quoted => {
99+
writeln!(f, "#include \"{}\"", include.path.escape_default())?;
100+
}
101+
IncludeKind::Bracketed => {
102+
writeln!(f, "#include <{}>", include.path)?;
103+
}
91104
}
92105
}
93106
if self.array {

gen/src/write.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
use crate::gen::include::Include;
12
use crate::gen::out::OutFile;
23
use crate::gen::{include, Opt};
34
use crate::syntax::atom::Atom::{self, *};
45
use crate::syntax::namespace::Namespace;
56
use crate::syntax::symbol::Symbol;
6-
use crate::syntax::{
7-
mangle, Api, Enum, ExternFn, ExternType, IncludeKind, Signature, Struct, Type, Types, Var,
8-
};
7+
use crate::syntax::{mangle, Api, Enum, ExternFn, ExternType, Signature, Struct, Type, Types, Var};
98
use proc_macro2::Ident;
109
use std::collections::HashMap;
1110

@@ -23,13 +22,12 @@ pub(super) fn gen(
2322
writeln!(out.front, "#pragma once");
2423
}
2524

26-
out.include.extend(opt.include.clone());
25+
out.include.extend(&opt.include);
2726
for api in apis {
2827
if let Api::Include(include) = api {
29-
match include.kind {
30-
IncludeKind::Quoted => out.include.insert(&include.path),
31-
IncludeKind::Bracketed => out.include.insert(format!("<{}>", include.path)),
32-
}
28+
let path = include.path.clone();
29+
let kind = include.kind;
30+
out.include.insert(Include { path, kind });
3331
}
3432
}
3533

0 commit comments

Comments
 (0)