Skip to content

Commit

Permalink
feat: Migrate to no_std
Browse files Browse the repository at this point in the history
This commit migrates the codebase to use `no_std`, shifting various library imports from `std` to `core` and `alloc`. It also removes `std::sync::Mutex` and a cache intended to store Ether instances. A configuration attribute for disabling `std` in non-test environments has been added in `lib.rs`. All necessary changes to enable compatibility with `no_std` environments have been accounted for.
  • Loading branch information
shuhuiluo committed Jul 9, 2024
1 parent 8e66061 commit 3818a4e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/entities/base_currency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Deref;

use alloy_primitives::ChainId;
use crate::prelude::*;

/// `CurrencyLike` is a generic struct representing a currency with a specific chain ID,
/// decimals, symbol, name, and additional metadata.
Expand Down
15 changes: 1 addition & 14 deletions src/entities/ether.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::prelude::*;

// Lazy static cache for Ether instances
lazy_static! {
static ref ETHER_CACHE: Mutex<FxHashMap<u64, Ether>> = Mutex::new(FxHashMap::default());
}

/// Ether is the main usage of a 'native' currency, i.e., for Ethereum mainnet and all testnets.
/// Represents the native currency of the blockchain.
pub type Ether = CurrencyLike<()>;
Expand Down Expand Up @@ -51,15 +46,7 @@ impl Ether {

/// Retrieves or creates an [`Ether`] instance for the specified chain ID.
pub fn on_chain(chain_id: u64) -> Self {
let mut cache = ETHER_CACHE.lock().unwrap();
match cache.get(&chain_id) {
Some(ether) => ether.clone(),
None => {
let ether = Ether::new(chain_id);
cache.insert(chain_id, ether.clone());
ether
}
}
Self::new(chain_id)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// External crate dependencies
use crate::prelude::*;
use std::ops::{Add, Deref, Mul, Sub};
use core::ops::{Add, Deref, Mul, Sub};

/// Struct representing a fraction with metadata
#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/examples/token_example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// token_example.rs
#![cfg(test)]

// Import the Token struct from the Uniswap SDK-Core
use crate::entities::token::Token;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap
//! decentralized exchange.
//! #![cfg_attr(not(test), no_std)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
Expand All @@ -12,6 +13,7 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
extern crate alloc;

/// Contains functionality related to All Contracts deployed and supported by the Uniswap SDK.
pub mod addresses;
Expand Down
8 changes: 7 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ pub use crate::{
error::Error,
utils::*,
};
pub use alloc::{string::String, vec::Vec};
pub use alloy_primitives::{address, Address};
pub use bigdecimal::{BigDecimal, RoundingMode};
pub use core::{
cmp::Ordering,
num::NonZeroU64,
ops::{Deref, Div},
str::FromStr,
};
pub use lazy_static::lazy_static;
pub use num_bigint::{BigInt, BigUint, ToBigInt, ToBigUint};
pub use num_integer::Integer;
pub use num_traits::{Num, ToPrimitive, Zero};
pub use rustc_hash::FxHashMap;
pub use std::{cmp::Ordering, num::NonZeroU64, ops::Div, str::FromStr, sync::Mutex};
pub use thiserror::Error;

0 comments on commit 3818a4e

Please sign in to comment.