Move non-boilerplate impls out of impls.rs
diff --git a/syntax/impls.rs b/syntax/impls.rs
index f4c5b05..a4b393a 100644
--- a/syntax/impls.rs
+++ b/syntax/impls.rs
@@ -1,13 +1,8 @@
-use crate::syntax::{
- Api, CppName, ExternFn, Impl, Include, Namespace, Pair, Receiver, Ref, ResolvableName,
- Signature, Slice, Symbol, Ty1, Type, Types,
-};
-use proc_macro2::{Ident, Span};
+use crate::syntax::{ExternFn, Impl, Include, Receiver, Ref, Signature, Slice, Ty1, Type};
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::mem;
use std::ops::{Deref, DerefMut};
-use syn::Token;
impl PartialEq for Include {
fn eq(&self, other: &Include) -> bool {
@@ -297,86 +292,3 @@
&self.ty
}
}
-
-impl Pair {
- /// Use this constructor when the item can't have a different
- /// name in Rust and C++.
- pub fn new(ns: Namespace, ident: Ident) -> Self {
- Self {
- rust: ident.clone(),
- cxx: CppName::new(ns, ident),
- }
- }
-
- /// Use this constructor when attributes such as #[rust_name]
- /// can be used to potentially give a different name in Rust vs C++.
- pub fn new_from_differing_names(ns: Namespace, cxx_ident: Ident, rust_ident: Ident) -> Self {
- Self {
- rust: rust_ident,
- cxx: CppName::new(ns, cxx_ident),
- }
- }
-}
-
-impl ResolvableName {
- pub fn new(ident: Ident) -> Self {
- Self { rust: ident }
- }
-
- pub fn make_self(span: Span) -> Self {
- Self {
- rust: Token.into(),
- }
- }
-
- pub fn is_self(&self) -> bool {
- self.rust == "Self"
- }
-
- pub fn span(&self) -> Span {
- self.rust.span()
- }
-
- pub fn to_symbol(&self, types: &Types) -> Symbol {
- types.resolve(self).to_symbol()
- }
-}
-
-impl Api {
- pub fn get_namespace(&self) -> Option<&Namespace> {
- match self {
- Api::CxxFunction(cfn) => Some(&cfn.ident.cxx.ns),
- Api::CxxType(cty) => Some(&cty.ident.cxx.ns),
- Api::Enum(enm) => Some(&enm.ident.cxx.ns),
- Api::Struct(strct) => Some(&strct.ident.cxx.ns),
- Api::RustType(rty) => Some(&rty.ident.cxx.ns),
- Api::RustFunction(rfn) => Some(&rfn.ident.cxx.ns),
- Api::Impl(_) | Api::Include(_) | Api::TypeAlias(_) => None,
- }
- }
-}
-
-impl CppName {
- pub fn new(ns: Namespace, ident: Ident) -> Self {
- Self { ns, ident }
- }
-
- fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> {
- self.ns.iter().chain(std::iter::once(&self.ident))
- }
-
- fn join(&self, sep: &str) -> String {
- self.iter_all_segments()
- .map(|s| s.to_string())
- .collect::<Vec<_>>()
- .join(sep)
- }
-
- pub fn to_symbol(&self) -> Symbol {
- Symbol::from_idents(self.iter_all_segments())
- }
-
- pub fn to_fully_qualified(&self) -> String {
- format!("::{}", self.join("::"))
- }
-}
diff --git a/syntax/mod.rs b/syntax/mod.rs
index 8d3b8a2..90af17f 100644
--- a/syntax/mod.rs
+++ b/syntax/mod.rs
@@ -11,6 +11,7 @@
pub mod ident;
mod impls;
pub mod mangle;
+mod names;
pub mod namespace;
mod parse;
pub mod qualified;
diff --git a/syntax/names.rs b/syntax/names.rs
new file mode 100644
index 0000000..a61ffc3
--- /dev/null
+++ b/syntax/names.rs
@@ -0,0 +1,72 @@
+use crate::syntax::{CppName, Namespace, Pair, ResolvableName, Symbol, Types};
+use proc_macro2::{Ident, Span};
+use syn::Token;
+
+impl Pair {
+ /// Use this constructor when the item can't have a different
+ /// name in Rust and C++.
+ pub fn new(ns: Namespace, ident: Ident) -> Self {
+ Self {
+ rust: ident.clone(),
+ cxx: CppName::new(ns, ident),
+ }
+ }
+
+ /// Use this constructor when attributes such as #[rust_name]
+ /// can be used to potentially give a different name in Rust vs C++.
+ pub fn new_from_differing_names(ns: Namespace, cxx_ident: Ident, rust_ident: Ident) -> Self {
+ Self {
+ rust: rust_ident,
+ cxx: CppName::new(ns, cxx_ident),
+ }
+ }
+}
+
+impl ResolvableName {
+ pub fn new(ident: Ident) -> Self {
+ Self { rust: ident }
+ }
+
+ pub fn make_self(span: Span) -> Self {
+ Self {
+ rust: Token.into(),
+ }
+ }
+
+ pub fn is_self(&self) -> bool {
+ self.rust == "Self"
+ }
+
+ pub fn span(&self) -> Span {
+ self.rust.span()
+ }
+
+ pub fn to_symbol(&self, types: &Types) -> Symbol {
+ types.resolve(self).to_symbol()
+ }
+}
+
+impl CppName {
+ pub fn new(ns: Namespace, ident: Ident) -> Self {
+ Self { ns, ident }
+ }
+
+ fn iter_all_segments(&self) -> impl Iterator<Item = &Ident> {
+ self.ns.iter().chain(std::iter::once(&self.ident))
+ }
+
+ fn join(&self, sep: &str) -> String {
+ self.iter_all_segments()
+ .map(|s| s.to_string())
+ .collect::<Vec<_>>()
+ .join(sep)
+ }
+
+ pub fn to_symbol(&self) -> Symbol {
+ Symbol::from_idents(self.iter_all_segments())
+ }
+
+ pub fn to_fully_qualified(&self) -> String {
+ format!("::{}", self.join("::"))
+ }
+}
diff --git a/syntax/namespace.rs b/syntax/namespace.rs
index 5eb203a..41a38dd 100644
--- a/syntax/namespace.rs
+++ b/syntax/namespace.rs
@@ -1,4 +1,5 @@
use crate::syntax::qualified::QualifiedName;
+use crate::syntax::Api;
#[cfg(test)]
use proc_macro2::Span;
use quote::IdentFragment;
@@ -79,3 +80,17 @@
self.iter()
}
}
+
+impl Api {
+ pub fn get_namespace(&self) -> Option<&Namespace> {
+ match self {
+ Api::CxxFunction(cfn) => Some(&cfn.ident.cxx.ns),
+ Api::CxxType(cty) => Some(&cty.ident.cxx.ns),
+ Api::Enum(enm) => Some(&enm.ident.cxx.ns),
+ Api::Struct(strct) => Some(&strct.ident.cxx.ns),
+ Api::RustType(rty) => Some(&rty.ident.cxx.ns),
+ Api::RustFunction(rfn) => Some(&rfn.ident.cxx.ns),
+ Api::Impl(_) | Api::Include(_) | Api::TypeAlias(_) => None,
+ }
+ }
+}