diff --git a/juniper_codegen/src/derive_input_object.rs b/juniper_codegen/src/derive_input_object.rs index f5d3c173..512b6121 100644 --- a/juniper_codegen/src/derive_input_object.rs +++ b/juniper_codegen/src/derive_input_object.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use syn; use syn::*; use quote::Tokens; @@ -110,7 +112,7 @@ pub fn impl_input_object(ast: &syn::DeriveInput) -> Tokens { let name = match field_attrs.name { Some(ref name) => { // Custom name specified. - name.to_string() + Cow::Borrowed(name.as_ref()) } None => { // Note: auto camel casing when no custom name specified. diff --git a/juniper_codegen/src/derive_object.rs b/juniper_codegen/src/derive_object.rs index 51b0dcd4..fd1e6c26 100644 --- a/juniper_codegen/src/derive_object.rs +++ b/juniper_codegen/src/derive_object.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use syn; use syn::*; use quote::Tokens; @@ -106,7 +108,7 @@ pub fn impl_object(ast: &syn::DeriveInput) -> Tokens { let name = match field_attrs.name { Some(ref name) => { // Custom name specified. - name.to_string() + Cow::Borrowed(name.as_ref()) } None => { // Note: auto camel casing when no custom name specified. diff --git a/juniper_codegen/src/util.rs b/juniper_codegen/src/util.rs index a3096c7b..c10a5050 100644 --- a/juniper_codegen/src/util.rs +++ b/juniper_codegen/src/util.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use syn::*; pub fn get_graphl_attr(attrs: &Vec) -> Option<&Vec> { @@ -44,12 +46,12 @@ pub fn keyed_item_value(item: &NestedMetaItem, name: &str, must_be_string: bool) // Note: duplicated from juniper crate! #[doc(hidden)] -pub fn to_camel_case(s: &str) -> String { - let mut dest = String::new(); +pub fn to_camel_case<'a>(s: &'a str) -> Cow<'a, str> { + let mut dest = Cow::Borrowed(s); for (i, part) in s.split('_').enumerate() { if i > 0 && part.len() == 1 { - dest.push_str(&part.to_uppercase()); + dest += Cow::Owned(part.to_uppercase()); } else if i > 0 && part.len() > 1 { let first = part.chars() .next() @@ -58,10 +60,10 @@ pub fn to_camel_case(s: &str) -> String { .collect::(); let second = &part[1..]; - dest.push_str(&first); - dest.push_str(second); + dest += Cow::Owned(first); + dest += second; } else if i == 0 { - dest.push_str(part); + dest = Cow::Borrowed(part); } }