Skip to content

Commit

Permalink
feat: bump rust version one more day and use inline_const
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Apr 26, 2024
1 parent 725d7a6 commit c8efc9e
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 68 deletions.
6 changes: 3 additions & 3 deletions lib/block-message/src/chain_impls/cosmos_sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use unionlabs::{
hash::H256,
ibc::core::client::height::IsHeight,
id::ConnectionId,
option_unwrap, promote,
option_unwrap,
tendermint::abci::{event::Event, event_attribute::EventAttribute},
traits::{ClientIdOf, HeightOf},
};
Expand Down Expand Up @@ -278,7 +278,7 @@ where
Fetch::<C>::specific(FetchTransactions {
height: from_height,
// who needs const blocks
page: promote!(NonZeroU32: option_unwrap!(NonZeroU32::new(1_u32))),
page: const { option_unwrap!(NonZeroU32::new(1_u32)) },
}),
))
} else {
Expand All @@ -291,7 +291,7 @@ where
Fetch::<C>::specific(FetchTransactions {
height: from_height,
// who needs const blocks
page: promote!(NonZeroU32: option_unwrap!(NonZeroU32::new(1_u32))),
page: const { option_unwrap!(NonZeroU32::new(1_u32)) },
}),
))]
.into_iter()
Expand Down
12 changes: 8 additions & 4 deletions lib/chain-utils/src/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use unionlabs::{
lightclients::tendermint::{self, fraction::Fraction},
},
id::ClientId,
option_unwrap, promote,
option_unwrap, result_unwrap,
signer::CosmosSigner,
traits::{Chain, ClientState, FromStrExact},
WasmClientType,
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Chain for Cosmos {
// https://github.com/cometbft/cometbft/blob/da0e55604b075bac9e1d5866cb2e62eaae386dd9/light/verifier.go#L16
trust_level: Fraction {
numerator: 1,
denominator: promote!(NonZeroU64: option_unwrap!(NonZeroU64::new(3))),
denominator: const { option_unwrap!(NonZeroU64::new(3)) },
},
// https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
trusting_period: unionlabs::google::protobuf::duration::Duration::new(
Expand All @@ -193,8 +193,12 @@ impl Chain for Cosmos {
)
.unwrap(),
// https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
max_clock_drift: unionlabs::google::protobuf::duration::Duration::new(60 * 10, 0)
.unwrap(),
max_clock_drift: const {
result_unwrap!(unionlabs::google::protobuf::duration::Duration::new(
60 * 10,
0
))
},
frozen_height: None,
latest_height: Height {
revision_number: self.chain_revision,
Expand Down
6 changes: 3 additions & 3 deletions lib/chain-utils/src/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use unionlabs::{
NextConnectionSequencePath, ReceiptPath,
},
id::{ChannelId, ClientId, PortId},
option_unwrap, promote,
option_unwrap,
traits::{Chain, ClientIdOf, ClientState, FromStrExact, HeightOf},
uint::U256,
};
Expand Down Expand Up @@ -216,7 +216,7 @@ impl<C: ChainSpec> FromStrExact for EthereumChainType<C> {
"ChainSpec string value is expected to be 7 bytes"
);

match core::str::from_utf8(&concat(C::EXPECTING.as_bytes())) {
match core::str::from_utf8(const { &concat(C::EXPECTING.as_bytes()) }) {
Ok(ok) => ok,
Err(_) => {
panic!()
Expand Down Expand Up @@ -388,7 +388,7 @@ impl<C: ChainSpec, S: EthereumSignersConfig> Chain for Ethereum<C, S> {
min_sync_committee_participants: 0,
trust_level: Fraction {
numerator: 1,
denominator: promote!(NonZeroU64: option_unwrap!(NonZeroU64::new(3))),
denominator: const { option_unwrap!(NonZeroU64::new(3)) },
},
frozen_height: Height {
revision_number: 0,
Expand Down
26 changes: 17 additions & 9 deletions lib/ics23/src/proof_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use unionlabs::{
length_op::LengthOp,
proof_spec::ProofSpec,
},
promote, result_unwrap,
result_unwrap,
};

pub const IAVL_PROOF_SPEC: ProofSpec = ProofSpec {
Expand All @@ -20,10 +20,14 @@ pub const IAVL_PROOF_SPEC: ProofSpec = ProofSpec {
prefix: Cow::Borrowed(&[0]),
},
inner_spec: InnerSpec {
child_order: Cow::Borrowed(promote!(&[PositiveI32AsUsize]: &[
result_unwrap!(PositiveI32AsUsize::new(0)),
result_unwrap!(PositiveI32AsUsize::new(1)),
])),
child_order: Cow::Borrowed(
const {
&[
result_unwrap!(PositiveI32AsUsize::new(0)),
result_unwrap!(PositiveI32AsUsize::new(1)),
]
},
),
child_size: result_unwrap!(PositiveI32AsUsize::new(33)),
min_prefix_length: result_unwrap!(PositiveI32AsUsize::new(4)),
max_prefix_length: result_unwrap!(PositiveI32AsUsize::new(12)),
Expand All @@ -44,10 +48,14 @@ pub const TENDERMINT_PROOF_SPEC: ProofSpec = ProofSpec {
prefix: Cow::Borrowed(&[0]),
},
inner_spec: InnerSpec {
child_order: Cow::Borrowed(promote!(&[PositiveI32AsUsize]: &[
result_unwrap!(PositiveI32AsUsize::new(0)),
result_unwrap!(PositiveI32AsUsize::new(1)),
])),
child_order: Cow::Borrowed(
const {
&[
result_unwrap!(PositiveI32AsUsize::new(0)),
result_unwrap!(PositiveI32AsUsize::new(1)),
]
},
),
child_size: result_unwrap!(PositiveI32AsUsize::new(32)),
min_prefix_length: result_unwrap!(PositiveI32AsUsize::new(1)),
max_prefix_length: result_unwrap!(PositiveI32AsUsize::new(1)),
Expand Down
4 changes: 2 additions & 2 deletions lib/tendermint-verifier/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ mod tests {
use std::{fs, num::NonZeroU64};

use ed25519_dalek::{Signature, Verifier, VerifyingKey};
use unionlabs::{ibc::lightclients::tendermint::header::Header, option_unwrap, promote};
use unionlabs::{ibc::lightclients::tendermint::header::Header, option_unwrap};

use super::*;

Expand Down Expand Up @@ -586,7 +586,7 @@ mod tests {
Duration::new(100_000_000, 0).unwrap(),
Fraction {
numerator: 1,
denominator: promote!(NonZeroU64: option_unwrap!(NonZeroU64::new(3))),
denominator: const { option_unwrap!(NonZeroU64::new(3)) },
},
&SignatureVerifier::new(EdVerifier),
)
Expand Down
2 changes: 1 addition & 1 deletion lib/unionlabs/src/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ macro_rules! bounded_int {

impl<const MIN: $ty, const MAX: $ty> $Struct<MIN, MAX> {
pub const fn new(n: $ty) -> Result<Self, BoundedIntError<$ty>> {
$crate::const_assert!(MIN: $ty, MAX: $ty => MIN < MAX);
const {MIN < MAX};

if n >= MIN && n <= MAX {
Ok(Self(n))
Expand Down
5 changes: 2 additions & 3 deletions lib/unionlabs/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::fmt::Debug;

use crate::{
const_assert,
errors::{ExpectedLength, InvalidLength},
validated::{Validate, Validated},
};
Expand Down Expand Up @@ -93,7 +92,7 @@ impl<T: Into<String> + From<String>, const MIN: usize, const MAX: usize> Validat
type Error = InvalidLength;

fn validate(t: T) -> Result<T, Self::Error> {
const_assert!(MIN: usize, MAX: usize => MIN <= MAX);
const { assert!(MIN <= MAX) };

let s: String = t.into();

Expand All @@ -115,7 +114,7 @@ impl<T: Into<String> + From<String>, const MIN: usize, const MAX: usize>
crate::validated::ValidateExt<T> for Bounded<MIN, MAX>
{
fn restrict(t: T, u: &mut arbitrary::Unstructured) -> arbitrary::Result<T> {
const_assert!(MIN: usize, MAX: usize => MIN <= MAX);
const { assert!(MIN <= MAX) };

let s: String = t.into();

Expand Down
2 changes: 1 addition & 1 deletion lib/unionlabs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub trait ByteArrayExt<const N: usize> {

impl<const N: usize> ByteArrayExt<N> for [u8; N] {
fn array_slice<const OFFSET: usize, const LEN: usize>(&self) -> [u8; LEN] {
const_assert!(OFFSET: usize, LEN: usize, N: usize => OFFSET + LEN <= N);
const { assert!(OFFSET + LEN <= N) };

unsafe { *addr_of!(self[OFFSET..(OFFSET + LEN)]).cast::<[u8; LEN]>() }
}
Expand Down
41 changes: 0 additions & 41 deletions lib/unionlabs/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,44 +402,3 @@ macro_rules! option_unwrap {
}
}};
}

// Useful in const contexts to promote a value to a const when const promotion fails with "temporary value dropped while borrowed"
#[macro_export]
macro_rules! promote {
($ty:ty: $expr:expr) => {{
const PROMOTED: $ty = $expr;
PROMOTED
}};
}

/// Assert some expression with the provided const variables.
///
/// Note that if this is called within a function, then the function must be called in order for the
/// generated constant to be evaluated to cause a compilation error.
///
/// # Example
///
/// ```rust
/// # use unionlabs::const_assert;
///
/// struct Bounded<const MIN: u8, const MAX: u8>(u8);
///
/// impl<const MIN: u8, const MAX: u8> Bounded<MIN, MAX> {
/// fn new(n: u8) -> Option<Self> {
/// const_assert!(MIN: u8, MAX: u8 => MIN < MAX);
/// (MIN..=MAX).contains(&n).then_some(Self(n))
/// }
/// }
///
/// // anywhere that `Bounded::new` is called will fail to compile if MIN >= MAX.
/// ```
#[macro_export]
macro_rules! const_assert {
($($list:ident: $ty:ty),* => $expr:expr) => {{
struct Assert<$(const $list: $ty,)*>;
impl<$(const $list: $ty,)*> Assert<$($list,)*> {
const OK: () = assert!($expr);
}
let _t = Assert::<$($list,)*>::OK;
}};
}
2 changes: 1 addition & 1 deletion tools/rust/rust.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ ... }: {
perSystem = { pkgs, system, dbg, ensureAtRepositoryRoot, mkCi, ... }:
let
nightlyVersion = "2024-04-24";
nightlyVersion = "2024-04-25";
defaultChannel = "nightly-${nightlyVersion}";

# # hopefully if we ever use wasi this issue will be resolved: https://github.com/NixOS/nixpkgs/pull/146274
Expand Down

0 comments on commit c8efc9e

Please sign in to comment.