From e25edb53036e90a27d3c671174e37e2b3baba6e4 Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Wed, 5 Apr 2017 14:04:59 -0400 Subject: [PATCH] Avoid lossy conversions from ConvertedType to ConvertedType through String --- src/main.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1772b45..28aba8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>(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 for ConvertedType { - fn into(self) -> String { - let mut str = String::from(self.prefix); - str.push_str(&self.postfix); - str - } -} - impl From 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::>() .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)), } }