Skip to content

Commit

Permalink
refactor: use Self for constructors and conversions (#85)
Browse files Browse the repository at this point in the history
This change replaces explicit struct names with `Self` in constructor and conversion implementations. It improves readability and follows best practices for Rust's self-referential code. The update also enables the `clippy::use_self` lint to enforce these changes.
  • Loading branch information
shuhuiluo authored Oct 2, 2024
1 parent 72bdf2f commit f88f0aa
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl From<i32> for FeeAmount {
impl From<FeeAmount> for U24 {
#[inline]
fn from(fee: FeeAmount) -> Self {
U24::from_limbs([fee as u64])
Self::from_limbs([fee as u64])
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl<TP: Clone + TickDataProvider> Pool<TP> {
};
Ok((
CurrencyAmount::from_raw_amount(output_token, -output_amount.to_big_int())?,
Pool::new_with_tick_data_provider(
Self::new_with_tick_data_provider(
self.token0.clone(),
self.token1.clone(),
self.fee,
Expand Down Expand Up @@ -484,7 +484,7 @@ impl<TP: Clone + TickDataProvider> Pool<TP> {
};
Ok((
CurrencyAmount::from_raw_amount(input_token, input_amount.to_big_int())?,
Pool::new_with_tick_data_provider(
Self::new_with_tick_data_provider(
self.token0.clone(),
self.token1.clone(),
self.fee,
Expand Down
2 changes: 1 addition & 1 deletion src/entities/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
}
assert!(current_input_token.equals(wrapped_output), "PATH");

Route {
Self {
pools,
input,
output,
Expand Down
4 changes: 2 additions & 2 deletions src/entities/tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl TickIndex for i32 {
}

impl<const BITS: usize, const LIMBS: usize> TickIndex for Signed<BITS, LIMBS> {
const ZERO: Self = Signed::ZERO;
const ONE: Self = Signed::ONE;
const ZERO: Self = Self::ZERO;
const ONE: Self = Self::ONE;

#[inline]
fn from_i24(value: I24) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ where
input_amount: CurrencyAmount<TInput>,
output_amount: CurrencyAmount<TOutput>,
) -> Self {
Swap {
Self {
route,
input_amount,
output_amount,
Expand Down Expand Up @@ -189,7 +189,7 @@ where
}
}
assert_eq!(num_pools, pool_address_set.len(), "POOLS_DUPLICATED");
Ok(Trade {
Ok(Self {
swaps,
trade_type,
_input_amount: None,
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ pub enum Error {
impl From<CoreError> for Error {
#[inline]
fn from(error: CoreError) -> Self {
Error::Core(error)
Self::Core(error)
}
}

#[cfg(feature = "extensions")]
impl From<ContractError> for Error {
#[inline]
fn from(error: ContractError) -> Self {
Error::ContractError(error)
Self::ContractError(error)
}
}

#[cfg(feature = "extensions")]
impl From<LensError> for Error {
#[inline]
fn from(error: LensError) -> Self {
Error::LensError(error)
Self::LensError(error)
}
}
4 changes: 2 additions & 2 deletions src/extensions/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Position {
slot0.sqrtPriceX96,
active_liquidity,
)?;
Ok(Position::new(
Ok(Self::new(
pool,
position.liquidity,
position.tickLower.as_i32(),
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<I: TickIndex> Position<EphemeralTickMapDataProvider<I>> {
pool.liquidity,
tick_data_provider,
)?;
Ok(Position::new(
Ok(Self::new(
pool,
position.liquidity,
position.tick_lower.try_into().unwrap(),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
clippy::unseparated_literal_suffix,
clippy::unused_self,
clippy::use_debug,
clippy::use_self,
rustdoc::all
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
Expand Down

0 comments on commit f88f0aa

Please sign in to comment.