Skip to content

Commit

Permalink
Merge pull request #1474 from multiversx/exhaustive-patterns
Browse files Browse the repository at this point in the history
remove never type and exhaustive patterns unstable features
  • Loading branch information
mihaicalinluca authored Mar 14, 2024
2 parents 58af3f6 + ba0e155 commit ee35a53
Show file tree
Hide file tree
Showing 36 changed files with 200 additions and 117 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![feature(never_type)]

multiversx_sc::imports!();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![feature(never_type)]

multiversx_sc::imports!();

Expand Down
1 change: 1 addition & 0 deletions data/codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ optional = true
[dependencies]
arrayvec = { version = "=0.7.4", default-features = false }
num-bigint = { version = "=0.4.4", optional = true } # can only be used in std contexts
unwrap-infallible = "0.1.5"

[dev-dependencies.multiversx-sc-codec-derive]
path = "../codec-derive"
Expand Down
6 changes: 4 additions & 2 deletions data/codec/src/codec_err_handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::convert::Infallible;

use crate::{DecodeError, EncodeError};

pub trait EncodeErrorHandler: Copy {
Expand Down Expand Up @@ -39,7 +41,7 @@ impl DecodeErrorHandler for DefaultErrorHandler {
pub struct PanicErrorHandler;

impl EncodeErrorHandler for PanicErrorHandler {
type HandledErr = !;
type HandledErr = Infallible;

#[inline]
fn handle_error(&self, err: EncodeError) -> Self::HandledErr {
Expand All @@ -48,7 +50,7 @@ impl EncodeErrorHandler for PanicErrorHandler {
}

impl DecodeErrorHandler for PanicErrorHandler {
type HandledErr = !;
type HandledErr = Infallible;

#[inline]
fn handle_error(&self, err: DecodeError) -> Self::HandledErr {
Expand Down
8 changes: 5 additions & 3 deletions data/codec/src/equivalent/codec_convert.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use unwrap_infallible::UnwrapInfallible;

use crate::{
CodecFrom, PanicErrorHandler, TopDecodeMultiInput, TopEncodeMulti, TopEncodeMultiOutput,
};
Expand All @@ -9,9 +11,9 @@ where
Medium: Default + TopDecodeMultiInput + TopEncodeMultiOutput,
{
let mut medium: Medium = Default::default();
let Ok(()) = from.multi_encode_or_handle_err(&mut medium, PanicErrorHandler);
let Ok(result) = To::multi_decode_or_handle_err(&mut medium, PanicErrorHandler);
result
from.multi_encode_or_handle_err(&mut medium, PanicErrorHandler)
.unwrap_infallible();
To::multi_decode_or_handle_err(&mut medium, PanicErrorHandler).unwrap_infallible()
}

#[allow(unused)]
Expand Down
2 changes: 0 additions & 2 deletions data/codec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![no_std]
#![feature(try_trait_v2)]
#![feature(never_type)]
#![feature(exhaustive_patterns)]
#![feature(auto_traits)]
#![feature(negative_impls)]

Expand Down
4 changes: 3 additions & 1 deletion data/codec/src/single/top_en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
PanicErrorHandler, TopEncodeOutput,
};
use alloc::vec::Vec;
use unwrap_infallible::UnwrapInfallible;

pub trait TopEncode: Sized {
/// Attempt to serialize the value to ouput.
Expand Down Expand Up @@ -48,6 +49,7 @@ pub fn top_encode_to_vec_u8<T: TopEncode>(obj: &T) -> Result<Vec<u8>, EncodeErro

pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
let Ok(()) = obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler);
obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
.unwrap_infallible();
bytes
}
14 changes: 8 additions & 6 deletions data/codec/src/test_util.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
use crate::*;
use alloc::vec::Vec;
use core::fmt::Debug;
use unwrap_infallible::UnwrapInfallible;

/// Calls top encode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
let Ok(()) = obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler);
obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
.unwrap_infallible();
bytes
}

/// Calls nested encode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn dep_encode_to_vec_or_panic<T: NestedEncode>(obj: &T) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
let Ok(()) = obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler);
obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler)
.unwrap_infallible();
bytes
}

Expand All @@ -41,15 +44,14 @@ pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
/// Calls nested decode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
let Ok(result) = dep_decode_from_byte_slice(input, PanicErrorHandler);
result
dep_decode_from_byte_slice(input, PanicErrorHandler).unwrap_infallible()
}

/// Calls both the fast exit and the regular top-decode,
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
let Ok(fast_exit_obj) = T::top_decode_or_handle_err(bytes, PanicErrorHandler);
let fast_exit_obj = T::top_decode_or_handle_err(bytes, PanicErrorHandler).unwrap_infallible();
let result_obj = T::top_decode_or_handle_err(bytes, DefaultErrorHandler).unwrap();
assert_eq!(fast_exit_obj, result_obj);
fast_exit_obj
Expand All @@ -59,7 +61,7 @@ pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
let Ok(fast_exit_obj) = dep_decode_from_byte_slice(bytes, PanicErrorHandler);
let fast_exit_obj = dep_decode_from_byte_slice(bytes, PanicErrorHandler).unwrap_infallible();
let result_obj = dep_decode_from_byte_slice(bytes, DefaultErrorHandler).unwrap();
assert_eq!(fast_exit_obj, result_obj);
fast_exit_obj
Expand Down
1 change: 1 addition & 0 deletions framework/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ esdt-token-payment-legacy-decode = []
hex-literal = "=0.4.1"
bitflags = "=2.4.2"
num-traits = { version = "=0.2.17", default-features = false }
unwrap-infallible = "0.1.5"

[dependencies.multiversx-sc-derive]
version = "=0.47.5"
Expand Down
40 changes: 23 additions & 17 deletions framework/base/src/contract_base/wrappers/serializer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use core::marker::PhantomData;
use core::{convert::Infallible, marker::PhantomData};

use unwrap_infallible::UnwrapInfallible;

use crate::codec::{
DecodeError, DecodeErrorHandler, EncodeError, EncodeErrorHandler, TopDecode, TopEncode,
Expand Down Expand Up @@ -30,19 +32,23 @@ where

pub fn top_encode_to_managed_buffer<T: TopEncode>(&self, value: &T) -> ManagedBuffer<M> {
let mut result = ManagedBuffer::new();
let Ok(()) = value.top_encode_or_handle_err(
&mut result,
ExitCodecErrorHandler::<M>::from(err_msg::SERIALIZER_ENCODE_ERROR),
);
value
.top_encode_or_handle_err(
&mut result,
ExitCodecErrorHandler::<M>::from(err_msg::SERIALIZER_ENCODE_ERROR),
)
.unwrap_infallible();
result
}

pub fn top_encode_to_boxed_bytes<T: TopEncode>(&self, value: &T) -> BoxedBytes {
let mut result = BoxedBytes::empty();
let Ok(()) = value.top_encode_or_handle_err(
&mut result,
ExitCodecErrorHandler::<M>::from(err_msg::SERIALIZER_ENCODE_ERROR),
);
value
.top_encode_or_handle_err(
&mut result,
ExitCodecErrorHandler::<M>::from(err_msg::SERIALIZER_ENCODE_ERROR),
)
.unwrap_infallible();
result
}

Expand All @@ -55,19 +61,19 @@ where
buffer: &ManagedBuffer<M>,
error_message: &'static [u8],
) -> T {
let Ok(value) = T::top_decode_or_handle_err(
T::top_decode_or_handle_err(
buffer.clone(), // TODO: remove clone
ExitCodecErrorHandler::<M>::from(error_message),
);
value
)
.unwrap_infallible()
}

pub fn top_decode_from_byte_slice<T: TopDecode>(&self, slice: &[u8]) -> T {
let Ok(value) = T::top_decode_or_handle_err(
T::top_decode_or_handle_err(
slice,
ExitCodecErrorHandler::<M>::from(err_msg::SERIALIZER_DECODE_ERROR),
);
value
)
.unwrap_infallible()
}
}

Expand Down Expand Up @@ -98,7 +104,7 @@ impl<M> EncodeErrorHandler for ExitCodecErrorHandler<M>
where
M: ManagedTypeApi + ErrorApi,
{
type HandledErr = !;
type HandledErr = Infallible;

fn handle_error(&self, err: EncodeError) -> Self::HandledErr {
let mut message_buffer = ManagedBuffer::<M>::new_from_bytes(self.base_message);
Expand All @@ -111,7 +117,7 @@ impl<M> DecodeErrorHandler for ExitCodecErrorHandler<M>
where
M: ManagedTypeApi + ErrorApi,
{
type HandledErr = !;
type HandledErr = Infallible;

fn handle_error(&self, err: DecodeError) -> Self::HandledErr {
let mut message_buffer = ManagedBuffer::<M>::new_from_bytes(self.base_message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::marker::PhantomData;

use unwrap_infallible::UnwrapInfallible;

use crate::codec::{TopDecode, TopEncode};

use crate::{
Expand Down Expand Up @@ -63,9 +65,8 @@ where
result_buffer.get_handle(),
);

let Ok(value) =
V::top_decode_or_handle_err(result_buffer, StorageGetErrorHandler::<A>::default());
value
V::top_decode_or_handle_err(result_buffer, StorageGetErrorHandler::<A>::default())
.unwrap_infallible()
}

/// Write a serializable value to storage under the given key
Expand Down
7 changes: 5 additions & 2 deletions framework/base/src/formatter/formatter_traits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use unwrap_infallible::UnwrapInfallible;

use crate::codec::TopEncode;

use crate::{
Expand Down Expand Up @@ -90,10 +92,11 @@ pub trait SCCodec {
impl<T: TopEncode> SCCodec for T {
fn fmt<F: FormatByteReceiver>(&self, f: &mut F) {
let mut encoded = ManagedBuffer::<F::Api>::new();
let Ok(()) = self.top_encode_or_handle_err(
self.top_encode_or_handle_err(
&mut encoded,
ExitCodecErrorHandler::<F::Api>::from(err_msg::FORMATTER_ENCODE_ERROR),
);
)
.unwrap_infallible();
SCLowerHex::fmt(&encoded, f);
}
}
4 changes: 2 additions & 2 deletions framework/base/src/io/arg_error_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::marker::PhantomData;
use core::{convert::Infallible, marker::PhantomData};

use crate::{
api::{ErrorApi, ManagedTypeApi},
Expand Down Expand Up @@ -33,7 +33,7 @@ impl<M> DecodeErrorHandler for ArgErrorHandler<M>
where
M: ManagedTypeApi + ErrorApi,
{
type HandledErr = !;
type HandledErr = Infallible;

#[inline(always)]
fn handle_error(&self, err: DecodeError) -> Self::HandledErr {
Expand Down
9 changes: 4 additions & 5 deletions framework/base/src/io/arg_nested_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use unwrap_infallible::UnwrapInfallible;

use super::{EndpointDynArgLoader, EndpointSingleArgLoader, ManagedResultArgLoader};
use crate::{
api::{
Expand Down Expand Up @@ -71,8 +73,7 @@ where
{
let mut arg_loader = EndpointSingleArgLoader::<AA>::new(index);
let h = ArgErrorHandler::<AA>::from(arg_id);
let Ok(value) = T::multi_decode_or_handle_err(&mut arg_loader, h);
value
T::multi_decode_or_handle_err(&mut arg_loader, h).unwrap_infallible()
}

#[inline(never)]
Expand All @@ -83,9 +84,7 @@ where
T: TopDecodeMulti,
{
let h = ArgErrorHandler::<AA>::from(arg_id);
let result = T::multi_decode_or_handle_err(loader, h);
let Ok(value) = result;
value
T::multi_decode_or_handle_err(loader, h).unwrap_infallible()
}

/// Models an argument tree of the form `(arg1, (arg2, ... (argn, ())))`, used for retrieving endpoint arguments.
Expand Down
5 changes: 4 additions & 1 deletion framework/base/src/io/finish.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::marker::PhantomData;

use unwrap_infallible::UnwrapInfallible;

use crate::codec::{EncodeErrorHandler, TopEncodeMulti, TopEncodeMultiOutput, TryStaticCast};

use crate::{
Expand All @@ -20,7 +22,8 @@ where
{
let h = ExitCodecErrorHandler::<FA>::from(err_msg::FINISH_ENCODE_ERROR);
let mut output = ApiOutputAdapter::<FA>::default();
let Ok(()) = item.multi_encode_or_handle_err(&mut output, h);
item.multi_encode_or_handle_err(&mut output, h)
.unwrap_infallible()
}

#[derive(Clone)]
Expand Down
2 changes: 0 additions & 2 deletions framework/base/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![no_std]
#![feature(never_type)]
#![feature(exhaustive_patterns)]
#![feature(try_trait_v2)]
#![feature(control_flow_enum)]
#![feature(negative_impls)]
Expand Down
Loading

0 comments on commit ee35a53

Please sign in to comment.