ident_ext.rs: New IdentExt trait to have a to_string() method for Ident

I'm taking bets on whether proc_macro2 changes this again.
parent aa2f79de
......@@ -89,7 +89,7 @@ pub fn slot_trait_impls<'ast>(names: &Names, slots: &[Slot<'ast>]) -> Vec<TokenS
let connect_signalname = signals::connect_signalname(signal);
let signalname_trampoline = signals::signal_trampoline_name(signal);
let sig = &signal.sig;
let signalname_str = sig.name.as_ref();
let signalname_str = format!("{}", sig.name);
let input_types = signal.sig.input_arg_types();
let output = &sig.output;
......
......@@ -135,7 +135,7 @@ pub fn set_properties<'ast>(properties: &[Property<'ast>]) -> Vec<TokenStream> {
&format!("set_property_{}", prop.name.to_string().to_lowercase()),
Span::call_site(),
);
let name_str: &str = prop.name.as_ref();
let name_str = prop.name.to_string();
let type_ = prop.type_;
quote_cs! {
......@@ -160,7 +160,7 @@ pub fn get_properties<'ast>(properties: &[Property<'ast>]) -> Vec<TokenStream> {
&format!("get_property_{}", prop.name.to_string().to_lowercase()),
Span::call_site(),
);
let name_str: &str = prop.name.as_ref();
let name_str = prop.name.to_string();
let type_ = prop.type_;
quote_cs! {
......@@ -217,7 +217,7 @@ fn prop_registration<'ast>(properties: &[Property<'ast>]) -> Vec<TokenStream> {
properties
.iter()
.map(|prop| {
let name = prop.name.as_ref();
let name = prop.name.to_string();
let para_spec;
let default;
......
......@@ -85,7 +85,8 @@ pub fn signal_declarations<'ast>(
// FIXME: here we unwrap(); we should check that the
// signal name is valid during the program checking phase
let canonical_signal_name = canonicalize_signal_name(signal.sig.name.as_ref()).unwrap();
let signal_name_str = signal.sig.name.to_string();
let canonical_signal_name = canonicalize_signal_name(&signal_name_str).unwrap();
let signal_name_literal = CStringLiteral(&canonical_signal_name);
assert!(!signal.sig.inputs.is_empty());
......@@ -172,7 +173,7 @@ pub fn signal_trampolines<'ast>(
/// From a signal called `foo`, generate `foo_signal_id`. This is used to
/// store the signal ids from g_signal_newv() in the Class structure.
fn signal_id_name<'ast>(signal: &'ast Signal) -> Ident {
Ident::from(format!("{}_signal_id", signal.sig.name.as_ref()))
Ident::from(format!("{}_signal_id", signal.sig.name))
}
/// From a signal called `foo` generate a `foo_trampoline` identifier. This is used
......@@ -180,20 +181,20 @@ fn signal_id_name<'ast>(signal: &'ast Signal) -> Ident {
pub fn signal_trampoline_name(signal: &Signal) -> Ident {
Ident::from(format!(
"{}_signal_handler_trampoline",
signal.sig.name.as_ref()
signal.sig.name
))
}
/// From a signal called `foo` generate a `connect_foo` identifier. This is used
/// for the public methods in the InstanceExt trait.
pub fn connect_signalname(signal: &Signal) -> Ident {
Ident::from(format!("connect_{}", signal.sig.name.as_ref()))
Ident::from(format!("connect_{}", signal.sig.name))
}
/// From a signal called `foo` generate a `emit_foo` identifier. This is used
/// for the user's implementations of methods.
fn emit_signalname(signal: &Signal) -> Ident {
Ident::from(format!("emit_{}", signal.sig.name.as_ref()))
Ident::from(format!("emit_{}", signal.sig.name))
}
/// Validates a GObject signal name and returns its canonical version
......
......@@ -19,6 +19,7 @@ use super::ast;
use super::checking::*;
use super::errors::*;
use super::glib_utils::*;
use super::ident_ext::IdentExt;
pub struct Program<'ast> {
pub classes: Classes<'ast>,
......@@ -141,7 +142,7 @@ impl<'ast> Ty<'ast> {
Ty::Bool(_) => "gobject_sys::G_TYPE_BOOLEAN",
Ty::Borrowed(_) => unimplemented!(),
Ty::Integer(ref ident) => match ident.as_ref() {
Ty::Integer(ref ident) => match ident.to_string().as_str() {
"i8" => "gobject_sys::G_TYPE_CHAR",
"i16" => unimplemented!("should we promote i16 to i32?"),
"i32" => "gobject_sys::G_TYPE_INT",
......@@ -503,7 +504,7 @@ impl<'ast> Ty<'ast> {
let ident = t.segments.first().unwrap().value().ident;
match ident.as_ref() {
match ident.to_string().as_str() {
"char" => Ok(Ty::Char(ident)),
"bool" => Ok(Ty::Bool(ident)),
"i8" | "i16" | "i32" | "i64" | "isize" | "u8" | "u16" | "u32" | "u64" | "usize" => {
......@@ -688,7 +689,7 @@ pub mod tests {
assert!(program.classes.len() == 1);
let class = program.classes.get(class_name);
assert_eq!(class.name.as_ref(), class_name);
assert_eq!(class.name.to_string(), class_name);
assert_eq!(class.parent.to_string(), superclass_name);
}
......
use std::fmt::Display;
/// Utilities for manipulating `Ident`
pub trait IdentExt: Display {
/// Creates a string out of the ident's name
fn to_string(&self) -> String {
format!("{}", self)
}
}
......@@ -35,6 +35,7 @@ mod errors;
mod gen;
mod glib_utils;
mod hir;
mod ident_ext;
mod param;
mod parser;
......
......@@ -281,6 +281,7 @@ fn keyword<'a>(name: &'static str) -> impl Fn(Cursor<'a>) -> PResult<Ident> {
pub mod tests {
use super::*;
use super::super::ident_ext::IdentExt;
use quote::ToTokens;
use syn::parse_str;
......@@ -305,7 +306,7 @@ pub mod tests {
let raw = "class Foo {}";
let class = parse_str::<ast::Class>(raw).unwrap();
assert_eq!(class.name.as_ref(), "Foo");
assert_eq!(class.name.to_string(), "Foo");
assert!(class.extends.is_none());
}
......@@ -325,7 +326,7 @@ pub mod tests {
let raw = "class Foo: Bar {}";
let class = parse_str::<ast::Class>(raw).unwrap();
assert_eq!(class.name.as_ref(), "Foo");
assert_eq!(class.name.to_string(), "Foo");
assert_tokens_equal(&class.extends, "Bar");
}
......@@ -334,7 +335,7 @@ pub mod tests {
let item = parse_str::<ast::Item>(raw).unwrap();
if let ast::Item::Class(class) = item {
assert_eq!(class.name.as_ref(), "Foo");
assert_eq!(class.name.to_string(), "Foo");
assert!(class.extends.is_none());
} else {
unreachable!();
......@@ -380,6 +381,6 @@ pub mod tests {
let raw = "interface Foo { virtual fn bar(&self); }";
let iface = parse_str::<ast::Interface>(raw).unwrap();
assert_eq!(iface.name.as_ref(), "Foo");
assert_eq!(iface.name.to_string(), "Foo");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment