From 34af48221c90e6818fe341d6d5b15116dfeab671 Mon Sep 17 00:00:00 2001 From: aeryz Date: Tue, 31 Jan 2023 05:18:08 -0700 Subject: [PATCH] Upgrade `no_std` to 1.3 Signed-off-by: aeryz --- packages/std/Cargo.toml | 13 ++----- packages/std/src/addresses.rs | 5 +-- packages/std/src/errors/std_error.rs | 36 ++++++++++++++++--- packages/std/src/errors/verification_error.rs | 2 +- packages/std/src/lib.rs | 2 +- packages/std/src/math/uint128.rs | 7 ++-- packages/std/src/query/wasm.rs | 4 +-- packages/std/src/results/cosmos_msg.rs | 3 +- 8 files changed, 44 insertions(+), 28 deletions(-) diff --git a/packages/std/Cargo.toml b/packages/std/Cargo.toml index 78a82a234d..baa3a6f95e 100644 --- a/packages/std/Cargo.toml +++ b/packages/std/Cargo.toml @@ -41,23 +41,16 @@ cosmwasm_1_1 = [] cosmwasm_1_2 = ["cosmwasm_1_1"] std = ["forward_ref", "schemars", "serde-json-wasm", "thiserror", "cosmwasm-crypto"] -[dependencies] -base64 = "0.13.0" -cosmwasm-derive = { path = "../derive", version = "1.2.0" } -derivative = { version = "2", optional = true } -forward_ref = "1" -hex = "0.4" - [dependencies] base64 = { version = "0.13.0", default-features = false, features = ["alloc"] } -cosmwasm-derive = { path = "../derive", version = "1.1.6" } +cosmwasm-derive = { path = "../derive", version = "1.2.0" } derivative = { version = "2", features = ["use_core"] } forward_ref = { version = "1", optional = true } hex = { version = "0.4", default-features = false, features = ["alloc"] } schemars = { version = "0.8.3", optional = true } -sha2 = "0.10.3" +sha2 = { version = "0.10.3", default-features = false } serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] } -serde-json-wasm = { version = "0.5.0" } +serde-json-wasm = { version = "0.5.0", optional = true } thiserror = { version = "1.0.26", optional = true } uint = { version = "0.9.3", default-features = false } diff --git a/packages/std/src/addresses.rs b/packages/std/src/addresses.rs index a5620bfa22..283abf90d5 100644 --- a/packages/std/src/addresses.rs +++ b/packages/std/src/addresses.rs @@ -383,14 +383,11 @@ fn hash(ty: &str, key: &[u8]) -> Vec { #[cfg(test)] mod tests { use super::*; -<<<<<<< HEAD use crate::HexBinary; - use hex_literal::hex; -======= use alloc::string::ToString; use alloc::vec::Vec; + use hex_literal::hex; #[cfg(feature = "std")] ->>>>>>> 059adae7 (feat(std): no_std) use std::collections::hash_map::DefaultHasher; #[cfg(feature = "std")] use std::collections::HashSet; diff --git a/packages/std/src/errors/std_error.rs b/packages/std/src/errors/std_error.rs index 1428dc7387..ac2ae9663f 100644 --- a/packages/std/src/errors/std_error.rs +++ b/packages/std/src/errors/std_error.rs @@ -579,15 +579,43 @@ impl DivideByZeroError { #[cfg_attr(feature = "std", derive(thiserror::Error))] #[derive(Debug, PartialEq, Eq)] -pub enum CheckedMultiplyRatioError { +pub enum CheckedMultiplyFractionError { #[cfg_attr(feature = "std", error("{0}"))] - DivideByZero(#[from] DivideByZeroError), + DivideByZero(DivideByZeroError), #[cfg_attr(feature = "std", error("{0}"))] - ConversionOverflow(#[from] ConversionOverflowError), + ConversionOverflow(ConversionOverflowError), #[cfg_attr(feature = "std", error("{0}"))] - Overflow(#[from] OverflowError), + Overflow(OverflowError), +} + +impl From for CheckedMultiplyFractionError { + fn from(e: DivideByZeroError) -> Self { + Self::DivideByZero(e) + } +} + +impl From for CheckedMultiplyFractionError { + fn from(e: ConversionOverflowError) -> Self { + Self::ConversionOverflow(e) + } +} + +impl From for CheckedMultiplyFractionError { + fn from(e: OverflowError) -> Self { + Self::Overflow(e) + } +} + +#[cfg_attr(feature = "std", derive(thiserror::Error))] +#[derive(Debug, PartialEq, Eq)] +pub enum CheckedMultiplyRatioError { + #[cfg_attr(feature = "std", error("Denominator must not be zero"))] + DivideByZero, + + #[cfg_attr(feature = "std", error("Multiplication overflow"))] + Overflow, } #[cfg_attr(feature = "std", derive(thiserror::Error))] diff --git a/packages/std/src/errors/verification_error.rs b/packages/std/src/errors/verification_error.rs index 1895e9bde8..b00edda382 100644 --- a/packages/std/src/errors/verification_error.rs +++ b/packages/std/src/errors/verification_error.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "backtraces")] +#[cfg(all(feature = "backtraces", feature = "std"))] use std::backtrace::Backtrace; #[cfg(all(not(target_arch = "wasm32"), feature = "std"))] diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index 85f4a3adc0..154154bf9c 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(feature = "backtraces", feature(backtrace))] +#![cfg_attr(all(feature = "backtraces", feature = "std"), feature(backtrace))] #![cfg_attr(not(feature = "std"), no_std)] // Exposed on all platforms diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index dafb7eac13..1fbd4af110 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -8,15 +8,12 @@ use core::str::FromStr; use forward_ref::{forward_ref_binop, forward_ref_op_assign}; use serde::{de, ser, Deserialize, Deserializer, Serialize}; -use forward_ref::{forward_ref_binop, forward_ref_op_assign}; -use schemars::JsonSchema; -use serde::{de, ser, Deserialize, Deserializer, Serialize}; - use crate::errors::{ CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError, OverflowOperation, StdError, }; -use crate::{impl_mul_fraction, ConversionOverflowError, Fraction, Uint256, Uint64}; +use crate::{impl_mul_fraction, Fraction}; +use crate::{ConversionOverflowError, Uint256, Uint64}; /// A thin wrapper around u128 that is using strings for JSON encoding/decoding, /// such that the full u128 range can be used for clients that convert JSON numbers to floats, diff --git a/packages/std/src/query/wasm.rs b/packages/std/src/query/wasm.rs index 4841bed7fb..157fb7e093 100644 --- a/packages/std/src/query/wasm.rs +++ b/packages/std/src/query/wasm.rs @@ -35,7 +35,6 @@ pub enum WasmQuery { } #[non_exhaustive] -#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)] #[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq)] pub struct ContractInfoResponse { @@ -78,7 +77,8 @@ impl ContractInfoResponse { /// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72 /// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199 #[non_exhaustive] -#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] #[cfg(feature = "cosmwasm_1_2")] pub struct CodeInfoResponse { pub code_id: u64, diff --git a/packages/std/src/results/cosmos_msg.rs b/packages/std/src/results/cosmos_msg.rs index 871a8981f0..5d3ac210fe 100644 --- a/packages/std/src/results/cosmos_msg.rs +++ b/packages/std/src/results/cosmos_msg.rs @@ -320,7 +320,8 @@ pub enum VoteOption { } #[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))] -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct WeightedVoteOption { pub option: VoteOption, pub weight: Decimal,