Skip to content

Commit 2a0cfd9

Browse files
authored
Merge pull request #1776 from mina86/a
cosmwasm_std: prefer core and alloc modules to prepare for no_std
2 parents aca2de1 + 7fc21c3 commit 2a0cfd9

38 files changed

+165
-159
lines changed

packages/std/src/addresses.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use alloc::borrow::Cow;
2+
use core::fmt;
3+
use core::ops::Deref;
14
use schemars::JsonSchema;
25
use serde::{Deserialize, Serialize};
36
use sha2::{
47
digest::{Digest, Update},
58
Sha256,
69
};
7-
use std::borrow::Cow;
8-
use std::fmt;
9-
use std::ops::Deref;
1010
use thiserror::Error;
1111

1212
use crate::{binary::Binary, forward_ref_partial_eq, HexBinary};
@@ -401,7 +401,6 @@ mod tests {
401401
use super::*;
402402
use crate::{assert_hash_works, HexBinary};
403403
use hex_literal::hex;
404-
use std::collections::HashSet;
405404

406405
#[test]
407406
fn addr_unchecked_works() {
@@ -657,6 +656,8 @@ mod tests {
657656
/// This requires Hash and Eq to be implemented
658657
#[test]
659658
fn canonical_addr_can_be_used_in_hash_set() {
659+
use std::collections::HashSet;
660+
660661
let alice1 = CanonicalAddr::from([0, 187, 61, 11, 250, 0]);
661662
let alice2 = CanonicalAddr::from([0, 187, 61, 11, 250, 0]);
662663
let bob = CanonicalAddr::from([16, 21, 33, 0, 255, 9]);

packages/std/src/assertions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
macro_rules! ensure {
3131
($cond:expr, $e:expr) => {
3232
if !($cond) {
33-
return Err(std::convert::From::from($e));
33+
return Err(core::convert::From::from($e));
3434
}
3535
};
3636
}
@@ -68,7 +68,7 @@ macro_rules! ensure_eq {
6868
($a:expr, $b:expr, $e:expr) => {
6969
// Not implemented via `ensure!` because the caller would have to import both macros.
7070
if !($a == $b) {
71-
return Err(std::convert::From::from($e));
71+
return Err(core::convert::From::from($e));
7272
}
7373
};
7474
}
@@ -100,7 +100,7 @@ macro_rules! ensure_ne {
100100
($a:expr, $b:expr, $e:expr) => {
101101
// Not implemented via `ensure!` because the caller would have to import both macros.
102102
if !($a != $b) {
103-
return Err(std::convert::From::from($e));
103+
return Err(core::convert::From::from($e));
104104
}
105105
};
106106
}

packages/std/src/binary.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::fmt;
2-
use std::ops::Deref;
1+
use core::fmt;
2+
use core::ops::Deref;
33

44
use schemars::JsonSchema;
55
use serde::{de, ser, Deserialize, Deserializer, Serialize};
@@ -135,15 +135,15 @@ impl From<Binary> for Vec<u8> {
135135
}
136136
}
137137

138-
/// Implement `encoding::Binary == std::vec::Vec<u8>`
138+
/// Implement `encoding::Binary == alloc::vec::Vec<u8>`
139139
impl PartialEq<Vec<u8>> for Binary {
140140
fn eq(&self, rhs: &Vec<u8>) -> bool {
141141
// Use Vec<u8> == Vec<u8>
142142
self.0 == *rhs
143143
}
144144
}
145145

146-
/// Implement `std::vec::Vec<u8> == encoding::Binary`
146+
/// Implement `alloc::vec::Vec<u8> == encoding::Binary`
147147
impl PartialEq<Binary> for Vec<u8> {
148148
fn eq(&self, rhs: &Binary) -> bool {
149149
// Use Vec<u8> == Vec<u8>
@@ -241,7 +241,6 @@ mod tests {
241241
use crate::assert_hash_works;
242242
use crate::errors::StdError;
243243
use crate::serde::{from_slice, to_vec};
244-
use std::collections::HashSet;
245244

246245
#[test]
247246
fn encode_decode() {
@@ -520,6 +519,8 @@ mod tests {
520519
/// This requires Hash and Eq to be implemented
521520
#[test]
522521
fn binary_can_be_used_in_hash_set() {
522+
use std::collections::HashSet;
523+
523524
let a1 = Binary::from([0, 187, 61, 11, 250, 0]);
524525
let a2 = Binary::from([0, 187, 61, 11, 250, 0]);
525526
let b = Binary::from([16, 21, 33, 0, 255, 9]);

packages/std/src/coin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use core::{fmt, str::FromStr};
12
use schemars::JsonSchema;
23
use serde::{Deserialize, Serialize};
3-
use std::{fmt, str::FromStr};
44

55
use crate::{errors::CoinFromStrError, math::Uint128};
66

packages/std/src/coins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::collections::BTreeMap;
2-
use std::fmt;
3-
use std::str::FromStr;
1+
use alloc::collections::BTreeMap;
2+
use core::fmt;
3+
use core::str::FromStr;
44

55
use crate::{errors::CoinsError, Coin, StdError, StdResult, Uint128};
66
use crate::{OverflowError, OverflowOperation};

packages/std/src/deps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::marker::PhantomData;
1+
use core::marker::PhantomData;
22

33
use crate::query::CustomQuery;
44
use crate::results::Empty;

packages/std/src/errors/recover_pubkey_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use core::fmt::Debug;
12
#[cfg(not(target_arch = "wasm32"))]
23
use cosmwasm_crypto::CryptoError;
34
#[cfg(feature = "backtraces")]
45
use std::backtrace::Backtrace;
5-
use std::fmt::Debug;
66
use thiserror::Error;
77

88
#[derive(Error, Debug)]

packages/std/src/errors/std_error.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use core::fmt;
12
#[cfg(feature = "backtraces")]
23
use std::backtrace::Backtrace;
3-
use std::fmt;
44
use thiserror::Error;
55

66
use crate::errors::{RecoverPubkeyError, VerificationError};
@@ -433,14 +433,14 @@ impl PartialEq<StdError> for StdError {
433433
}
434434
}
435435

436-
impl From<std::str::Utf8Error> for StdError {
437-
fn from(source: std::str::Utf8Error) -> Self {
436+
impl From<core::str::Utf8Error> for StdError {
437+
fn from(source: core::str::Utf8Error) -> Self {
438438
Self::invalid_utf8(source)
439439
}
440440
}
441441

442-
impl From<std::string::FromUtf8Error> for StdError {
443-
fn from(source: std::string::FromUtf8Error) -> Self {
442+
impl From<alloc::string::FromUtf8Error> for StdError {
443+
fn from(source: alloc::string::FromUtf8Error) -> Self {
444444
Self::invalid_utf8(source)
445445
}
446446
}
@@ -517,7 +517,7 @@ impl OverflowError {
517517
/// The error returned by [`TryFrom`] conversions that overflow, for example
518518
/// when converting from [`Uint256`] to [`Uint128`].
519519
///
520-
/// [`TryFrom`]: std::convert::TryFrom
520+
/// [`TryFrom`]: core::convert::TryFrom
521521
/// [`Uint256`]: crate::Uint256
522522
/// [`Uint128`]: crate::Uint128
523523
#[derive(Error, Debug, PartialEq, Eq)]
@@ -618,11 +618,11 @@ pub enum CoinFromStrError {
618618
#[error("Missing amount or non-digit characters in amount")]
619619
MissingAmount,
620620
#[error("Invalid amount: {0}")]
621-
InvalidAmount(std::num::ParseIntError),
621+
InvalidAmount(core::num::ParseIntError),
622622
}
623623

624-
impl From<std::num::ParseIntError> for CoinFromStrError {
625-
fn from(value: std::num::ParseIntError) -> Self {
624+
impl From<core::num::ParseIntError> for CoinFromStrError {
625+
fn from(value: core::num::ParseIntError) -> Self {
626626
Self::InvalidAmount(value)
627627
}
628628
}
@@ -636,7 +636,7 @@ impl From<CoinFromStrError> for StdError {
636636
#[cfg(test)]
637637
mod tests {
638638
use super::*;
639-
use std::str;
639+
use core::str;
640640

641641
// constructors
642642

packages/std/src/errors/system_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub enum SystemError {
4141

4242
impl std::error::Error for SystemError {}
4343

44-
impl std::fmt::Display for SystemError {
45-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44+
impl core::fmt::Display for SystemError {
45+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4646
match self {
4747
SystemError::InvalidRequest { error, request } => write!(
4848
f,

packages/std/src/errors/verification_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use core::fmt::Debug;
12
#[cfg(feature = "backtraces")]
23
use std::backtrace::Backtrace;
3-
use std::fmt::Debug;
44
use thiserror::Error;
55

66
#[cfg(not(target_arch = "wasm32"))]

packages/std/src/exports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//! and `do_sudo` should be wrapped with a extern "C" entry point including
88
//! the contract-specific function pointer. This is done via the `#[entry_point]`
99
//! macro attribute from cosmwasm-derive.
10-
use std::marker::PhantomData;
11-
use std::vec::Vec;
10+
use alloc::vec::Vec;
11+
use core::marker::PhantomData;
1212

1313
use serde::de::DeserializeOwned;
1414

packages/std/src/hex_binary.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::fmt;
2-
use std::ops::Deref;
1+
use core::fmt;
2+
use core::ops::Deref;
33

44
use schemars::JsonSchema;
55
use serde::{de, ser, Deserialize, Deserializer, Serialize};
@@ -143,15 +143,15 @@ impl From<HexBinary> for Binary {
143143
}
144144
}
145145

146-
/// Implement `HexBinary == std::vec::Vec<u8>`
146+
/// Implement `HexBinary == alloc::vec::Vec<u8>`
147147
impl PartialEq<Vec<u8>> for HexBinary {
148148
fn eq(&self, rhs: &Vec<u8>) -> bool {
149149
// Use Vec<u8> == Vec<u8>
150150
self.0 == *rhs
151151
}
152152
}
153153

154-
/// Implement `std::vec::Vec<u8> == HexBinary`
154+
/// Implement `alloc::vec::Vec<u8> == HexBinary`
155155
impl PartialEq<HexBinary> for Vec<u8> {
156156
fn eq(&self, rhs: &HexBinary) -> bool {
157157
// Use Vec<u8> == Vec<u8>
@@ -248,7 +248,6 @@ mod tests {
248248
use super::*;
249249

250250
use crate::{assert_hash_works, from_slice, to_vec, StdError};
251-
use std::collections::HashSet;
252251

253252
#[test]
254253
fn from_hex_works() {
@@ -583,6 +582,8 @@ mod tests {
583582
/// This requires Hash and Eq to be implemented
584583
#[test]
585584
fn hex_binary_can_be_used_in_hash_set() {
585+
use std::collections::HashSet;
586+
586587
let a1 = HexBinary::from([0, 187, 61, 11, 250, 0]);
587588
let a2 = HexBinary::from([0, 187, 61, 11, 250, 0]);
588589
let b = HexBinary::from([16, 21, 33, 0, 255, 9]);

packages/std/src/ibc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// The CosmosMsg variants are defined in results/cosmos_msg.rs
33
// The rest of the IBC related functionality is defined here
44

5+
use core::cmp::{Ord, Ordering, PartialOrd};
56
use schemars::JsonSchema;
67
use serde::{Deserialize, Serialize};
7-
use std::cmp::{Ord, Ordering, PartialOrd};
88

99
#[cfg(feature = "ibc3")]
1010
use crate::addresses::Addr;

packages/std/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::vec::Vec;
1+
use alloc::vec::Vec;
22

33
use crate::addresses::{Addr, CanonicalAddr};
44
use crate::errors::{RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError};

packages/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![cfg_attr(feature = "backtraces", feature(error_generic_member_access))]
22
#![cfg_attr(feature = "backtraces", feature(provide_any))]
33

4+
extern crate alloc;
5+
46
// Exposed on all platforms
57

68
mod addresses;

packages/std/src/math/decimal.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use core::cmp::Ordering;
2+
use core::fmt::{self, Write};
3+
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
4+
use core::str::FromStr;
15
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
26
use schemars::JsonSchema;
37
use serde::{de, ser, Deserialize, Deserializer, Serialize};
4-
use std::cmp::Ordering;
5-
use std::fmt::{self, Write};
6-
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
7-
use std::str::FromStr;
88
use thiserror::Error;
99

1010
use crate::errors::{
@@ -175,7 +175,7 @@ impl Decimal {
175175
///
176176
/// ```
177177
/// # use cosmwasm_std::{Decimal, Uint128};
178-
/// # use std::str::FromStr;
178+
/// # use core::str::FromStr;
179179
/// // Value with whole and fractional part
180180
/// let a = Decimal::from_str("1.234").unwrap();
181181
/// assert_eq!(a.decimal_places(), 18);
@@ -385,7 +385,7 @@ impl Decimal {
385385
/// ## Examples
386386
///
387387
/// ```
388-
/// use std::str::FromStr;
388+
/// use core::str::FromStr;
389389
/// use cosmwasm_std::{Decimal, Uint128};
390390
///
391391
/// let d = Decimal::from_str("12.345").unwrap();
@@ -408,7 +408,7 @@ impl Decimal {
408408
/// ## Examples
409409
///
410410
/// ```
411-
/// use std::str::FromStr;
411+
/// use core::str::FromStr;
412412
/// use cosmwasm_std::{Decimal, Uint128};
413413
///
414414
/// let d = Decimal::from_str("12.345").unwrap();
@@ -679,7 +679,7 @@ impl RemAssign<Decimal> for Decimal {
679679
}
680680
forward_ref_op_assign!(impl RemAssign, rem_assign for Decimal, Decimal);
681681

682-
impl<A> std::iter::Sum<A> for Decimal
682+
impl<A> core::iter::Sum<A> for Decimal
683683
where
684684
Self: Add<A, Output = Self>,
685685
{
@@ -1408,7 +1408,7 @@ mod tests {
14081408
(Decimal::permille(6), Decimal::permille(13)),
14091409
];
14101410

1411-
// The regular std::ops::Mul is our source of truth for these tests.
1411+
// The regular core::ops::Mul is our source of truth for these tests.
14121412
for (x, y) in test_data.into_iter() {
14131413
assert_eq!(x * y, x.checked_mul(y).unwrap());
14141414
}

0 commit comments

Comments
 (0)