Skip to content

Commit 4c673f4

Browse files
authored
Merge pull request #1971 from CosmWasm/1484-std-feature
Add `std` feature
2 parents 632b8c2 + 2219519 commit 4c673f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+139
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ and this project adheres to
6868
- cosmwasm-vm: Replace `MockApi` with bech32 implementation. ([#1914])
6969
- cosmwasm-std: Make `IbcReceiveResponse::acknowledgement` optional and add
7070
`IbcReceiveResponse::without_ack` constructor. ([#1892])
71+
- cosmwasm-std: Add `std` feature and make it a default feature. ([#1971])
7172

7273
[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
7374
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
@@ -86,6 +87,7 @@ and this project adheres to
8687
[#1944]: https://github.com/CosmWasm/cosmwasm/pull/1944
8788
[#1949]: https://github.com/CosmWasm/cosmwasm/pull/1949
8889
[#1967]: https://github.com/CosmWasm/cosmwasm/pull/1967
90+
[#1971]: https://github.com/CosmWasm/cosmwasm/pull/1971
8991

9092
### Removed
9193

MIGRATING.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ major releases of `cosmwasm`. Note that you can also view the
2121

2222
If you were using cosmwasm-std's `ibc3` feature, you can remove it, as it is
2323
the default now. Depending on your usage, you might have to enable the
24-
`stargate` feature instead, since it was previously implied by `ibc3`. Also
25-
remove any uses of the `backtraces` feature. You can use a `RUST_BACKTRACE=1`
26-
env variable for this now.
24+
`stargate` feature instead, since it was previously implied by `ibc3`.
25+
26+
Also remove any uses of the `backtraces` feature. You can use a
27+
`RUST_BACKTRACE=1` env variable for this now.
28+
29+
If you were using `cosmwasm-std` with `default-features = false`, you probably
30+
want to enable the `std` feature now, as we might move certain existing
31+
functionality to that feature in the future to support no_std environments:
32+
33+
```diff
34+
-cosmwasm-std = { version = "2.0.0", default-features = false, features = [...] }
35+
+cosmwasm-std = { version = "2.0.0", default-features = false, features = ["std", ...] }
36+
```
2737

2838
- `ContractInfoResponse::new` now takes all fields of the response as
2939
parameters:

docs/USING_COSMWASM_STD.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ there it becomes impossible to use the contract in chains with lower CosmWasm
7474
versions. If you add `abort`, it becomes impossible for the contract developer
7575
to opt out of the abort feature due to your library. Since this affects the
7676
default features `abort` and `iterator`, you should always disable default
77-
features.
77+
features. However, you should make sure to keep the `std` feature enabled, as we
78+
might move certain existing functionality to that feature in the future.
7879

7980
Also libraries should define a loose version range that allows the contract
8081
developer to control which cosmwasm-std version they want to use in the final
@@ -85,5 +86,6 @@ A typical dependency then looks like this:
8586

8687
```toml
8788
# We really need `stargate` here as this is an IBC related library. `abort` and `iterator` are not needed.
88-
cosmwasm-std = { version = "1.0.1", default-features = false, features = ["stargate"] }
89+
# `std` should always stay enabled.
90+
cosmwasm-std = { version = "1.0.1", default-features = false, features = ["std", "stargate"] }
8991
```

packages/std/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ readme = "README.md"
1212
features = ["abort", "stargate", "staking", "cosmwasm_1_4"]
1313

1414
[features]
15-
default = ["iterator", "abort"]
15+
default = ["iterator", "abort", "std"]
1616
abort = []
17+
# This feature can be used in the future to provide support for no_std environments,
18+
# but disabling it will not provide any benefit at the moment. Even with this feature disabled,
19+
# we currently require an std environment.
20+
# You probably want to keep this enabled for now to avoid possible breaking changes later.
21+
std = ["serde/std", "serde-json-wasm/std"]
1722
# iterator allows us to iterate over all DB items in a given range
1823
# optional as some merkle stores (like tries) don't support this
1924
# given Ethereum 1.0, 2.0, Substrate, and other major projects use Tries
@@ -45,13 +50,13 @@ cosmwasm_1_4 = ["cosmwasm_1_3"]
4550
[dependencies]
4651
base64 = "0.21.0"
4752
cosmwasm-derive = { path = "../derive", version = "1.5.0" }
48-
derivative = "2"
53+
derivative = { version = "2", features = ["use_core"] }
4954
forward_ref = "1"
5055
hex = "0.4"
5156
schemars = "0.8.3"
5257
sha2 = "0.10.3"
5358
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }
54-
serde-json-wasm = { version = "1.0.0" }
59+
serde-json-wasm = { version = "1.0.0", default-features = false }
5560
thiserror = "1.0.26"
5661
bnum = "0.8.0"
5762
static_assertions = "1.1.0"

packages/std/src/addresses.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use sha2::{
99
};
1010
use thiserror::Error;
1111

12+
use crate::prelude::*;
1213
use crate::{binary::Binary, forward_ref_partial_eq, HexBinary};
1314

1415
/// A human readable address.

packages/std/src/binary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use schemars::JsonSchema;
66
use serde::{de, ser, Deserialize, Deserializer, Serialize};
77

88
use crate::errors::{StdError, StdResult};
9+
use crate::prelude::*;
910

1011
/// Binary is a wrapper around Vec<u8> to add base64 de/serialization
1112
/// with serde. It also adds some helper methods to help encode inline.

packages/std/src/checksum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
55
use sha2::{Digest, Sha256};
66
use thiserror::Error;
77

8+
use crate::prelude::*;
89
use crate::{StdError, StdResult};
910

1011
/// A SHA-256 checksum of a Wasm blob, used to identify a Wasm code.

packages/std/src/coin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::{fmt, str::FromStr};
22
use schemars::JsonSchema;
33
use serde::{Deserialize, Serialize};
44

5+
use crate::prelude::*;
56
use crate::{errors::CoinFromStrError, math::Uint128};
67

78
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, JsonSchema)]

packages/std/src/coins.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use alloc::collections::BTreeMap;
22
use core::fmt;
33
use core::str::FromStr;
44

5+
use crate::prelude::*;
56
use crate::{
67
errors::CoinsError, Coin, OverflowError, OverflowOperation, StdError, StdResult, Uint128,
78
};

packages/std/src/errors/backtrace.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::fmt::{Debug, Display, Formatter, Result};
22

3+
use crate::prelude::*;
4+
35
/// This wraps an actual backtrace to achieve two things:
46
/// - being able to fill this with a stub implementation in `no_std` environments
57
/// - being able to use this in conjunction with [`thiserror::Error`]
@@ -9,10 +11,17 @@ impl BT {
911
#[track_caller]
1012
pub fn capture() -> Self {
1113
// in case of no_std, we can fill with a stub here
12-
#[cfg(target_arch = "wasm32")]
13-
return BT(Box::new(std::backtrace::Backtrace::disabled()));
14-
#[cfg(not(target_arch = "wasm32"))]
15-
return BT(Box::new(std::backtrace::Backtrace::capture()));
14+
#[cfg(feature = "std")]
15+
{
16+
#[cfg(target_arch = "wasm32")]
17+
return BT(Box::new(std::backtrace::Backtrace::disabled()));
18+
#[cfg(not(target_arch = "wasm32"))]
19+
return BT(Box::new(std::backtrace::Backtrace::capture()));
20+
}
21+
#[cfg(not(feature = "std"))]
22+
{
23+
BT(Box::new(Stub))
24+
}
1625
}
1726
}
1827

@@ -31,6 +40,21 @@ impl Display for BT {
3140
}
3241
}
3342

43+
#[allow(unused)]
44+
struct Stub;
45+
46+
impl Debug for Stub {
47+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
48+
write!(f, "<disabled>")
49+
}
50+
}
51+
52+
impl Display for Stub {
53+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
54+
write!(f, "<disabled>")
55+
}
56+
}
57+
3458
/// This macro implements `From` for a given error type to a given error type where
3559
/// the target error has a `backtrace` field.
3660
/// This is meant as a replacement for `thiserror`'s `#[from]` attribute, which does not

packages/std/src/errors/std_error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::{impl_from_err, BT};
44
use thiserror::Error;
55

66
use crate::errors::{RecoverPubkeyError, VerificationError};
7+
use crate::prelude::*;
78

89
/// Structured error type for init, execute and query.
910
///

packages/std/src/errors/system_error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4+
use crate::prelude::*;
45
use crate::Binary;
56

67
/// SystemError is used for errors inside the VM and is API friendly (i.e. serializable).
@@ -39,6 +40,7 @@ pub enum SystemError {
3940
},
4041
}
4142

43+
#[cfg(feature = "std")]
4244
impl std::error::Error for SystemError {}
4345

4446
impl core::fmt::Display for SystemError {

packages/std/src/hex_binary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::ops::Deref;
44
use schemars::JsonSchema;
55
use serde::{de, ser, Deserialize, Deserializer, Serialize};
66

7+
use crate::prelude::*;
78
use crate::{Binary, StdError, StdResult};
89

910
/// This is a wrapper around Vec<u8> to add hex de/serialization

packages/std/src/ibc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::addresses::Addr;
99
use crate::binary::Binary;
1010
use crate::coin::Coin;
1111
use crate::errors::StdResult;
12+
use crate::prelude::*;
1213
use crate::results::{Attribute, CosmosMsg, Empty, Event, SubMsg};
1314
use crate::serde::to_json_binary;
1415
use crate::timestamp::Timestamp;

packages/std/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ mod timestamp;
3232
mod traits;
3333
mod types;
3434

35+
/// This module is to simplify no_std imports
36+
pub(crate) mod prelude;
37+
3538
/// This modules is very advanced and will not be used directly by the vast majority of users.
3639
/// We want to offer it to ensure a stable storage key composition system but don't encourage
3740
/// contract devs to use it directly.

packages/std/src/math/decimal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::errors::{
1111
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1212
OverflowOperation, RoundUpOverflowError, StdError,
1313
};
14+
use crate::prelude::*;
1415
use crate::{forward_ref_partial_eq, Decimal256, SignedDecimal, SignedDecimal256};
1516

1617
use super::Fraction;

packages/std/src/math/decimal256.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::errors::{
1111
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1212
OverflowOperation, RoundUpOverflowError, StdError,
1313
};
14+
use crate::prelude::*;
1415
use crate::{forward_ref_partial_eq, Decimal, SignedDecimal, SignedDecimal256, Uint512};
1516

1617
use super::Fraction;

packages/std/src/math/int128.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use schemars::JsonSchema;
99
use serde::{de, ser, Deserialize, Deserializer, Serialize};
1010

1111
use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
12+
use crate::prelude::*;
1213
use crate::{
1314
forward_ref_partial_eq, CheckedMultiplyRatioError, Int256, Int512, Int64, Uint128, Uint256,
1415
Uint512, Uint64,

packages/std/src/math/int256.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use schemars::JsonSchema;
99
use serde::{de, ser, Deserialize, Deserializer, Serialize};
1010

1111
use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
12+
use crate::prelude::*;
1213
use crate::{
1314
forward_ref_partial_eq, CheckedMultiplyRatioError, Int128, Int512, Int64, Uint128, Uint256,
1415
Uint512, Uint64,

packages/std/src/math/int512.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use schemars::JsonSchema;
99
use serde::{de, ser, Deserialize, Deserializer, Serialize};
1010

1111
use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
12+
use crate::prelude::*;
1213
use crate::{forward_ref_partial_eq, Int128, Int256, Int64, Uint128, Uint256, Uint512, Uint64};
1314

1415
/// Used internally - we don't want to leak this type since we might change

packages/std/src/math/int64.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use schemars::JsonSchema;
99
use serde::{de, ser, Deserialize, Deserializer, Serialize};
1010

1111
use crate::errors::{DivideByZeroError, DivisionError, OverflowError, OverflowOperation, StdError};
12+
use crate::prelude::*;
1213
use crate::{
1314
forward_ref_partial_eq, CheckedMultiplyRatioError, Int128, Int256, Int512, Uint128, Uint256,
1415
Uint512, Uint64,

packages/std/src/math/signed_decimal.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::errors::{
1313
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1414
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
1515
};
16+
use crate::prelude::*;
1617
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int256, SignedDecimal256};
1718

1819
use super::Fraction;

packages/std/src/math/signed_decimal_256.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::errors::{
1313
CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1414
OverflowOperation, RoundDownOverflowError, RoundUpOverflowError, StdError,
1515
};
16+
use crate::prelude::*;
1617
use crate::{forward_ref_partial_eq, Decimal, Decimal256, Int512, SignedDecimal};
1718

1819
use super::Fraction;

packages/std/src/math/uint128.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use core::fmt::{self};
1+
use core::fmt;
22
use core::ops::{
3-
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
4-
Sub, SubAssign,
3+
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
4+
ShrAssign, Sub, SubAssign,
55
};
66
use core::str::FromStr;
7-
use std::ops::Not;
87

98
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
109
use schemars::JsonSchema;
@@ -14,6 +13,7 @@ use crate::errors::{
1413
CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1514
OverflowOperation, StdError,
1615
};
16+
use crate::prelude::*;
1717
use crate::{
1818
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint256,
1919
Uint64,

packages/std/src/math/uint256.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use core::fmt;
22
use core::ops::{
3-
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
4-
Sub, SubAssign,
3+
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
4+
ShrAssign, Sub, SubAssign,
55
};
66
use core::str::FromStr;
77
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
88
use schemars::JsonSchema;
99
use serde::{de, ser, Deserialize, Deserializer, Serialize};
10-
use std::ops::Not;
1110

1211
use crate::errors::{
1312
CheckedMultiplyFractionError, CheckedMultiplyRatioError, ConversionOverflowError,
1413
DivideByZeroError, OverflowError, OverflowOperation, StdError,
1514
};
15+
use crate::prelude::*;
1616
use crate::{
1717
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint128,
1818
Uint512, Uint64,

packages/std/src/math/uint512.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
1111
use crate::errors::{
1212
ConversionOverflowError, DivideByZeroError, OverflowError, OverflowOperation, StdError,
1313
};
14+
use crate::prelude::*;
1415
use crate::{forward_ref_partial_eq, Int128, Int256, Int512, Int64, Uint128, Uint256, Uint64};
1516

1617
/// Used internally - we don't want to leak this type since we might change

packages/std/src/math/uint64.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use core::fmt::{self};
1+
use core::fmt;
22
use core::ops::{
3-
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
4-
Sub, SubAssign,
3+
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr,
4+
ShrAssign, Sub, SubAssign,
55
};
66
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
77
use schemars::JsonSchema;
88
use serde::{de, ser, Deserialize, Deserializer, Serialize};
9-
use std::ops::Not;
109

1110
use crate::errors::{
1211
CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
1312
OverflowOperation, StdError,
1413
};
14+
use crate::prelude::*;
1515
use crate::{
1616
forward_ref_partial_eq, impl_mul_fraction, Fraction, Int128, Int256, Int512, Int64, Uint128,
1717
};

packages/std/src/metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4+
use crate::prelude::*;
5+
46
/// Replicates the cosmos-sdk bank module Metadata type
57
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
68
pub struct DenomMetadata {

packages/std/src/pagination.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4+
use crate::prelude::*;
45
use crate::Binary;
56

67
/// Simplified version of the PageRequest type for pagination from the cosmos-sdk

packages/std/src/prelude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub use alloc::boxed::Box;
2+
pub use alloc::format;
3+
pub use alloc::string::{String, ToString};
4+
pub use alloc::vec;
5+
pub use alloc::vec::Vec;
6+
pub use core::option::Option::{self, None, Some};

packages/std/src/query/bank.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
33

44
use crate::Coin;
55

6+
use crate::prelude::*;
67
#[cfg(feature = "cosmwasm_1_3")]
78
use crate::PageRequest;
89
use crate::{Binary, DenomMetadata};

0 commit comments

Comments
 (0)