third_party_rust_cxx/syntax/symbol.rs
David Tolnay 7fae55596e
Resolve dead code warnings uncovered by rustc_privacy/rustc_resolve refactor
warning: associated function is never used: `checked_succ`
       --> gen/build/src/syntax/discriminant.rs:183:18
        |
    183 |     pub const fn checked_succ(self) -> Option<Self> {
        |                  ^^^^^^^^^^^^
        |
        = note: `#[warn(dead_code)]` on by default

    warning: associated function is never used: `ty`
       --> gen/build/src/syntax/tokens.rs:305:12
        |
    305 |     pub fn ty(&self) -> ReceiverType {
        |            ^^

    warning: associated function is never used: `ty_self`
       --> gen/build/src/syntax/tokens.rs:310:12
        |
    310 |     pub fn ty_self(&self) -> ReceiverTypeSelf {
        |            ^^^^^^^

    warning: variant is never constructed: `Foreign`
       --> gen/build/src/syntax/mod.rs:122:5
        |
    122 |     Foreign { rust_type: Path },
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

    warning: associated function is never used: `checked_succ`
       --> macro/src/syntax/discriminant.rs:183:18
        |
    183 |     pub const fn checked_succ(self) -> Option<Self> {
        |                  ^^^^^^^^^^^^
        |
        = note: `#[warn(dead_code)]` on by default

    warning: associated function is never used: `is_empty`
      --> macro/src/syntax/doc.rs:22:12
       |
    22 |     pub fn is_empty(&self) -> bool {
       |            ^^^^^^^^

    warning: associated function is never used: `to_string`
      --> macro/src/syntax/doc.rs:26:12
       |
    26 |     pub fn to_string(&self) -> String {
       |            ^^^^^^^^^

    warning: associated function is never used: `remove`
       --> macro/src/syntax/map.rs:127:16
        |
    127 |         pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
        |                ^^^^^^

    warning: associated function is never used: `to_fully_qualified`
      --> macro/src/syntax/names.rs:24:12
       |
    24 |     pub fn to_fully_qualified(&self) -> String {
       |            ^^^^^^^^^^^^^^^^^^

    warning: associated function is never used: `prefix_with`
      --> macro/src/syntax/symbol.rs:44:12
       |
    44 |     pub fn prefix_with(&self, prefix: &str) -> Symbol {
       |            ^^^^^^^^^^^

    warning: variant is never constructed: `Foreign`
       --> macro/src/syntax/mod.rs:122:5
        |
    122 |     Foreign { rust_type: Path },
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2022-01-10 18:50:49 -08:00

111 lines
2.5 KiB
Rust

use crate::syntax::namespace::Namespace;
use crate::syntax::{ForeignName, Pair};
use proc_macro2::{Ident, TokenStream};
use quote::ToTokens;
use std::fmt::{self, Display, Write};
// A mangled symbol consisting of segments separated by '$'.
// For example: cxxbridge1$string$new
pub struct Symbol(String);
impl Display for Symbol {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, formatter)
}
}
impl ToTokens for Symbol {
fn to_tokens(&self, tokens: &mut TokenStream) {
ToTokens::to_tokens(&self.0, tokens);
}
}
impl Symbol {
fn push(&mut self, segment: &dyn Display) {
let len_before = self.0.len();
if !self.0.is_empty() {
self.0.push('$');
}
self.0.write_fmt(format_args!("{}", segment)).unwrap();
assert!(self.0.len() > len_before);
}
pub fn from_idents<'a>(it: impl Iterator<Item = &'a dyn Segment>) -> Self {
let mut symbol = Symbol(String::new());
for segment in it {
segment.write(&mut symbol);
}
assert!(!symbol.0.is_empty());
symbol
}
}
pub trait Segment {
fn write(&self, symbol: &mut Symbol);
}
impl Segment for str {
fn write(&self, symbol: &mut Symbol) {
symbol.push(&self);
}
}
impl Segment for usize {
fn write(&self, symbol: &mut Symbol) {
symbol.push(&self);
}
}
impl Segment for Ident {
fn write(&self, symbol: &mut Symbol) {
symbol.push(&self);
}
}
impl Segment for Symbol {
fn write(&self, symbol: &mut Symbol) {
symbol.push(&self);
}
}
impl Segment for Namespace {
fn write(&self, symbol: &mut Symbol) {
for segment in self {
symbol.push(segment);
}
}
}
impl Segment for Pair {
fn write(&self, symbol: &mut Symbol) {
self.namespace.write(symbol);
self.cxx.write(symbol);
}
}
impl Segment for ForeignName {
fn write(&self, symbol: &mut Symbol) {
// TODO: support C++ names containing whitespace (`unsigned int`) or
// non-alphanumeric characters (`operator++`).
self.to_string().write(symbol);
}
}
impl<T> Segment for &'_ T
where
T: ?Sized + Segment + Display,
{
fn write(&self, symbol: &mut Symbol) {
(**self).write(symbol);
}
}
pub fn join(segments: &[&dyn Segment]) -> Symbol {
let mut symbol = Symbol(String::new());
for segment in segments {
segment.write(&mut symbol);
}
assert!(!symbol.0.is_empty());
symbol
}