Minor improvements to bounded_vec and defensive. (#10873)

* Fix a few things in bounded_vec

* add test for try_extend

* Update frame/support/src/storage/bounded_vec.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* some review comments

* use swap

* remove clone

* use pop instead of truncate

* remove warn

* review comments

* Update frame/support/src/storage/bounded_vec.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fix rustdoc

* fix links

* undo link

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Kian Paimani
2022-02-26 08:39:56 +00:00
committed by GitHub
parent 4c984500a7
commit b77d3f917d
5 changed files with 189 additions and 171 deletions
+43 -100
View File
@@ -24,8 +24,34 @@ use sp_arithmetic::traits::{CheckedAdd, CheckedMul, CheckedSub, Saturating};
use sp_runtime::{traits::Block as BlockT, DispatchError};
use sp_std::{cmp::Ordering, prelude::*};
const DEFENSIVE_OP_PUBLIC_ERROR: &'static str = "a defensive failure has been triggered; please report the block number at https://github.com/paritytech/substrate/issues";
const DEFENSIVE_OP_INTERNAL_ERROR: &'static str = "Defensive failure has been triggered!";
#[doc(hidden)]
pub const DEFENSIVE_OP_PUBLIC_ERROR: &'static str = "a defensive failure has been triggered; please report the block number at https://github.com/paritytech/substrate/issues";
#[doc(hidden)]
pub const DEFENSIVE_OP_INTERNAL_ERROR: &'static str = "Defensive failure has been triggered!";
/// Generic function to mark an execution path as ONLY defensive.
///
/// Similar to mark a match arm or `if/else` branch as `unreachable!`.
#[macro_export]
macro_rules! defensive {
() => {
frame_support::log::error!(
target: "runtime",
"{}",
$crate::traits::misc::DEFENSIVE_OP_PUBLIC_ERROR
);
debug_assert!(false, "{}", $crate::traits::misc::DEFENSIVE_OP_INTERNAL_ERROR);
};
($error:tt) => {
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
$crate::traits::misc::DEFENSIVE_OP_PUBLIC_ERROR,
$error
);
debug_assert!(false, "{}: {:?}", $crate::traits::misc::DEFENSIVE_OP_INTERNAL_ERROR, $error);
}
}
/// Prelude module for all defensive traits to be imported at once.
pub mod defensive_prelude {
@@ -116,12 +142,7 @@ impl<T> Defensive<T> for Option<T> {
match self {
Some(inner) => inner,
None => {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR
);
defensive!();
or
},
}
@@ -131,12 +152,7 @@ impl<T> Defensive<T> for Option<T> {
match self {
Some(inner) => inner,
None => {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR
);
defensive!();
f()
},
}
@@ -149,12 +165,7 @@ impl<T> Defensive<T> for Option<T> {
match self {
Some(inner) => inner,
None => {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR
);
defensive!();
Default::default()
},
}
@@ -164,12 +175,7 @@ impl<T> Defensive<T> for Option<T> {
match self {
Some(inner) => Some(inner),
None => {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR
);
defensive!();
None
},
}
@@ -181,13 +187,7 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
match self {
Ok(inner) => inner,
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
or
},
}
@@ -197,13 +197,7 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
match self {
Ok(inner) => inner,
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
f()
},
}
@@ -216,13 +210,7 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
match self {
Ok(inner) => inner,
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
Default::default()
},
}
@@ -232,13 +220,7 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
match self {
Ok(inner) => Ok(inner),
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
Err(e)
},
}
@@ -248,13 +230,7 @@ impl<T, E: sp_std::fmt::Debug> Defensive<T> for Result<T, E> {
impl<T, E: sp_std::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
fn defensive_map_err<F, O: FnOnce(E) -> F>(self, o: O) -> Result<T, F> {
self.map_err(|e| {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
o(e)
})
}
@@ -262,13 +238,7 @@ impl<T, E: sp_std::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
fn defensive_map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
self.map_or_else(
|e| {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
default(e)
},
f,
@@ -279,13 +249,7 @@ impl<T, E: sp_std::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
match self {
Ok(inner) => Some(inner),
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
None
},
}
@@ -295,13 +259,7 @@ impl<T, E: sp_std::fmt::Debug> DefensiveResult<T, E> for Result<T, E> {
match self {
Ok(inner) => Ok(f(inner)),
Err(e) => {
debug_assert!(false, "{}: {:?}", DEFENSIVE_OP_INTERNAL_ERROR, e);
frame_support::log::error!(
target: "runtime",
"{}: {:?}",
DEFENSIVE_OP_PUBLIC_ERROR,
e
);
defensive!(e);
Err(e)
},
}
@@ -312,12 +270,7 @@ impl<T> DefensiveOption<T> for Option<T> {
fn defensive_map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
self.map_or_else(
|| {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR,
);
defensive!();
default()
},
f,
@@ -326,12 +279,7 @@ impl<T> DefensiveOption<T> for Option<T> {
fn defensive_ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
self.ok_or_else(|| {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR,
);
defensive!();
err()
})
}
@@ -340,12 +288,7 @@ impl<T> DefensiveOption<T> for Option<T> {
match self {
Some(inner) => Some(f(inner)),
None => {
debug_assert!(false, "{}", DEFENSIVE_OP_INTERNAL_ERROR);
frame_support::log::error!(
target: "runtime",
"{}",
DEFENSIVE_OP_PUBLIC_ERROR,
);
defensive!();
None
},
}