Avoid lossy conversions from ConvertedType to ConvertedType through String

This commit is contained in:
Kartikaya Gupta
2017-04-05 14:04:59 -04:00
parent dfcbadc0b8
commit e25edb5303
+15 -18
View File
@@ -10,7 +10,7 @@ use syn::*;
mod rust_lib;
#[derive(Debug)]
#[derive(Debug, Clone)]
struct ConvertedType {
/// The type converted to C (e.g. `uint32_t`)
prefix: String,
@@ -19,11 +19,16 @@ struct ConvertedType {
}
impl ConvertedType {
fn new<T: Into<String>>(pre: T, post: String) -> Self {
ConvertedType {
prefix: pre.into(),
postfix: post
}
fn append_prefix(&self, str: &str) -> ConvertedType {
let mut clone = self.clone();
clone.prefix.push_str(str);
clone
}
fn append_postfix(&self, str: &str) -> ConvertedType {
let mut clone = self.clone();
clone.postfix.push_str(str);
clone
}
fn format_with_ident(&self, ident: &Ident) -> String {
@@ -31,14 +36,6 @@ impl ConvertedType {
}
}
impl Into<String> for ConvertedType {
fn into(self) -> String {
let mut str = String::from(self.prefix);
str.push_str(&self.postfix);
str
}
}
impl From<String> for ConvertedType {
fn from(str: String) -> ConvertedType {
ConvertedType {
@@ -103,7 +100,7 @@ fn map_path(p: &Path) -> ConvertedType {
if let PathParameters::AngleBracketed(ref d) = p.segments[0].parameters {
let template_args = d.types
.iter()
.map(|ty| map_ty(ty).into())
.map(|ty| map_ty(ty).to_string())
.collect::<Vec<String>>()
.join(", ");
if !template_args.is_empty() {
@@ -122,9 +119,9 @@ fn map_mut_ty(mut_ty: &MutTy) -> ConvertedType {
fn map_ty(ty: &Ty) -> ConvertedType {
match ty {
&Ty::Path(_, ref p) => map_path(p),
&Ty::Ptr(ref p) => ConvertedType::from(format!("{}*", map_ty(&p.ty))),
&Ty::Rptr(_, ref mut_ty) => ConvertedType::from(format!("{}*", map_mut_ty(mut_ty))),
&Ty::Array(ref p, ConstExpr::Lit(Lit::Int(sz, _))) => ConvertedType::new(map_ty(&p), format!("[{}]", sz)),
&Ty::Ptr(ref p) => map_ty(&p.ty).append_prefix("*"),
&Ty::Rptr(_, ref mut_ty) => map_mut_ty(mut_ty).append_prefix("*"),
&Ty::Array(ref p, ConstExpr::Lit(Lit::Int(sz, _))) => map_ty(&p).append_postfix(&format!("[{}]", sz)),
_ => ConvertedType::from(format!("unknown {:?}", ty)),
}
}