diff --git a/pilota-build/src/middle/context.rs b/pilota-build/src/middle/context.rs index 2e78a69c..9ca36c53 100644 --- a/pilota-build/src/middle/context.rs +++ b/pilota-build/src/middle/context.rs @@ -471,7 +471,18 @@ impl Context { .map(|(k, v)| { let k = self.lit_into_ty(k, k_ty)?.0; let v = self.lit_into_ty(v, v_ty)?.0; - anyhow::Ok(format!("map.insert({k}.clone(), {v}.clone());")) + let clone_if_needed = |ty: &CodegenTy| { + if ty.should_lazy_static() { + ".clone()" + } else { + "" + } + }; + anyhow::Ok(format!( + "map.insert({k}{}, {v}{});", + clone_if_needed(k_ty), + clone_if_needed(v_ty), + )) }) .try_collect::<_, Vec<_>, _>()? .join(""); diff --git a/pilota-build/test_data/thrift/const_val.rs b/pilota-build/test_data/thrift/const_val.rs index 1fc0d85e..9ef06ead 100644 --- a/pilota-build/test_data/thrift/const_val.rs +++ b/pilota-build/test_data/thrift/const_val.rs @@ -96,7 +96,7 @@ pub mod const_val { ::pilota::lazy_static::lazy_static! { pub static ref TEST_MAP_LIST: ::pilota::AHashMap> = { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(1i32.clone(), ::std::vec!["hello"].clone()); + map.insert(1i32, ::std::vec!["hello"].clone()); map }; } @@ -104,7 +104,7 @@ pub mod const_val { ::pilota::lazy_static::lazy_static! { pub static ref TEST_MAP: ::pilota::AHashMap = { let mut map = ::pilota::AHashMap::with_capacity(2); - map.insert(Index::A.clone(), "hello".clone());map.insert(Index::B.clone(), "world".clone()); + map.insert(Index::A, "hello");map.insert(Index::B, "world"); map }; } diff --git a/pilota-build/test_data/thrift/default_value.rs b/pilota-build/test_data/thrift/default_value.rs index 38bf87d9..2d00a050 100644 --- a/pilota-build/test_data/thrift/default_value.rs +++ b/pilota-build/test_data/thrift/default_value.rs @@ -285,7 +285,7 @@ pub mod default_value { empty: ::pilota::Bytes::from_static("".as_bytes()), test_map: { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }, test_set: ::pilota::AHashSet::from([::pilota::OrderedFloat(1f64)]), @@ -595,7 +595,7 @@ pub mod default_value { } let test_map = test_map.unwrap_or_else(|| { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }); let test_set = test_set @@ -833,7 +833,7 @@ pub mod default_value { } let test_map = test_map.unwrap_or_else(|| { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }); let test_set = test_set.unwrap_or_else(|| { diff --git a/pilota-build/test_data/thrift/enum_map.rs b/pilota-build/test_data/thrift/enum_map.rs index 9a64b6c9..0ed743d7 100644 --- a/pilota-build/test_data/thrift/enum_map.rs +++ b/pilota-build/test_data/thrift/enum_map.rs @@ -70,7 +70,7 @@ pub mod enum_map { ::pilota::lazy_static::lazy_static! { pub static ref TYPE_A_MAP: ::pilota::AHashMap = { let mut map = ::pilota::AHashMap::with_capacity(2); - map.insert(TYPE_B1.clone(), TYPE_A1.clone());map.insert(TYPE_B2.clone(), TYPE_A2.clone()); + map.insert(TYPE_B1, TYPE_A1.clone());map.insert(TYPE_B2, TYPE_A2.clone()); map }; } diff --git a/pilota-build/test_data/thrift/multi.rs b/pilota-build/test_data/thrift/multi.rs index a1bcbfd8..6d85c378 100644 --- a/pilota-build/test_data/thrift/multi.rs +++ b/pilota-build/test_data/thrift/multi.rs @@ -287,7 +287,7 @@ pub mod multi { empty: ::pilota::Bytes::from_static("".as_bytes()), test_map: { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }, test_set: ::pilota::AHashSet::from([::pilota::OrderedFloat(1f64)]), @@ -597,7 +597,7 @@ pub mod multi { } let test_map = test_map.unwrap_or_else(|| { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }); let test_set = test_set @@ -835,7 +835,7 @@ pub mod multi { } let test_map = test_map.unwrap_or_else(|| { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(::pilota::OrderedFloat(1f64).clone(), 2f64.clone()); + map.insert(::pilota::OrderedFloat(1f64), 2f64); map }); let test_set = test_set.unwrap_or_else(|| { diff --git a/pilota-build/test_data/unknown_fields.rs b/pilota-build/test_data/unknown_fields.rs index fde003c1..facd7643 100644 --- a/pilota-build/test_data/unknown_fields.rs +++ b/pilota-build/test_data/unknown_fields.rs @@ -1640,7 +1640,7 @@ pub mod unknown_fields { ::pilota::lazy_static::lazy_static! { pub static ref TEST_MAP_LIST: ::pilota::AHashMap> = { let mut map = ::pilota::AHashMap::with_capacity(1); - map.insert(1i32.clone(), ::std::vec!["hello"].clone()); + map.insert(1i32, ::std::vec!["hello"].clone()); map }; } @@ -3585,7 +3585,7 @@ pub mod unknown_fields { ::pilota::lazy_static::lazy_static! { pub static ref TEST_MAP: ::pilota::AHashMap = { let mut map = ::pilota::AHashMap::with_capacity(2); - map.insert(Index::A.clone(), "hello".clone());map.insert(Index::B.clone(), "world".clone()); + map.insert(Index::A, "hello");map.insert(Index::B, "world"); map }; }