Skip to content

Commit

Permalink
rm useless clone for const or temporary var in map
Browse files Browse the repository at this point in the history
  • Loading branch information
WUST-mengqinyu committed Jul 8, 2024
1 parent f56f90e commit 06c68ce
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
13 changes: 12 additions & 1 deletion pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
4 changes: 2 additions & 2 deletions pilota-build/test_data/thrift/const_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ pub mod const_val {
::pilota::lazy_static::lazy_static! {
pub static ref TEST_MAP_LIST: ::pilota::AHashMap<i32, ::std::vec::Vec<&'static str>> = {
let mut map = ::pilota::AHashMap::with_capacity(1);
map.insert(1i32.clone(), ::std::vec!["hello"].clone());
map.insert(1i32, ::std::vec!["hello"].clone());
map
};
}
pub const TEST_LIST: [&'static str; 2] = ["hello", "world"];
::pilota::lazy_static::lazy_static! {
pub static ref TEST_MAP: ::pilota::AHashMap<Index, &'static str> = {
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
};
}
Expand Down
6 changes: 3 additions & 3 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(|| {
Expand Down
2 changes: 1 addition & 1 deletion pilota-build/test_data/thrift/enum_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub mod enum_map {
::pilota::lazy_static::lazy_static! {
pub static ref TYPE_A_MAP: ::pilota::AHashMap<TypeB, TypeA> = {
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
};
}
Expand Down
6 changes: 3 additions & 3 deletions pilota-build/test_data/thrift/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(|| {
Expand Down
4 changes: 2 additions & 2 deletions pilota-build/test_data/unknown_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ pub mod unknown_fields {
::pilota::lazy_static::lazy_static! {
pub static ref TEST_MAP_LIST: ::pilota::AHashMap<i32, ::std::vec::Vec<&'static str>> = {
let mut map = ::pilota::AHashMap::with_capacity(1);
map.insert(1i32.clone(), ::std::vec!["hello"].clone());
map.insert(1i32, ::std::vec!["hello"].clone());
map
};
}
Expand Down Expand Up @@ -3585,7 +3585,7 @@ pub mod unknown_fields {
::pilota::lazy_static::lazy_static! {
pub static ref TEST_MAP: ::pilota::AHashMap<Index, &'static str> = {
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
};
}
Expand Down

0 comments on commit 06c68ce

Please sign in to comment.