Skip to content

Commit 0f8ab22

Browse files
committed
Improving Pair construction.
1 parent 0447e96 commit 0f8ab22

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

syntax/impls.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,22 @@ impl Borrow<Type> for &Impl {
300300

301301
impl Pair {
302302
/// Use this constructor when the item can't have a different
303-
/// name in Rust and C++. For cases where #[rust_name] and similar
304-
/// attributes can be used, construct the object by hand.
303+
/// name in Rust and C++.
305304
pub fn new(ns: Namespace, ident: Ident) -> Self {
306305
Self {
307306
rust: ident.clone(),
308307
cxx: CppName::new(ns, ident),
309308
}
310309
}
310+
311+
/// Use this constructor when attributes such as #[rust_name]
312+
/// can be used to potentially give a different name in Rust vs C++.
313+
pub fn new_from_differing_names(ns: Namespace, cxx_ident: Ident, rust_ident: Ident) -> Self {
314+
Self {
315+
rust: rust_ident,
316+
cxx: CppName::new(ns, cxx_ident),
317+
}
318+
}
311319
}
312320

313321
impl ResolvableName {

syntax/parse.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::syntax::file::{Item, ItemForeignMod};
33
use crate::syntax::report::Errors;
44
use crate::syntax::Atom::*;
55
use crate::syntax::{
6-
attrs, error, Api, CppName, Doc, Enum, ExternFn, ExternType, Impl, Include, IncludeKind, Lang,
6+
attrs, error, Api, Doc, Enum, ExternFn, ExternType, Impl, Include, IncludeKind, Lang,
77
Namespace, Pair, Receiver, Ref, ResolvableName, Signature, Slice, Struct, Ty1, Type, TypeAlias,
88
Var, Variant,
99
};
@@ -398,10 +398,11 @@ fn parse_extern_fn(
398398
let throws = throws_tokens.is_some();
399399
let unsafety = foreign_fn.sig.unsafety;
400400
let fn_token = foreign_fn.sig.fn_token;
401-
let ident = Pair {
402-
cxx: CppName::new(ns, cxx_name.unwrap_or(foreign_fn.sig.ident.clone())),
403-
rust: rust_name.unwrap_or(foreign_fn.sig.ident.clone()),
404-
};
401+
let ident = Pair::new_from_differing_names(
402+
ns,
403+
cxx_name.unwrap_or(foreign_fn.sig.ident.clone()),
404+
rust_name.unwrap_or(foreign_fn.sig.ident.clone()),
405+
);
405406
let paren_token = foreign_fn.sig.paren_token;
406407
let semi_token = foreign_fn.semi_token;
407408
let api_function = match lang {

0 commit comments

Comments
 (0)