Skip to content

Commit

Permalink
Variety of small clippy inspired refactors (#1281)
Browse files Browse the repository at this point in the history
### What
Variety of small clippy inspired refactors

### Why
Some are meaningful, some are clippy opinionated. Just a little house
keeping.
  • Loading branch information
leighmcculloch authored Jun 11, 2024
1 parent 4d26520 commit 53e99f5
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
6 changes: 3 additions & 3 deletions soroban-sdk-macros/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn derive_type_enum(
enum_ident,
&case_num_lit,
&case_name_str_lit,
&case_name,
case_name,
case_ident,
&variant.attrs,
&variant.fields,
Expand All @@ -114,7 +114,7 @@ pub fn derive_type_enum(
enum_ident,
&case_num_lit,
&case_name_str_lit,
&case_name,
case_name,
case_ident,
&variant.attrs,
);
Expand Down Expand Up @@ -378,7 +378,7 @@ fn map_tuple_variant(
ScSpecUdtUnionCaseV0::TupleV0(ScSpecUdtUnionCaseTupleV0 {
doc: docs_from_attrs(attrs),
name: case_name.try_into().unwrap_or_else(|_| StringM::default()),
type_: field_types.try_into().unwrap(),
type_: field_types,
})
};

Expand Down
7 changes: 2 additions & 5 deletions soroban-sdk-macros/src/derive_enum_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn derives_copy(attrs: &[Attribute]) -> bool {
if let Some(_tt) = ml.tokens.clone().into_iter().find(|tt| {
// match this token with what we want
if let proc_macro2::TokenTree::Ident(id) = tt {
id.to_string() == "Copy"
id == "Copy"
} else {
false
}
Expand Down Expand Up @@ -48,10 +48,7 @@ pub fn derive_type_enum_int(
if !derives_copy(attrs) {
errors.push(Error::new(
enum_ident.span(),
format!(
"enum integer {} must have `derive(Copy)`",
enum_ident.to_string()
),
format!("enum integer {enum_ident} must have `derive(Copy)`"),
));
}

Expand Down
15 changes: 7 additions & 8 deletions soroban-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use proc_macro::TokenStream;
use proc_macro2::{Literal, Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use sha2::{Digest, Sha256};
use std::fs;
use std::{fmt::Write, fs};
use syn::{
parse_macro_input, parse_str, spanned::Spanned, Data, DeriveInput, Error, Fields, ItemImpl,
ItemStruct, LitStr, Path, Type, Visibility,
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
let ident = &m.sig.ident;
let call = quote! { <super::#ty>::#ident };
derive_pub_fn(
&crate_path,
crate_path,
&call,
ident,
&m.attrs,
Expand All @@ -246,7 +246,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
match derived {
Ok(derived_ok) => {
let cfs = derive_contract_function_registration_ctor(
&crate_path,
crate_path,
ty,
trait_ident,
pub_methods.into_iter(),
Expand Down Expand Up @@ -346,11 +346,10 @@ pub fn contractmeta(metadata: TokenStream) -> TokenStream {

let ident = format_ident!(
"__CONTRACT_KEY_{}",
args.key
.as_bytes()
.iter()
.map(|b| format!("{b:02X}"))
.collect::<String>()
args.key.as_bytes().iter().fold(String::new(), |mut s, b| {
let _ = write!(s, "{b:02x}");
s
})
);
quote! {
#[doc(hidden)]
Expand Down
12 changes: 6 additions & 6 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ impl<const N: usize> IntoVal<Env, BytesN<N>> for Hash<N> {
}
}

impl<const N: usize> Into<Bytes> for Hash<N> {
fn into(self) -> Bytes {
self.0.into()
impl<const N: usize> From<Hash<N>> for Bytes {
fn from(v: Hash<N>) -> Self {
v.0.into()
}
}

impl<const N: usize> Into<BytesN<N>> for Hash<N> {
fn into(self) -> BytesN<N> {
self.0
impl<const N: usize> From<Hash<N>> for BytesN<N> {
fn from(v: Hash<N>) -> Self {
v.0
}
}

Expand Down
12 changes: 4 additions & 8 deletions soroban-sdk/src/prng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl Prng {
where
T: Shuffle,
{
v.shuffle(&self);
v.shuffle(self);
}
}

Expand Down Expand Up @@ -454,9 +454,7 @@ impl Fill for u64 {
impl Gen for u64 {
fn gen(prng: &Prng) -> Self {
let env = prng.env();
internal::Env::prng_u64_in_inclusive_range(env, u64::MIN.into(), u64::MAX.into())
.unwrap_infallible()
.into()
internal::Env::prng_u64_in_inclusive_range(env, u64::MIN, u64::MAX).unwrap_infallible()
}
}

Expand All @@ -475,9 +473,7 @@ impl GenRange for u64 {
Bound::Excluded(b) => *b - 1,
Bound::Unbounded => u64::MAX,
};
internal::Env::prng_u64_in_inclusive_range(env, start_bound.into(), end_bound.into())
.unwrap_infallible()
.into()
internal::Env::prng_u64_in_inclusive_range(env, start_bound, end_bound).unwrap_infallible()
}
}

Expand All @@ -489,7 +485,7 @@ impl Fill for Bytes {
/// If the length of Bytes is greater than u32::MAX in length.
fn fill(&mut self, prng: &Prng) {
let env = prng.env();
let len: u32 = self.len().try_into().unwrap_optimized();
let len: u32 = self.len();
let obj = internal::Env::prng_bytes_new(env, len.into()).unwrap_infallible();
*self = unsafe { Bytes::unchecked_new(env.clone(), obj) };
}
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Storage {
V: TryFromVal<Env, Val>,
{
let key = key.into_val(&self.env);
if self.has_internal(key, storage_type.clone()) {
if self.has_internal(key, storage_type) {
let rv = self.get_internal(key, storage_type);
Some(V::try_from_val(&self.env, &rv).unwrap_optimized())
} else {
Expand Down
5 changes: 5 additions & 0 deletions soroban-sdk/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ impl String {
self.env().string_len(self.obj).unwrap_infallible().into()
}

#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Copy the bytes in [String] into the given slice.
///
/// ### Panics
Expand Down
10 changes: 5 additions & 5 deletions soroban-sdk/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl<T> Clone for Vec<T> {
fn clone(&self) -> Self {
Self {
env: self.env.clone(),
obj: self.obj.clone(),
_t: self._t.clone(),
obj: self.obj,
_t: self._t,
}
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<T> TryFromVal<Env, Vec<T>> for Vec<Val> {
type Error = Infallible;

fn try_from_val(env: &Env, v: &Vec<T>) -> Result<Self, Self::Error> {
Ok(unsafe { Vec::unchecked_new(env.clone(), v.obj.clone()) })
Ok(unsafe { Vec::unchecked_new(env.clone(), v.obj) })
}
}

Expand All @@ -169,7 +169,7 @@ impl<T> TryFromVal<Env, &Vec<Val>> for Vec<T> {
type Error = Infallible;

fn try_from_val(env: &Env, v: &&Vec<Val>) -> Result<Self, Self::Error> {
Ok(unsafe { Vec::unchecked_new(env.clone(), v.obj.clone()) })
Ok(unsafe { Vec::unchecked_new(env.clone(), v.obj) })
}
}

Expand All @@ -181,7 +181,7 @@ where

#[inline(always)]
fn try_from_val(env: &Env, obj: &VecObject) -> Result<Self, Self::Error> {
Ok(unsafe { Vec::<T>::unchecked_new(env.clone(), obj.clone()) })
Ok(unsafe { Vec::<T>::unchecked_new(env.clone(), *obj) })
}
}

Expand Down

0 comments on commit 53e99f5

Please sign in to comment.