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

0 commit comments

Comments
 (0)